talira

styled-components

Fast, expressive styling for React. Server components, client components, streaming SSR, React Native, all one API.

open sourceMITself-hostablefree

styled-components is a CSS-in-JS library for React: you write real CSS in tagged template literals (styled.button...``), and each one becomes a React component with its own generated, scoped class names, styles that change with its props, and theming through a ThemeProvider or, from v6.4, createTheme() with CSS variables. It is TypeScript-native since v6, supports streaming server-side rendering and, since v6.3, React Server Components without 'use client', and the same API styles React Native through styled-components/native. Version 7, in alpha, replaces the stylis CSS parser with its own and rewrites the native runtime toward one CSS authoring model across web, iOS, Android and Expo Web.

Problems it solves

  • keep each React component's styles next to its code instead of in a global stylesheet that leaks everywhere
  • change a component's styles from its props, like a primary or disabled button, without juggling class names
  • share a theme of colours and spacing across every component and switch it at runtime
  • write the same styling code for our React web app and our React Native mobile app
  • styles that work in React Server Components and streaming SSR without a client boundary
  • class name collisions keep breaking styles when two teams' components end up on the same page
  • write plain CSS syntax, with nesting and pseudo-classes, inside my TypeScript components
  • set a CSP nonce on injected styles so our content security policy doesn't block them

Features

  • CSS in tagged template literals that become React components with scoped class names
  • styles that adapt to props, and extending one styled component from another
  • ThemeProvider theming, and createTheme() for CSS variable themes in server and client components (v6.4)
  • React Server Components support with no 'use client' needed (v6.3)
  • streaming server-side rendering via renderToPipeableStream (v6.2)
  • createGlobalStyle for global styles, and .attrs() for default props
  • TypeScript-native types since v6
  • React Native support through styled-components/native
  • CSP nonce detection from StyleSheetManager, ServerStyleSheet or meta tags
  • llms.txt with version-by-version notes for coding agents

Limitations

  • Doesn't support Internet Explorer 11 in v6: the docs say to stay on v5 for it1

Capabilities

Takes pluginsplan not known

Alternatives

The same job

These do what styled-components does.

  1. Emotion CSS-in-JS library designed for high performance style composition
  2. StyleX The expressive, type-safe, composable, predictable and themeable styling system for ambitious interfaces
  3. goober A less than 1KB css-in-js solution
  4. vanilla-extract Zero-runtime Stylesheets-in-TypeScript
  5. Panda CSS Universal, type-safe, CSS-in-JS framework for design systems

Compare styled-components with the top 3