Building a Consistent Design System with Tailwind CSS

Building a Consistent Design System with Tailwind CSS

Tokens, component variants and a few rules that stop Tailwind projects from turning into class soup.

Tailwind makes it fast to build anything, which also makes it easy to build everything slightly differently. A few rules turn it into a design system.

Define tokens, not one-off values

Put brand colors, spacing, radii and font sizes in the theme and use only those. If you find yourself typing arbitrary values like text-[13px], add a token or use an existing one.

Components own their classes

Pages should compose Button, Card and Input components, not repeat long class strings. When a style changes, you change it in one file.

Variants with CVA

const button = cva("inline-flex items-center rounded-md font-medium", {
  variants: {
    intent: { primary: "bg-primary text-white", ghost: "bg-transparent" },
    size: { sm: "h-8 px-3 text-sm", md: "h-10 px-4" },
  },
  defaultVariants: { intent: "primary", size: "md" },
});

Dark mode through variables

Map colors to CSS variables and switch the variables for dark mode. Components then use bg-background and text-foreground and work in both themes without dark: prefixes everywhere.

Keep it readable

  • Use the Prettier Tailwind plugin to keep class order consistent.
  • Merge conflicting classes with tailwind-merge in reusable components.
  • Extract a component when the same class string appears three times.

More on this topic

Photography
3D Models
Development
Illustrations
Fashion
Digital Art
Packaging
Motion
Illustrations
Video Production
Photography
3D Models
Development
Illustrations
Fashion