Full answer
CSS-in-JS means writing styles in JavaScript files, next to the component that uses them. Libraries differ, but they fall into two groups.
Runtime libraries create the CSS in the browser. The component runs, the library turns its style object or template into CSS text, makes a unique class name, and inserts a style rule into the page. Styles can use props and state directly: color: ${props.color}.
Build-time (zero-runtime) tools extract the CSS during the build and ship normal CSS files. The class names are generated, and dynamic values are passed with custom properties, such as style="--color: red".
Benefits:
- Styles live with the component, are removed with it, and cannot clash, because class names are unique.
- Easy theming and dynamic values with JavaScript logic.
- Typed styles and tooling in TypeScript projects.
Costs of the runtime kind:
- Extra JavaScript to download and run, and work on every render: hashing, string building and rule insertion. This adds to the main thread time, especially with many dynamic styles.
- Server rendering needs extra setup to collect the styles and send them with the HTML, or the page flashes without styles.
- Many unique classes and rules from changing props can grow the style sheet and cause style recalculation.
- Style insertion order can cause specificity surprises, and the generated names make debugging and global overrides harder.
- It is harder to cache and to share one CSS file across pages and teams.
Build-time tools remove most of the runtime cost, but dynamic styles must be expressed through custom properties or a limited set of variants.
How to choose: consider the framework, the server rendering needs, the team's habits and performance budgets. Plain CSS with modules, custom properties and layers is a strong baseline that needs no runtime. Measure before deciding, and do not mix three methods in one codebase.
This tiny runtime caches by the rule text. The same style gives the same class and adds no new rule, while a different value adds one. Styles are applied through classes, and the rule count shows what a runtime has to grow.
A real library adds hashing, vendor handling and server rendering, which is where most of the cost and complexity come from.
<script>
const sheet = new CSSStyleSheet();
document.adoptedStyleSheets = [sheet];
const cache = new Map();
window.css = rules => {
if (!cache.has(rules)) {
const name = "c" + cache.size;
sheet.insertRule("." + name + " { " + rules + " }", sheet.cssRules.length);
cache.set(rules, name);
}
return cache.get(rules);
};
const a = css("color: rgb(10, 20, 30); padding: 4px");
const b = css("color: rgb(10, 20, 30); padding: 4px");
const c = css("color: rgb(200, 0, 0); padding: 4px");
document.body.insertAdjacentHTML("beforeend", '<p id="p1" class="' + a + '">One</p><p id="p2" class="' + c + '">Two</p>');
window.names = [a, b, c];
window.ruleCount = sheet.cssRules.length;
</script>The library code is written by hand for the example. No CSS-in-JS package was installed or timed. Run in Chrome 156 (headless) as a page; the snippet is wrapped in a minimal HTML document with doctype, charset and viewport meta tag. Two full runs gave identical results, and the measured values in the answer come from them.
Key points
- Runtime libraries generate and insert rules in the browser.
- Build-time tools ship extracted CSS.
- Benefits: scoping, colocation and dynamic values.
- Costs: JavaScript, server rendering and many rules.
- Plain CSS with modules and custom properties is a strong baseline.
Common mistakes
- Choosing a runtime library and ignoring server rendering and main thread cost.
- Generating a new class for every prop value, which grows the style sheet.
- Mixing several styling methods in one codebase.
Follow-ups
- How do build-time tools handle dynamic values?
- Why does server rendering need extra work with runtime CSS-in-JS?
Tips
- Separate runtime and build-time libraries clearly.
- List benefits and costs, and end with how you would choose.