All You Need To Know About CSS-in-JS

What CSS-in-JS is, how styled-components scopes styles to a component, and the trade-offs. Written in 2017, with a 2026 note on zero-runtime options.

This article is outdated

The tools or APIs discussed here have changed significantly since this was written. The article is preserved for reference, but the code examples may no longer work as shown.

Update (2026): This article was written in 2017. The CSS-in-JS landscape has shifted significantly since then. styled-components is now in maintenance mode, and the industry has largely moved toward zero-runtime solutions like Tailwind CSS, CSS Modules, and vanilla-extract. Runtime CSS-in-JS libraries have well-documented performance issues with SSR and hydration. Native CSS features like nesting, :has(), and container queries have also reduced the need for CSS-in-JS.

CSS-in-JS brings CSS to the component level, moving away from the traditional document-level styling approach.

You’ve likely come across terms like CSS-in-JS, Styled Components, Radium, and Aphrodite.

You might be wondering, “Why should I care? I’m perfectly fine with CSS in traditional .css files.”

CSS-in-JS is a topic that often sparks debate. I encourage you to keep an open mind and consider if it might enhance your workflow. Ultimately, the goal is to use tools that make you more efficient and content in your work.

Maintaining a large collection of stylesheets has always felt cumbersome to me, so I’m always looking for new approaches.

Many developers are curious about alternative styling methods. CSS-in-JS has emerged as one of the most promising concepts. Let’s explore why it might be worth trying.

What is CSS-in-JS?

import styled from 'styled-components'
// Create a Title component that'll render an <h1> tag with some styles
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: #BF4F74;
`;
// Create a Wrapper component that'll render a <section> tag with some styles
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`;
// Use Title and Wrapper like any other React component – except they're styled!
render(
<Wrapper>
<Title>
Hello World!
</Title>
</Wrapper>
);

CSS-in-JS is a powerful abstraction over traditional CSS. It allows you to describe styles using JavaScript in a declarative and maintainable way. This approach leverages JavaScript’s capabilities to manage CSS at runtime and even server-side. It’s framework-agnostic, lightweight (around 6KB minified and gzipped), and extensible through a plugin API.

It’s important to note that inline styles and CSS-in-JS are not the same. Here’s a demonstration to clarify the difference.

The Difference

Not all CSS features can be replaced by JavaScript event handlers. For example, pseudo-selectors like :disabled, :before, and :nth-child aren’t possible with inline styles, and styling html and body tags is not supported.

However, with CSS-in-JS, you can use the full range of CSS features. Since it generates actual CSS, you can use any media query or pseudo-selector you need. Some libraries, like jss and styled-components, even add support for non-native features like nesting!

Why Not Just Stick With CSS?

Traditionally, we’ve used CSS at the document level, but modern web development revolves around components, not pages. CSS was not originally designed for a component-based approach. CSS-in-JS addresses this gap. A shout-out to Vue for handling this well, although Vue’s styles don’t have access to component state.

What Are the Benefits of Using CSS-in-JS?

  • Component-Based Thinking: Manage styles at the component level, eliminating the need to maintain numerous stylesheets.
  • Leverages JavaScript’s Power: Enhance CSS by tapping into JavaScript’s ecosystem.
  • True Isolation of Rules: CSS-in-JS ensures that styles don’t unintentionally inherit properties from parent elements, thanks to tools like the jss-isolate plugin.
  • Scoped Selectors: CSS-in-JS prevents selector collisions by generating unique class names, unlike traditional CSS, which has a global namespace.
  • Automatic Vendor Prefixing: No need to worry about vendor prefixes; they’re handled for you.
  • Code Sharing: Easily share constants and functions between your JavaScript and CSS.
  • Efficient DOM Management: Only the styles currently in use are rendered in the DOM, reducing clutter.
  • Dead Code Elimination: Unused styles are removed automatically.
  • CSS Unit Testing: You can write unit tests for your styles.

What Are the Drawbacks of Using CSS-in-JS?

  • Learning Curve: It takes time to learn this new approach.
  • Additional Dependencies: You’ll need to manage new libraries in your project.
  • Onboarding Challenges: New team members might take longer to adapt to a codebase using CSS-in-JS.
  • Challenging the Status Quo: Introducing CSS-in-JS might disrupt established workflows, though this isn’t necessarily a bad thing.

Despite these challenges, the advantages of CSS-in-JS far outweigh the drawbacks. It’s worth giving it a try!

npm trends chart over six months: radium, glamorous and styled-components lead CSS-in-JS downloads, ahead of jss

All of these libraries offer extensive functionality, including theming, dynamic props, server-side rendering, and more.


Trevor I. Lasn

Trevor I. Lasn

Building whiteheap.com and 0xinsider.com.



Related Articles

Check out these related articles that might be useful for you. They cover similar topics and provide additional insights.

CSS
8 min read

Create animated transitions between different states of your app without complex animation libraries.

Nov 11, 2025
Read article
CSS
4 min read

Style connected UI with the :interest-source and :interest-target pseudo-classes. Interactive examples of tooltips, forms and navigation without JavaScript.

Nov 12, 2025
Read article
CSS
3 min read

Transform your CSS with :has(), the game-changing selector that finally lets us style elements based on their children.

Dec 4, 2024
Read article
CSS
3 min read

Use CSS @supports to check whether a browser supports a feature such as grid, and give other browsers a fallback. How it works, with multiple conditions.

Dec 6, 2024
Read article
CSS
3 min read

content-visibility: auto lets the browser skip layout and paint for offscreen elements until they are needed. How to use it with contain-intrinsic-size.

Dec 5, 2024
Read article
CSS
3 min read

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

Dec 17, 2024
Read article
CSS
7 min read

The CSSOM, or CSS Object Model, is the browser's map of every style rule. How it is built beside the DOM, how it computes styles, and how to keep it fast.

Oct 25, 2024
Read article
CSS
3 min read

The ::details-content pseudo-element lets you style the expandable content of details elements separately from the summary, no divs needed.

Nov 11, 2025
Read article
CSS
3 min read

Finally, we can center things in block layouts without flexbox gymnastics

Dec 13, 2024
Read article

This article was originally published on https://www.trevorlasn.com/blog/all-you-need-to-know-about-css-in-js. It was written by a human and polished using grammar tools for clarity.