Styling: Global CSS, CSS Modules, Tailwind and Styled Components

Next.js Course: Beginner to Advanced with Examples · Components and Layouts · 5 min read · Updated

Four ways to style

  1. Global CSS applies to the whole site. Use it for variables and resets.
  2. CSS Modules make every class name unique, so styles cannot clash.
  3. Tailwind lets you build a look from many small classes inside className.
  4. styled-components write CSS in JavaScript and need Client Components and some setup.

Next.js does not force one way of styling. It supports several, and each has its own strengths. In this lesson we look at four of them: global CSS, CSS Modules, Tailwind CSS and styled-components. Think of clothes. Global CSS is a uniform for everyone. CSS Modules are name-tagged clothes, so nobody takes the wrong one. Tailwind is a wardrobe of small pieces that you combine. Styled-components are clothes that you sew into each component.

1. Global CSS

A global stylesheet applies to the whole site. A create-next-app project already has one at app/globals.css, and the root layout imports it. Put things there that really are global: the base font, page colours, resets and CSS variables.

/* app/globals.css */
:root {
  --background: #ffffff;
  --foreground: #171717;
}

body {
  background: var(--background);
  color: var(--foreground);
}

You may import a global stylesheet in the root layout. The risk is that a rule written for one page leaks into another, because the class names are shared. That is why big projects use the next options too.

2. CSS Modules

A CSS Module is a CSS file whose name ends in .module.css. Next.js rewrites each class name into a unique one, so a class called primary in one file never clashes with a class called primary in another. You import the file as an object and use its keys.

.primary {
  background: #1d4ed8;
  color: white;
  padding: 0.5rem 1rem;
  border-radius: 0.375rem;
}
import styles from "./Button.module.css";

export default function Button({ children }: { children: React.ReactNode }) {
  return <button className={styles.primary}>{children}</button>;
}

We built a page that used this button, and the production build passed. The class name that arrives in the browser has an extra unique part, so it cannot clash with others. Use CSS Modules when you like writing normal CSS but want it safe by default.

3. Tailwind CSS

Tailwind gives you many small classes such as p-4 for padding, flex for layout and text-lg for a large font. You write them straight in the className. A create-next-app project can set Tailwind up for you, and the globals.css file starts with an import for it.

export default function Badge({ text }: { text: string }) {
  return (
    <span className="rounded bg-blue-600 px-2 py-1 text-sm text-white">
      {text}
    </span>
  );
}
  • Good: very fast to write, a consistent set of sizes and colours, and no separate files to name.
  • Not so good: long class lists can be hard to read, and you must learn the names. Most teams extract repeated pieces into components.

The starter project you made in lesson 4 already used Tailwind classes, and the production build passed with them in the lab. The home page in this course uses a few of them.

4. Styled Components and CSS-in-JS

Libraries such as styled-components let you write CSS inside JavaScript, attached to a component. They are popular in React, but they need extra care in the App Router. They run in the browser, so they only work inside Client Components, and the library needs a small registry setup so that the styles are collected on the server too. Because of this extra work, many new Next.js projects choose Tailwind or CSS Modules instead.

"use client";

import styled from "styled-components";

const Title = styled.h1`
  color: #1d4ed8;
`;

export default function Heading() {
  return <Title>Hello</Title>;
}

How to choose

  • Starting a new project? Tailwind is the default of create-next-app and is a good first choice.
  • You like plain CSS? Use CSS Modules.
  • You maintain an older project with styled-components? Keep it, but isolate it in Client Components.
  • Always keep a small global file for variables and resets.

Good habits

  • Import global CSS only in the root layout.
  • Do not mix too many methods in one project. Two is plenty.
  • Check your pages at a phone width of about 390 pixels. A page that scrolls sideways has a layout problem.
  • Respect users who prefer less motion. Keep animations small.

Your task

  1. Make a Button component with a CSS Module and use it on two pages.
  2. Make a Badge component with Tailwind classes.
  3. Open the browser tools and look at the class name that your CSS Module produced.
  4. Write two sentences on which method you would pick for your next project and why.

How to check it worked

  • Your button looks styled, and the class name in the browser is not just primary.
  • You can name one strength and one weakness of each method.
  • You know why styled-components needs a Client Component.

Frequently asked questions

Which styling method is the best for Next.js?

There is no single best. Tailwind is the default of create-next-app, CSS Modules are a safe choice for plain CSS, and CSS-in-JS libraries need extra setup in the App Router.

Where do I import global CSS?

In the root layout, app/layout.tsx. That makes it apply to every page.

Can I use Sass?

Yes. Install the sass package and name your files .scss or .module.scss. Check the Next.js documentation for the current options.