Menu

Styling Options in Next.js

Lesson 1: Styling Options in Next.js

Which styling options are available?

Option Scope Setup Best for
Global CSS Whole app Import in root layout Resets, base typography, CSS variables
CSS Modules One component Name a file *.module.css Component styles without class-name clashes
Tailwind CSS Utility classes Chosen in create-next-app Fast, consistent UI building
Sass Global or module npm install sass Teams already using Sass
CSS-in-JS (styled-components, Emotion) Component Library + registry setup Existing codebases; works only in Client Components

Example: CSS Modules

/* app/courses/CourseCard.module.css */
.card {
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 1rem;
}
.title {
  font-size: 1.25rem;
  font-weight: 600;
}
import styles from './CourseCard.module.css';

export function CourseCard({ title }: { title: string }) {
  return (
    <article className={styles.card}>
      <h2 className={styles.title}>{title}</h2>
    </article>
  );
}

Example: Tailwind CSS

With Tailwind CSS v4, app/globals.css contains a single import:

@import "tailwindcss";
export function CourseCard({ title, level }: { title: string; level: string }) {
  return (
    <article className="rounded-xl border border-gray-200 p-4 hover:shadow-md">
      <h2 className="text-xl font-semibold">{title}</h2>
      <span className="text-sm text-gray-500">{level}</span>
    </article>
  );
}

How it works

  • CSS Modules generate unique class names at build time, such as CourseCard_card__x7f2a, so styles never clash.
  • Tailwind scans your files and includes only the classes you use in the final CSS.
  • Global CSS should be imported once, in app/layout.tsx.
  • Both CSS Modules and Tailwind work in Server and Client Components with no runtime cost.

Common mistakes

Mistake Effect Fix
Importing global CSS in many components Unexpected overrides, ordering issues Import global CSS in the root layout only
Building class names dynamically in Tailwind (text-${color}-500) Classes missing in production Use full class names, e.g. a lookup object
Using runtime CSS-in-JS in Server Components Errors or missing styles Prefer CSS Modules or Tailwind

Key takeaways

  • CSS Modules and Tailwind are the two most common, zero-runtime choices.
  • Keep global CSS small: resets, variables, and base styles.

Try it: Style the LearnHub CourseCard twice, once with a CSS Module and once with Tailwind, and compare the code.


Nextjs Tutorial

J

Jebasta





Get in Touch For Details