Skip to content
IT glossary
FrontendUpdated December 3, 2026

CSS-in-JS

This approach defines web styles in JavaScript or TypeScript, often next to the components they style. Styles can change based on a component’s inputs.

Also known as: styled-components

What it means

A button might come in several variants: primary, secondary, and destructive. With CSS-in-JS, you can define the styling for those variants in a JavaScript or TypeScript module and tie the choice to the button’s props. The browser still applies CSS. styled-components is one React library that implements the approach, not another name for the whole category.

When the CSS is generated

Most CSS-in-JS tools give rules generated class names and apply the matching classes to HTML elements. This reduces accidental naming collisions, but it does not create an isolation boundary like Shadow DOM. Global selectors can still match the elements, and inheritance, specificity, and the cascade still apply.

With a runtime library, JavaScript can evaluate style expressions and insert the resulting rules while the app runs. A build-time tool instead extracts rules it can determine ahead of time into CSS files. Build-time extraction does not mean the interface must look the same for every user: the component can select a prebuilt class or set a CSS custom property to a value it only learns at runtime.

Example: a button variant

import styled from 'styled-components';

const Button = styled.button`
  background: ${({ $primary }) => ($primary ? '#1d4ed8' : '#374151')};
  color: white;
  padding: 8px 12px;

  &:hover {
    text-decoration: underline;
  }
`;

export function SaveButton() {
  return <Button $primary>Save</Button>;
}

The library uses $primary to select a background color. The $ prefix makes it a transient prop: it is available to the styling code but is not forwarded as a DOM attribute. The browser handles the hover rule directly. A React style object, by contrast, cannot express a :hover selector on its own.

Where it fits and what to watch for

CSS-in-JS is useful in a design system where components share a theme and expose a small set of named variants. If you mainly need local class names and your rules are largely static, CSS Modules can offer that without runtime rule generation.

Define styled React components outside render functions. Otherwise, every render creates a new component type, which can cause React to remount it and discard local state. Also distinguish a handful of variants from frequently changing values. For something like a drag position, updating a custom property can be a better fit than generating a class for each distinct position.

In interviews

Be ready to compare CSS-in-JS with inline styles and CSS Modules, then explain why you would pick one for a given project. For a server-rendered app, describe how the library makes the initial styles available with the server HTML and how the client reuses them during hydration. This can prevent a flash of unstyled content or duplicate style insertion; the exact setup depends on the library and framework.