Course contentsShow
← React
Lesson 1931 of 2,39644. Styling ApproachesFree lesson

Performance Tradeoffs: Runtime Style Injection

CSS-in-JS injects styles at runtime in the client: bundle size, parsing cost, and why zero-runtime solutions emerged.

Performance Tradeoffs: Runtime Style Injection

You'll learn why CSS-in-JS libraries like styled-components and Emotion inject styles at runtime in the browser, and what costs that imposes.


The Runtime Injection Model

CSS-in-JS libraries generate and inject <style> tags into the document at runtime—in the browser, after your JavaScript loads. When your component renders, the library:

  1. Parses the tagged template literal or css call
  2. Generates unique class names
  3. Serializes the CSS rules
  4. Injects them into the DOM

This happens every time your app loads, even if the styles never change.


What This Costs You

Bundle size: The entire CSS-in-JS library ships to every user (~15-30 KB for styled-components, ~8-15 KB for Emotion).

Parsing cost: JavaScript must parse your style definitions at runtime. Template literals aren't free—they're code.

Injection overhead: Creating and inserting <style> tags blocks the main thread. On slower devices, this delays first paint and interactivity.

No static optimization: Because styles are in JavaScript, build tools can't extract, deduplicate, or preload them as separate .css files.


Why Zero-Runtime Solutions Emerged

Libraries like Linaria, vanilla-extract, and Panda CSS shift style extraction to build time. They parse your CSS-in-JS syntax during bundling, generate static .css files, and replace your style code with simple class name imports.

Result: no runtime overhead, smaller bundles, instant styles.


Key Takeaway: Runtime CSS-in-JS trades bundle size, parsing cost, and injection overhead for developer experience and dynamic styling flexibility. Zero-runtime alternatives eliminate these costs by moving work to the build step.