Why Do We Use CSS-in-JS?

If you’re a frontend developer using React, you’ve probably used styled-components quite a bit. I’ve used it alongside React too, and recently I’ve been using Emotion more often because of its better performance and simpler handling of props.
CSS-in-JS illustration

I’ve been taking it for granted, so let’s revisit why we use CSS-in-JS.

How CSS-in-JS Emerged

  1. The rise of component-based architecture:
    • Frameworks such as React, Vue, and Angular encouraged component-based development.
    • Traditional CSS struggled to fully support component independence and reuse.
  2. The global namespace problem:
    • Traditional CSS uses a global namespace, making style conflicts common.
    • Large applications required complex naming conventions to avoid collisions.
  3. The need for dynamic styling:
    • As web applications became more dynamic and interactive, the need to change styles dynamically at runtime grew.
    • Changing styles based on JavaScript state was cumbersome with traditional CSS.
  4. Performance optimization:
    • Removing unused CSS from large applications was difficult.
    • There was demand to reduce initial loading times by loading only the styles needed.
  5. Better maintainability:
    • Managing CSS and JavaScript files separately added complexity to large projects.
    • Developers wanted to keep all component-related code (HTML, JS, and CSS) together.
  6. Limitations of CSS preprocessors:
    • Preprocessors such as Sass and Less solved many problems, but still couldn’t fully address dynamic styling and component isolation.
  7. Drawing on JavaScript’s capabilities:
    • Developers wanted to use JavaScript’s rich features and ecosystem for styling too.
    • Conditions, loops, and functions could be used directly in styles.
  8. Type safety:
    • As TypeScript became more popular, demand grew for type checking in CSS as well.

CSS-in-JS emerged as an attempt to solve these problems. Since Facebook’s Christopher Chedeau (vjeux) introduced the concept in his 2014 talk “React: CSS in JS,” a variety of CSS-in-JS libraries have been developed.

However, not every library satisfies every requirement. Some of these things are also possible with traditional CSS.

Because I use a component-based architecture, I find CSS-in-JS more readable and efficient than traditional CSS.

Drawbacks of CSS-in-JS

While researching its origins, I came across an article called “Why We’re Breaking Up with CSS-in-JS.” https://junghan92.medium.com/%EB%B2%88%EC%97%AD-%EC%9A%B0%EB%A6%AC%EA%B0%80-css-in-js%EC%99%80-%ED%97%A4%EC%96%B4%EC%A7%80%EB%8A%94-%EC%9D%B4%EC%9C%A0-a2e726d6ace6

These are some of its shortcomings.

  1. CSS-in-JS adds runtime overhead. When a component renders, the library has to serialize styles into plain CSS that can be inserted into the document. This takes extra CPU time.
  2. CSS-in-JS increases bundle size. Visitors need to download JavaScript for the CSS-in-JS library. Emotion is 7.9 kB compressed, and styled-components is 12.7 kB.
  3. There are many more things that can go wrong with CSS-in-JS, especially with SSR and/or component libraries. The Emotion GitHub repository receives numerous issue reports of this kind.

A newer library emerged to address these issues: Vanilla Extract. Since runtime CSS causes significant issues with SSR, compile-time CSS-in-JS libraries appeared. We need to compare the strengths and weaknesses of these libraries and choose one that suits the project.


References https://ttaerrim.tistory.com/64 https://shiwoo.dev/posts/next-13-and-css-in-js