Skip to content

Lesson 40 of 52Tailwind in practice

Tailwind Design Systems

Turn Tailwind into your own design system: custom tokens with @theme, reusable components instead of @apply, readable class lists, and class merging.

18 min read0/5 questions

Out of the box, Tailwind gives you its own colours, fonts and spacing. A real product has its own brand: a specific green, a heading font, a particular corner radius on every card. A design system is that set of shared decisions, written down once and reused everywhere, plus the components built from them. This lesson shows how to put your brand into Tailwind with @theme, how to reuse styles without drowning in repeated class lists, how to merge classes safely, and how to tell when Tailwind is the right tool and when it is not.

Tokens, utilities, components

A design system has layers. Small decisions at the bottom feed bigger pieces at the top.

The layers of a Tailwind design system
  1. Pagesthe pricing page, the dashboard
  2. ComponentsButton, Card, Badge in React
  3. Utility classesbg-brand-600, font-display
  4. Design tokens@theme { --color-brand-600: ... }

Design tokens are named values: your brand colours, fonts, radii. You define them once in CSS with @theme.

1/4

Custom tokens with @theme

In Tailwind version 4, configuration lives in your CSS file, right under the import. The @theme block holds theme variables: CSS custom properties with special name prefixes that tell Tailwind which utilities to create.

@import "tailwindcss";
 
@theme {
  --color-brand-50: oklch(0.97 0.02 160);
  --color-brand-600: oklch(0.55 0.15 160);
  --color-brand-900: oklch(0.3 0.08 160);
  --font-display: "Fraunces", Georgia, serif;
  --radius-card: 1.25rem;
  --breakpoint-3xl: 120rem;
}

Each prefix is a namespace that maps to a family of utilities:

Theme variableCreates classes like
--color-brand-600bg-brand-600, text-brand-600, border-brand-600
--font-displayfont-display
--text-hugetext-huge (font size)
--radius-cardrounded-card
--shadow-softshadow-soft
--breakpoint-3xlthe 3xl: variant
--spacingthe base unit behind p-4, gap-2, w-10

These are still normal CSS variables too. Tailwind outputs them on :root, so you can write var(--color-brand-600) in plain CSS or an inline style when you need to.

You can try this live. The preview's Tailwind reads @theme from a <style type="text/tailwindcss"> block:

HTML + Tailwind
<style type="text/tailwindcss"> @theme { --color-brand-50: oklch(0.97 0.02 160); --color-brand-600: oklch(0.55 0.15 160); --color-brand-900: oklch(0.3 0.08 160); --font-display: Georgia, "Times New Roman", serif; --radius-card: 1.25rem; }</style><article class="max-w-sm rounded-card bg-brand-50 p-6"> <p class="text-sm font-semibold uppercase tracking-wide text-brand-600">New</p> <h2 class="mt-1 font-display text-3xl text-brand-900">Seasonal soup club</h2> <p class="mt-2 text-slate-600">A fresh recipe and a bag of spices, every other Friday.</p> <button class="mt-4 rounded-full bg-brand-600 px-5 py-2 font-semibold text-white">Join</button></article>

Preview

Change the last number in each oklch(...) from 160 to 30 and the whole card turns warm orange. That number is the hue. One token edit, and every class that uses it follows. Then try --radius-card: 0 for sharp corners.

To replace a whole namespace instead of adding to it, reset it first: --color-*: initial; removes all default colours so only yours remain. Teams do this so nobody can reach for an off-brand bg-pink-400.

Semantic tokens and dark mode

So far the tokens describe raw colours: brand-600, brand-50. Bigger systems add a second layer of semantic tokens that describe a job instead: surface for card backgrounds, content for body text, accent for buttons. Components use only the semantic names, and the semantic names point at raw colours.

The pay-off comes with themes. Because Tailwind's utilities read your tokens through CSS variables, you can redefine a token under a selector and every utility that uses it changes:

HTML + Tailwind
<style type="text/tailwindcss"> @theme { --color-surface: #ffffff; --color-content: #1e293b; --color-accent: #0f766e; } .dark { --color-surface: #0f172a; --color-content: #e2e8f0; --color-accent: #2dd4bf; }</style><div class="dark"> <article class="max-w-sm rounded-2xl bg-surface p-6 text-content"> <h2 class="text-xl font-bold">Your weekly plan</h2> <p class="mt-2 opacity-80">Five dinners, one shopping list, zero decisions.</p> <button class="mt-4 rounded-lg bg-accent px-4 py-2 font-semibold text-surface">Open plan</button> </article></div>

Preview

Remove class="dark" from the outer div and the card switches to its light theme. Notice the card itself has no dark: classes at all: bg-surface and text-content simply mean different colours in each theme. That keeps component class lists short, and adding a third theme later is one more block of variables.

Predict the outputHTML & CSS

After adding this token, what class gives an element a mint background? Type the class name.

@theme {
--color-mint-500: oklch(0.8 0.12 170);
}
Need a nudge? Hints walk you through it.
Quick check
What is the difference between putting --color-brand: #0a7 inside @theme versus inside a plain :root rule?
Need a nudge? A hint rules out a wrong answer.

Reuse with components, not long @apply rules

Once you have a nice button, you need it in forty places. Copying the same twelve classes forty times is a maintenance problem. Tailwind's advice is to reuse in this order:

  1. Loops. If the repetition comes from a list, render it with a loop so the classes are written once.
  2. Components. In React, Vue, Svelte or any template system, make a Button component. The class list lives in one file.
  3. @apply for small, plain-CSS cases. It copies utilities into a normal CSS rule, which helps when you cannot add classes (styling content from a CMS, or a third-party widget).
/* Fine for small, stable cases */
.prose-link {
  @apply font-medium text-brand-600 underline underline-offset-2;
}

Why not use @apply for everything? Because you end up back where you started: inventing class names, a growing stylesheet, and styles far from the markup. Components keep the utility workflow and remove the repetition. Here is a typical React button:

// Button.jsx
const VARIANTS = {
  primary: "bg-brand-600 text-white hover:bg-brand-900",
  secondary: "border border-slate-300 bg-white text-slate-900 hover:bg-slate-50",
};
 
export function Button({ variant = "primary", className, ...props }) {
  return (
    <button
      className={cn(
        "inline-flex items-center gap-2 rounded-full px-5 py-2 font-semibold",
        "focus-visible:outline-2 focus-visible:outline-offset-2 disabled:opacity-50",
        VARIANTS[variant],
        className,
      )}
      {...props}
    />
  );
}

Notice the variant classes are full strings in an object, so Tailwind can find them when it scans the file. Libraries like class-variance-authority (cva) formalise this variant pattern, and component kits like shadcn/ui are built this way.

For tiny reusable helpers that should behave like real utilities (working with hover: and md:), version 4 has @utility:

@utility text-balance-safe {
  text-wrap: balance;
  overflow-wrap: anywhere;
}

Class merging: clsx and tailwind-merge

The Button above calls a helper named cn. It solves two problems.

Conditional classes. You often want a class only sometimes: active tabs, error states. The small clsx library joins strings and skips anything false:

clsx("rounded-lg px-3", isActive && "bg-slate-900 text-white", hasError && "border-red-500");
// "rounded-lg px-3 bg-slate-900 text-white" when active and no error

Conflicting classes. Suppose someone uses <Button className="bg-red-600"> to make a danger button. Now the element has both bg-brand-600 and bg-red-600. You might expect the later one in the attribute to win, but the order of classes in HTML does not matter in CSS. What matters is the order of the rules in the stylesheet, which Tailwind decides. So the result is unreliable. The tailwind-merge library understands Tailwind's classes and removes the earlier conflicting one, so the last class you pass really does win.

import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";
 
export function cn(...inputs) {
  return twMerge(clsx(inputs));
}
 
cn("px-4 py-2 bg-brand-600", "bg-red-600");
// "px-4 py-2 bg-red-600"

This tiny cn function appears in a huge number of React and Tailwind codebases.

Quick check
An element has p-2 p-6 in its class attribute, in that order, with no merging. Which padding applies?
Need a nudge? A hint rules out a wrong answer.

Keeping class lists readable

Long class lists are Tailwind's main cost. A few habits keep them manageable:

  • Sort them automatically. The official Prettier plugin, prettier-plugin-tailwindcss, sorts classes into a consistent order every time you save, so every file reads the same way.
  • Break long lists into lines inside cn(...): base layout on one line, states on another, variants on a third, as the Button does.
  • Extract components early. If a class list is hard to read, it probably wants a name, and a component gives it one.
  • Use tokens instead of arbitrary values. bg-brand-600 says what it means; bg-[#0a7a55] does not.
  • Install the editor extension. Tailwind CSS IntelliSense autocompletes your custom tokens and shows the CSS on hover.
Match the pairs0/5 matched

Match each tool or feature to its job.

Choose an item, then its match on the other side.

When Tailwind fits, and when it does not

Good fitWeaker fit
Component-based apps (React, Vue, Svelte, Next.js)Styling HTML you do not control, like CMS or Markdown output (though the official typography plugin helps)
Teams that want one consistent scaleA tiny one-page site where a short stylesheet is simpler
Building and iterating quicklyTeams that strongly prefer separate stylesheets
Design systems with clear tokensHeavy, unusual animation work, which is often clearer as plain CSS

Tailwind is a tool, not a religion. Plenty of excellent sites use CSS Modules or plain CSS. You can also mix: Tailwind for layout and spacing, a hand-written CSS file for a complex animation. Everything compiles to CSS in the end.

Quick check
Your team keeps copying a 14-class button into every page of a React app. What is the best fix?
Need a nudge? A hint rules out a wrong answer.
Key terms
design system
A shared set of design decisions (tokens) and components that keep a product consistent.
design token
A named design value, like a brand colour or radius, defined once and reused everywhere.
@theme
Tailwind v4's CSS block for theme variables. Each variable creates matching utility classes.
@apply
A Tailwind directive that copies utility classes into a regular CSS rule.
clsx
A tiny library that joins class names and drops the ones whose condition is false.
tailwind-merge
A library that removes conflicting Tailwind classes so the last one passed wins.