Skip to content

CSS Modules

CSS Modules is an important part of building production-ready design systems systems. This lesson explains what css modules means, how it works, and how to apply it with practical examples you can reuse.

CSS Modules Overview

At its core, css modules is about doing one thing well inside your design systems project. Once you understand the pattern, you can apply it consistently across features and teams.

Good css modules pays off across the whole codebase: fewer surprises, easier testing, and smoother onboarding. The snippet below is a solid starting point.

/* Button.module.css */
.primary {
  background: var(--color-brand-500);
  color: #fff;
  padding: var(--space-2) var(--space-4);
  border-radius: var(--radius-md);
}

CSS Modules scope styles locally while still reading shared tokens from CSS custom properties.

CSS Modules Example

/* Tokens flow into components, which flow into products */
:root { --color-brand-500: #2563eb; }
.button { background: var(--color-brand-500); }
  • Start from a minimal CSS Modules example and grow it only as needed.
  • Keep configuration explicit so CSS Modules behaves the same in every environment.
  • Name things clearly so teammates understand your CSS Modules at a glance.
  • Add tests around CSS Modules early to lock in expected behaviour.

Design System Cheatsheet

Quick reference for css modules within a modern design system.

Concept Example Purpose
Token --color-brand-500: #2563eb Single source of design decisions
Semantic token --color-text: var(--color-brand-500) Meaningful, theme-ready names
Component <ds-button variant="primary"> Reusable, consistent UI
Theme [data-theme="dark"] Swap token values at runtime
Docs Storybook story Show usage and states
Distribution @acme/design-system on npm Share across products
Accessibility roles + ARIA + keyboard Usable by everyone

How CSS Modules Fits into a Design System

CSS Modules connects the design decisions in your system to the code teams actually ship. When it is defined once and reused, products stay consistent and updates roll out everywhere.

CSS Modules scope styles locally while still reading shared tokens from CSS custom properties.

  • Define it once as a token or shared component.
  • Document intended usage with clear examples.
  • Make it themeable and accessible by default.
  • Version and release changes so consumers can upgrade safely.

Getting Teams to Adopt CSS Modules

Adoption is where design systems succeed or stall. Make css modules the easiest option: great docs, sensible defaults, and clear migration paths beat mandates every time.

Lever Why it drives adoption
Documentation Teams use what they can understand quickly
Defaults Accessible, on-brand results with zero config
Tooling Linting and codemods reduce migration cost
Support A responsive team builds trust

Common Mistakes

  • Copying css modules snippets without understanding what each line does.
  • Skipping error handling and edge cases when wiring up css modules.
  • Leaving css modules untested, so regressions slip into production.
  • Over-engineering css modules before you actually need the extra flexibility.

Key Takeaways

  • CSS Modules is a core part of working effectively with design systems.
  • Start small and keep css modules focused on a single responsibility.
  • Apply consistent patterns so css modules scales across your project.
  • Test and document css modules to keep it maintainable over time.

Pro Tip

Bookmark this css modules pattern and reuse it. Consistency across your design systems codebase is worth more than clever one-off solutions.