CSS-in-JS

CSS-in-JS authors styles in JavaScript or TypeScript, usually right next to the component they style. The appeal is co-location, scoping without naming conventions, and styles that respond to props and themes as plain code.

The catch is cost: many popular libraries do styling work at runtime in the browser, which adds overhead and doesn't play well with React Server Components. That's pushed the ecosystem toward zero-runtime options that extract real CSS at build time.

TL;DR

Quick Example

A styled component whose styles react to props (Emotion / styled-components style):

Core Concepts

Runtime vs Zero-Runtime

This is the distinction that matters most today:

Runtime CSS-in-JS has fallen out of favor for performance-sensitive and server-component apps; new projects increasingly choose zero-runtime CSS-in-JS or utility-first Tailwind.

Best Practices

Common Mistakes

Creating styled components inside render

Ignoring runtime cost at scale

FAQ

Is CSS-in-JS dead?

No, but runtime CSS-in-JS has lost ground. Its browser overhead and incompatibility with React Server Components pushed the ecosystem toward zero-runtime libraries (vanilla-extract, Linaria, Panda) and utility-first Tailwind. The co-location idea lives on, mostly without the runtime cost.

What's the difference between runtime and zero-runtime?

Runtime libraries generate and inject styles in the browser as components render. Zero-runtime libraries extract static CSS files at build time, so the browser just loads plain CSS — faster and compatible with server components.

Does CSS-in-JS work with React Server Components?

Runtime libraries generally don't, because they rely on client-side context and render-time style injection. Zero-runtime libraries do, since they produce static CSS at build time. Pick a zero-runtime option for RSC apps.

CSS-in-JS or Tailwind or CSS Modules?

CSS-in-JS for heavily dynamic, prop-driven styling. Tailwind for fast, consistent utility-first styling. CSS Modules for plain CSS with local scoping and zero runtime. Many teams now combine Tailwind or CSS Modules with a zero-runtime extractor.

Related Topics

References