# CSS-in-JS

> Style components with JavaScript (styled-components, Emotion) or zero-runtime tools (vanilla-extract, Panda); know the trade-offs. Free notes, key concepts,...

Canonical: https://www.internhack.in/roadmaps/tracks/frontend/css-in-js
Last updated: 2026-11-28

Style components with JavaScript (styled-components, Emotion) or zero-runtime tools (vanilla-extract, Panda); know the trade-offs.

Intermediate · about 3 hours · difficulty 3/5

Learn first: [Custom Properties and Modern CSS Features](https://www.internhack.in/roadmaps/tracks/frontend/modern-css), [Build Tools and Bundlers](https://www.internhack.in/roadmaps/tracks/frontend/build-tools)

CSS-in-JS lets you write styles inside JavaScript or TypeScript files, usually colocated with the component and able to use props directly. It became popular with React and powers libraries like MUI and Chakra.

### Two families

- **Runtime libraries** such as **styled-components** and **Emotion** generate and inject CSS in the browser as components render. They are flexible (`color: ${p => p.variant === "danger" ? "red" : "black"}`) but add JavaScript cost and do not fit well with React Server Components or streaming SSR.
- **Zero-runtime libraries** such as **vanilla-extract**, **Panda CSS**, **Linaria** and **StyleX** extract static CSS at build time. You keep type-safe, colocated styles without shipping a style engine to users.

### Strengths

- Real scoping with no naming conventions.
- Type-safe themes and variants, and styles that can read component props.
- Easy deletion: styles go when the component goes.

### Trade-offs

- Runtime libraries cost performance (serialising styles during render) and complicate SSR setup.
- Heavier tooling and a steeper debugging story than plain CSS.
- Much of the industry's new work has moved to Tailwind or zero-runtime options for these reasons.

### When to choose it

Pick it when you join a codebase or component library that already uses it, or when you need strongly typed variant APIs and choose a zero-runtime tool. For a new app, Tailwind or CSS Modules is usually simpler.

### Good enough to move on

You can build a themed, variant-driven Button with one CSS-in-JS library and explain why runtime CSS-in-JS conflicts with Server Components.

## Key concepts

- **Runtime vs zero-runtime**
- **styled-components and Emotion**
- **vanilla-extract, Panda CSS, StyleX**
- **Theming and typed variants**
- **SSR and Server Components constraints**
- **Performance cost of runtime styles**

## Other options for this step

- [Tailwind CSS](https://www.internhack.in/roadmaps/tracks/frontend/tailwind): Utility-first styling that keeps design decisions consistent and colocated with markup; our recommended default for new projects.
- [CSS Modules](https://www.internhack.in/roadmaps/tracks/frontend/css-modules): Write normal CSS files whose class names are scoped to one component automatically, with zero runtime cost.
- [Sass and Preprocessors](https://www.internhack.in/roadmaps/tracks/frontend/sass): Variables, nesting, mixins and partials compiled to CSS; still common in existing codebases even though native CSS has caught up.

## Mini project

Implement the same Button component (sizes, variants, disabled state, theme) twice: once with a runtime library and once with a zero-runtime library, then compare bundle size and SSR output.

## Check yourself

What work does a runtime CSS-in-JS library do on every render that a zero-runtime one moves to build time?

## Free resources

- [styled-components documentation](https://styled-components.com/docs) (styled-components)
- [vanilla-extract](https://vanilla-extract.style/) (vanilla-extract)
- [Panda CSS](https://panda-css.com/) (Panda CSS)

Previous: [Sass and Preprocessors](https://www.internhack.in/roadmaps/tracks/frontend/sass) · Next: [TypeScript Fundamentals](https://www.internhack.in/roadmaps/tracks/frontend/typescript-basics)

## More in Styling at Scale

- [Tailwind CSS](https://www.internhack.in/roadmaps/tracks/frontend/tailwind)
- [CSS Modules](https://www.internhack.in/roadmaps/tracks/frontend/css-modules)
- [Sass and Preprocessors](https://www.internhack.in/roadmaps/tracks/frontend/sass)
- [CSS-in-JS](https://www.internhack.in/roadmaps/tracks/frontend/css-in-js)

## Related

- [Roadmaps](https://www.internhack.in/roadmaps)
- [Frontend Developer Roadmap](https://www.internhack.in/roadmaps/tracks/frontend)
