WhoBuilt

Styling

Jul 3, 2020· 20 min· 30K views·Watch on YouTube

Learn Next.js, the channel’s presenter, explains how to style Next.js applications with CSS, Sass, CSS-in-JS, and responsive component libraries.

Import global CSS

The presenter describes enabling Next.js’s experimental CSS flag to import stylesheets directly. They show adding a global stylesheet through `_app.js`, which also lets developers keep shared layout or state across page changes.

Add Sass and CSS-in-JS

Sass adds features such as nesting, partials, and mixins, and the example uses a Next.js plugin to enable Sass imports. The presenter also introduces built-in styled-jsx and favors styled-components for writing component-scoped CSS with dynamic styles and themes.

Configure styled-components

The setup uses `_app.js` to wrap pages in a ThemeProvider and `_document.js` to collect styles for server rendering. A `.babelrc` can extend Next.js’s Babel preset with the styled-components plugin, enabling server-side rendering and readable display names for debugging.

Build mobile-first responsive layouts

The presenter recommends applying mobile styles first, then using a `min-width` media query to increase an article’s font from 12 to 18 pixels above 768 pixels. They show Chakra UI’s responsive array syntax as another way to set font sizes at mobile, medium, and larger breakpoints.

Use component library styling props

Chakra’s Box component accepts CSS properties as React props, while its ThemeProvider and CSS reset provide shared theme values and consistent browser defaults. Chakra’s breakpoints can be customized, reducing the need to write repeated media queries by hand.

Takeaways

  • Choose plain CSS, Sass, or CSS-in-JS based on the project’s needs and team preferences.
  • Use `_app.js` for shared styles and theme context across Next.js pages.
  • Styled-components can scope styles, support themes, and collect styles for server rendering.
  • Start with mobile styles and add larger-screen rules at explicit breakpoints.
  • Responsive component props can reduce repeated media-query boilerplate.

Mentioned in this talk

Search inside this talk

What was claimed 6

  • feature
    Next.js: Supports importing CSS directly behind an experimental feature flag; CSS Modules support was described as forthcoming.
    “they added an experimental feature flag that you can turn on”
    YouTube
    Unverified1 source· Talk
  • feature
    Next.js: Includes styled-jsx for CSS-in-JS without extra setup.
    “it works out of the box”
    YouTube
    Unverified1 source· Talk
  • feature
    styled-components: Supports theming through ThemeProvider and scopes styles to avoid global CSS conflicts.
    “you can theme your application using the styled-components ThemeProvider”
    YouTube
    Unverified1 source· Talk
  • feature
    styled-components: Automatically prefixes vendor styles during compilation.
    “automatically prefixing all the vendor styles”
    YouTube
    Unverified1 source· Talk
  • uses
    “the defaults for spacing, styling, colors, and all of these things come from Tailwind”
    YouTube
    Unverified1 source· Talk
  • uses
    “It uses a library called Styled System under the hood”
    YouTube
    Unverified1 source· Talk

Related talks