Next.jsMedium

What is the recommended styling approach for Server Components?

Use styling that is resolved at build time, so it needs no JavaScript in the browser: Tailwind CSS, CSS Modules, global CSS with CSS variables, Sass, or zero-runtime CSS-in-JS (vanilla-extract, Panda CSS, StyleX). Avoid runtime CSS-in-JS in Server Components, because it needs client hooks and context.

Why build-time styling fits

  • Server Components render once on the server; there is no client runtime to inject styles.
  • Static CSS files are cacheable, chunked per route and loaded in parallel with HTML.
  • Zero JavaScript cost keeps hydration and INP fast.

Options compared

ApproachWorks in Server ComponentsRuntime JS
Tailwind CSSYesNone
CSS Modules / Sass ModulesYesNone
Global CSS + variablesYesNone
vanilla-extract, Panda, StyleX, KumaYesNone or minimal
styled-components, EmotionNo (Client only + registry)Yes

Dynamic styling without runtime CSS-in-JS

  • Toggle classes based on props: className={cn("btn", variant === "primary" && "btn-primary")}.
  • Use data-* attributes with CSS selectors: [data-state="open"].
  • Pass values via CSS variables in style.
  • Use variant helpers like class-variance-authority (pure functions, fine on the server).

Component libraries

Headless or Tailwind-based libraries (shadcn/ui, Radix, React Aria) work well because the styling is plain classes; interactive parts are Client Components but styles are still static CSS.

Code example

tsx
1// components/badge.tsx - Server Component with variants, zero runtime styling
2import { cva, type VariantProps } from "class-variance-authority";
3
4const badge = cva("inline-flex items-center rounded-full px-2 py-0.5 text-xs font-medium", {
5 variants: {
6 tone: {
7 success: "bg-green-100 text-green-800",
8 warning: "bg-amber-100 text-amber-800",
9 danger: "bg-red-100 text-red-800",
10 },
11 },
12 defaultVariants: { tone: "success" },
13});
14
15type Props = VariantProps<typeof badge> & { children: React.ReactNode };
16
17export function Badge({ tone, children }: Props) {
18 return (
19 <span className={badge({ tone })} data-tone={tone}>
20 {children}
21 </span>
22 );
23}
24
25// Usage in any Server Component: <Badge tone="warning">Pending</Badge>

Practice this question

Open it in the study app to track progress, take notes, listen to the answer and work through the rest of the Next.js set.

Open in study app