Skip to main content

Welcome to a Grasp lesson

Create a free account to keep studying and create your own study path.

Lesson 10

CSS in Design Systems: A Comparative Study

Recommended resources

Hello! In our last lesson, we architected a robust design token hierarchy, establishing a single source of truth for our UI's visual language. We differentiated between primitive, semantic, and component tokens, understanding that this layered structure is key to building a scalable and maintainable system.

Now that we have our design decisions codified as tokens, the next critical architectural question is: how do we consume them? This lesson bridges the gap between token definition and implementation. We will compare the dominant strategies for applying styles in a modern frontend application, a decision that has profound implications for performance, developer experience, and long-term maintainability. For a senior engineer, this isn't just about personal preference; it's about choosing the right tool for the right job based on a deep understanding of the trade-offs involved.

We will analyze three core paradigms:

  • Utility-First CSS, which you're familiar with through Tailwind CSS.
  • CSS-in-JS, exploring both its powerful runtime and more performant compile-time variants.
  • CSS Custom Properties (Variables), the native browser feature that has become the foundation of modern, dynamic styling systems.

By the end of this lesson, you'll be able to articulate why the most sophisticated design systems often don't choose just one, but instead create a hybrid architecture that leverages the best of each approach.

The Landscape of CSS-in-Components

Before we dive into specific libraries, let's establish what we're looking for in a good CSS component system. A robust system should provide scoping, reusability, a way to enforce design constraints (our tokens), composability, and ideally, dead code elimination. The following podcast offers a great overview of these foundational needs.

CSS Components: Tailwind, Panda, Scoped, Modules, Classes, Variables, CSS-in-JS and Sprinkles!

The hosts of the Syntax podcast, Wes Bos and Scott Tolinski, frame the discussion by outlining the essential features of any component-based CSS architecture.

Watch the introductory segment from the beginning of the discussion. Pay attention to the five key characteristics they identify: scoping, reusability, predefined styles (systematization), composability, and dead code elimination. These are the criteria we'll use to evaluate each approach.


Paradigm 1: Utility-First CSS (e.g., Tailwind CSS)

Given your experience with Tailwind, we'll start here but view it through an architectural lens.

The Philosophy: Instead of writing CSS, you apply a pre-defined, constrained set of utility classes directly in your markup. These classes are low-level and often map directly to a single CSS property (e.g., flex, pt-4, text-center). The design system is enforced because developers can only use utilities generated from the design tokens in tailwind.config.js.

Strengths:

  • Performance: Utility classes are generated at build-time. With its Just-in-Time (JIT) compiler, Tailwind produces a highly optimized, minimal CSS file containing only the classes you actually use. There is zero runtime overhead.
  • Developer Velocity: Rapidly building UIs by composing utilities is extremely fast, especially for standard layouts. You rarely leave your HTML/JSX.
  • Consistency: It's difficult to introduce one-off "magic number" styles, as you're forced to use the provided token scale.
  • No Naming Conflicts: You don't have to think about class names, avoiding the need for conventions like BEM.

Weaknesses:

  • Verbose Markup: Can lead to long strings of classes in your HTML, which some developers find unreadable ("class soup").
  • DSL Abstraction: You are learning Tailwind's Domain Specific Language, not necessarily writing raw CSS. When a new CSS feature emerges (like container queries), you often have to wait for the framework to support it or use workarounds.
  • Complex or Custom Styles: Applying highly unique or complex styles to a single element can feel clunky compared to just writing a block of CSS. This often leads to using the @apply directive, which can re-introduce some of the problems utility classes aim to solve.

You can hear a balanced discussion of these points in this segment of the Syntax podcast.

CSS Components: Tailwind, Panda, Scoped, Modules, Classes, Variables, CSS-in-JS and Sprinkles!

This segment focuses specifically on utility-first CSS, with Tailwind as the primary example. The hosts discuss both the benefits and the common criticisms.

Listen to their analysis starting from the section on utility CSS. Note their points about the trade-offs between a full system you get "for free" and the limitations of working within a DSL.


Paradigm 2: CSS-in-JS

CSS-in-JS solutions allow you to write your component's styles directly inside your JavaScript or TypeScript files. This enables a level of dynamic styling and logic that is difficult to achieve otherwise. The key distinction to make here is between runtime and compile-time libraries.

Runtime CSS-in-JS (e.g., Emotion, styled-components)

This was the approach that popularized CSS-in-JS. The library runs in the browser, processing your style objects or strings and injecting CSS rules into the document as components render.

Strengths:

  • True Dynamic Styling: Styles can react to any prop, state, or context from your React application. This is incredibly powerful for creating variants.
  • Colocation: Styles live in the same file as the component logic, creating a self-contained unit.
  • Automatic Scoping: Styles are scoped by default, so you never have to worry about class name collisions.

Weaknesses:
The convenience of runtime CSS-in-JS comes at a significant and often unacceptable performance cost for large-scale applications. The following article, written by a maintainer of Emotion, is a canonical resource on this topic.

Why We're Breaking Up with CSS-in-JS

This article provides a compelling, data-backed analysis of the performance penalties associated with runtime CSS-in-JS, leading the author's team to migrate away from it.

First, read the introduction and the section titled The Good, The Bad, and the Ugly. This gives a balanced overview of the pros and cons. Pay special attention to "The Ugly," which introduces the fundamental performance issues. Next, read the Performance Deep Dive. The key takeaway is the 48% decrease in render time after replacing Emotion with a zero-runtime solution. This is a powerful statistic to have in an interview discussion. The flamegraph analysis clearly shows the overhead of style serialization during the render cycle.

The core issues with runtime CSS-in-JS are:

  1. Runtime Overhead: The CPU cycles spent serializing styles and injecting them into the DOM add up, especially in complex UIs.
  2. Increased Bundle Size: You ship the library's code to every user.
  3. Style Injection Thrashing: As React renders concurrently, frequent insertion of new CSS rules can force the browser to repeatedly recalculate styles for the entire DOM, leading to severe performance bottlenecks.

Compile-time (or "Zero-Runtime") CSS-in-JS (e.g., vanilla-extract, Panda, StyleX)

A newer generation of libraries aims to provide the developer experience of writing styles in TypeScript while eliminating the runtime performance cost. They do this by extracting all styles into static CSS files at build time.

The Philosophy: Get the best of both worlds. Write type-safe, colocated styles, but serve static, performant CSS to the user. Dynamic properties are typically handled by mapping them to CSS variables under the hood.

This approach is rapidly becoming the standard for component libraries that need to offer the flexibility of CSS-in-JS without the performance penalty.


Paradigm 3: CSS Custom Properties (Variables)

CSS Variables are not a framework, but a native browser feature that underpins nearly all modern styling architectures. They are the essential bridge between your design tokens and a dynamic, themeable UI.

The Philosophy: Define reusable values as variables in your CSS that can be updated at runtime.

Design Systems with Brad Frost

Design system expert Brad Frost explains the relationship between design tokens and CSS Custom Properties, and how they are used to power themeable web component systems.

First, watch the segment from where the hosts ask about design tokens. Brad explains how tokens are low-level brand definitions that are ultimately consumed as CSS variables on the web. Then, watch from the explanation of how CSS penetrates Shadow DOM. He describes how a single CSS file containing custom properties can theme an entire library of components, demonstrating their power and efficiency.

Strengths:

  • Native and Performant: The browser is optimized to handle variable changes and style recalculations efficiently.
  • Dynamic Theming: This is their killer feature. You can change an entire application's theme (e.g., from light to dark) by simply changing a class or data-attribute on the <body> or <html> tag, which re-defines the variable values. No React re-renders are needed for the style change itself.
  • Framework Agnostic: They are part of the CSS standard and work everywhere.

The Synthesis: A Hybrid, Senior-Level Approach

In a senior-level system design interview, simply listing the pros and cons of each tool is not enough. The key is to demonstrate that you can synthesize these ideas into a cohesive, scalable architecture. The modern industry-standard, used by libraries like shadcn/ui and recommended by the Next.js team, is a hybrid approach.

Combine Tailwind's Developer Experience with the Power of CSS Variables.

The following resource explains this powerful combination succinctly.

Tailwind vs CSS Variables: Which Should You Use for Color Palettes? | HexPickr | HexPickr

This guide contrasts the two approaches and provides a clear recipe for the modern hybrid solution.

Read the sections explaining CSS Variables and the Comparison table. Focus on the section Best Practice: The Hybrid Approach. This is the most important takeaway. It shows how to define your tokens as CSS variables and reference them in your Tailwind config. Finally, review the Summary for a concise decision-making framework.

The Hybrid Architecture:

  1. Define your semantic tokens as CSS variables in your global stylesheet (e.g., globals.css). You can define different values for different themes (e.g., inside :root for light mode and [data-theme="dark"] for dark mode).
  2. Configure tailwind.config.js to use these CSS variables. Instead of hardcoding hex values, you'll reference the variables (e.g., colors: { primary: 'rgb(var(--primary))' }).
  3. Build your UI with Tailwind classes. Now, classes like bg-primary are powered by your dynamic CSS variables.

This architecture gives you the best of all worlds:

  • The exceptional developer experience and build-time performance of Tailwind.
  • The powerful, native runtime dynamism of CSS Variables for theming, which avoids the pitfalls of runtime CSS-in-JS.

Trade-Off Analysis

This table provides a high-level summary of which approach is best suited for different scenarios. It reinforces the idea that there is no single "best" solution, only the most appropriate one for a given set of requirements.

This table compares Tailwind CSS, CSS Modules, and various forms of CSS-in-JS, recommending the best choice for scenarios like fast prototyping, component-specific styles, design systems, dynamic styles, and performance-critical pages.

Conclusion

Choosing a styling architecture is a critical decision in frontend system design. While utility-first frameworks like Tailwind offer incredible velocity and compile-time performance, they lack native support for runtime theming. Runtime CSS-in-JS provides ultimate dynamic power but at a significant performance cost that is often unacceptable at scale.

Key Takeaways:

  • Runtime vs. Compile-time is the critical distinction. For performance-sensitive applications, always prefer styling solutions that do their work at build time, not in the user's browser.
  • CSS Variables are the foundation of modern theming. Their native, performant, and dynamic nature makes them the ideal tool for handling light/dark modes, multi-brand support, and other user-configurable UI.
  • The Hybrid Approach is the senior-level play. Combining Tailwind's utility-first DX with the runtime power of CSS Variables provides a scalable, performant, and flexible architecture that meets the demands of most large-scale applications.

In our next lesson, we will put this knowledge into practice. We'll design a complete, scalable theming system that supports light/dark modes, multiple brands, and RTL layouts, using the token and styling architecture we've established.