The Basics
Motivation
styled-components is the result of wondering how we could enhance CSS for styling React component systems. By focusing on a single use case we managed to optimize the experience for developers as well as the output for end users.
Apart from the improved experience for developers, styled-components provides:
- Automatic critical CSS: styled-components keeps track of which components are rendered on a page and injects their styles and nothing else, fully automatically. Combined with code splitting, this means your users load the least amount of code necessary.
- No class name bugs: styled-components generates unique class names for your styles. You never have to worry about duplication, overlap or misspellings.
- Easier deletion of CSS: it can be hard to know whether a class name is used somewhere in your codebase. styled-components makes it obvious, as every bit of styling is tied to a specific component. If the component is unused (which tooling can detect) and gets deleted, all its styles get deleted with it.
- Simple dynamic styling: adapting the styling of a component based on its props or a global theme is simple and intuitive without having to manually manage dozens of classes.
- Painless maintenance: you never have to hunt across different files to find the styling affecting your component, so maintenance is a piece of cake no matter how big your codebase is.
- Vendor prefixing on demand: enable legacy browser support via
StyleSheetManagerwhen needed.
You get all of these benefits while still writing the CSS you know and love, just bound to individual components.
Installation
Installing styled-components only takes a single command and you're ready to roll:
# with npm npm install styled-components # with yarn yarn add styled-components
If you use a package manager like yarn that supports the "resolutions" package.json field, we also highly recommend you add an entry to it as well corresponding to the major version range. This helps avoid an entire class of problems that arise from multiple versions of styled-components being installed in your project.
In package.json:
{ "resolutions": { "styled-components": "^6" } }
It's highly recommended (but not required) to also use the Babel plugin or SWC plugin. They offer many benefits like more legible class names, server-side rendering compatibility, smaller bundles, and more.
Click here to see alternative CDN installation instructions
If you're not using a module bundler or package manager we also have a global ("UMD") build hosted on the unpkg CDN. Simply add the following <script> tag to the bottom of your HTML file:
<script src="https://unpkg.com/styled-components/dist/styled-components.min.js"></script>
Once you've added styled-components you will have access to the global window.styled variable.
const Component = window.styled.div` color: red; `
This style of usage requires the React CDN bundles (react and react-dom) to be on the page as well (before the styled-components script.) See React's CDN documentation for details.
Getting Started
styled-components utilises tagged template literals to style your components.
It removes the mapping between components and styles. This means that when you're defining your styles, you're actually creating a normal React component, that has your styles attached to it.
This example creates two simple components, a wrapper and a title, with some styles attached to it:
Hello World!
This is a live editor, so play around with the code to get a feel for what it's like to work with styled-components!
Vendor prefixing is available but disabled by default in v6+. Enable it via StyleSheetManager if needed for legacy browser support.
Adapting based on props
You can pass a function ("interpolations") to a styled component's template literal to adapt it based on its props.
This button component has a primary state that changes its color. When setting the $primary prop to true, we are swapping out its background and text color.
React Server Components: Each unique interpolation result produces a separate CSS rule in the SSR'd HTML. For discrete variants, prefer data attributes (e.g., &[data-primary="true"]) with static styles so every instance shares one cached rule. See the RSC section for best practices.
Extending Styles
Quite frequently you might want to use a component, but change it slightly for a single case. Now, you could pass in an interpolated function and change them based on some props, but that's quite a lot of effort for overriding the styles once.