skip to content

In CSS-in-JS, what is the difference between a runtime library that injects styles and a zero-runtime library that extracts them at build time?

level: juniorimportance: must knowfreq 30%

answer

  1. when does the CSS come to exist
  2. an engine running in the page
  3. a static stylesheet from the bundler
  4. values only known while the page runs
  5. a custom property or a fixed variant

basics

~20 s

A runtime library generates CSS in the browser as components render and injects it into style elements; a zero-runtime library evaluates the styles during the build and ships a static stylesheet plus class names, with no styling engine in the page.

solid answer

~50 s

Both let you write styles in JavaScript or TypeScript next to the component; they differ in *when* the CSS comes into existence. A **runtime** library (styled-components or Emotion, for example) ships its engine to the browser: as a component renders, it turns the style template or object, with the current props and theme, into CSS, hashes it into a class name and inserts the rule into a `<style>` element if that class is new. A **zero-runtime** library (vanilla-extract, for example) runs your style files during the build and emits ordinary `.css` files plus the generated class names, so the browser only downloads a stylesheet. The price of extraction is that a value known only while the page runs cannot be precompiled: it becomes a CSS custom property set at runtime, or one of a fixed set of variants written in advance.

go deeper

for a junior

Recall the one axis that matters: when the CSS is produced. Runtime libraries generate and inject it in the page; zero-runtime libraries produce a static stylesheet during the build.

for a middle

Explain what each family does with a prop-driven value: a runtime library interpolates it into a new rule, while extraction needs a custom property set at runtime or a predefined variant.

for a senior

Show where the difference bites in production: runtime injection adds work while rendering, needs server-side style collection, and meets content security policies, while extraction removes all three.

for a principal

Frame the choice as flexibility against a static stylesheet, and decide which dynamic values a codebase truly needs before committing a design system to either family.

## What "CSS-in-JS" covers **CSS-in-JS** is any approach where you author styles in JavaScript or TypeScript modules, usually next to the component that uses them, instead of in separate `.css` files. Under that single name sit two families that behave very differently once the code ships: - **Runtime injection** — a library in the page generates CSS while components render and writes it into the document. - **Build-time extraction** (often called *zero-runtime*) — a compiler or bundler plugin evaluates the style definitions during the build and writes static CSS files. React's own documentation lists three common approaches: static extraction to CSS files with a compiler, inline styles, and runtime injection of `<style>` tags. It recommends a combination of the first two and advises against runtime injection. ## Runtime injection Take a concert-ticketing site whose event cards are written with a runtime library such as styled-components or Emotion. When an event card renders, the library: 1. evaluates the style template or object, including any interpolation that reads props or the theme; 2. serialises the result into a CSS string; 3. hashes that string into a class name, so identical styles share one class; 4. inserts the rule into a `<style>` element if that class has not been inserted before; 5. hands the class name to the element. Everything above happens in the browser (and again on the server if the page is server-rendered). The library's code is part of the JavaScript bundle, and the stylesheet is built up rule by rule as new styles appear. ## Build-time extraction A zero-runtime library moves the same work to the build. In vanilla-extract, for example, styles live in `.css.ts` files; the bundler integration evaluates them during the build, emits static CSS, and the module's exports become plain class-name strings. Its README describes the result as "Zero-runtime Stylesheets-in-TypeScript", with all styles generated at build time. The browser receives a normal stylesheet, which it downloads, parses and caches like any other. ## Side by side | Question | Runtime injection | Build-time extraction | |---|---|---| | When is the CSS produced? | While components render | During the build | | What reaches the browser? | A styling engine plus rules created on the fly | A `.css` file plus class-name strings | | Can a style use a prop's value directly? | Yes, interpolated on render | Only through a custom property or a known variant | | Extra work during rendering | Serialising, hashing, inserting new rules | None for styling | | What server rendering needs | Collecting the rules each render produced | Nothing beyond linking the stylesheet | Both families usually generate **scoped class names**, so the protection against two components' `.title` rules colliding is the same. What differs is where CSS generation happens: in every visitor's browser on every page load, or once on the build machine. ## The value you cannot extract Extraction has one firm limit: the compiler can only write down what it can compute **before the page runs**. A promoter's brand colour fetched from an API, a seat's live demand level or a dragged element's position does not exist at build time. Such a value takes one of two forms: - a **CSS custom property**: the static rule reads `var(--promoter-accent)`, and the component sets the property's value on the element at runtime; - a **known variant**: when the value comes from a small fixed set (sold, held, available), every variant is written as its own rule in advance and the component picks one. This is also why "zero-runtime" does not always mean zero JavaScript. vanilla-extract's documentation, for instance, offers a small `@vanilla-extract/dynamic` package that it describes as a tiny runtime for assigning scoped variables inline, and its Sprinkles package keeps a small runtime that only looks up pre-generated class names. What disappears is *generating and injecting CSS* in the page, not every line of styling code. ## Choosing between them - A runtime library is flexible: any expression can drive any declaration, and nothing is configured in the build. - Extraction trades that flexibility for a static stylesheet: no engine in the bundle, no rule insertion while rendering, and nothing to collect on the server. - Teams that start with runtime libraries often feel the difference first on server rendering, server components and strict content security policies, each of which is a separate question. - A common middle path is to keep a runtime library on existing screens while new shared components are written for extraction, with dynamic values already expressed as custom properties so either family can consume them.

  • Does "zero-runtime" mean no styling JavaScript reaches the browser at all?
    Not quite. The stylesheet is static, but components still import the generated class names as strings, and some extraction libraries ship a small helper: vanilla-extract's docs describe `@vanilla-extract/dynamic` as a tiny runtime that assigns scoped variables inline. What disappears is generating and injecting CSS while the page runs.
  • Where do inline styles fit next to these two families?
    React's documentation lists three approaches: static extraction to CSS files, inline styles, and runtime injection of `<style>` tags. It recommends combining the first two, static CSS for static styles and inline styles for dynamic values, and advises against runtime injection because it makes the browser recalculate styles more often.

Runtime injection is a print shop that typesets each flyer when a customer asks for it; extraction prints the whole run before the shop opens, so anything only known at the counter must be left as a blank to fill in by hand.

  • CSS-in-JS always means styles are injected into the page at runtime
  • A zero-runtime library can extract values computed from props at render time
  • Runtime and build-time libraries emit the same output, only the syntax differs
  • Zero-runtime means no styling-related JavaScript at all ever reaches the browser
  • Build-time extraction still needs a style collector during server rendering