styled-components
Visual primitives for the component age. Use the best bits of ES6 and CSS to style your apps without stress 💅
Top Related Projects
👩🎤 CSS-in-JS library designed for high performance style composition
Documentation about css-modules
A utility-first CSS framework for rapid UI development.
[Not Actively Maintained] CSS-in-JS with near-zero runtime, SSR, multi-variant support, and a best-in-class developer experience.
Zero-runtime Stylesheets-in-TypeScript
Quick Overview
Styled-components is a popular CSS-in-JS library for React applications. It allows developers to write actual CSS code to style their components, combining the benefits of CSS with the power of JavaScript. This library enhances the component-based architecture of React by tightly coupling styles with components.
Pros
- Automatic critical CSS: Only the styles used in the rendered components are injected, reducing load time
- No class name bugs: Styled-components generates unique class names, eliminating conflicts
- Easy dynamic styling: Styles can easily adapt based on props or a global theme
- Seamless integration with React: Styled components behave just like regular React components
Cons
- Learning curve: Developers need to adapt to a new way of writing styles
- Potential performance overhead: Runtime styling can impact performance in large applications
- Debugging challenges: Generated class names can make it harder to debug styles in browser dev tools
- Limited use outside React: Primarily designed for React, limiting its use in other frameworks
Code Examples
Creating a styled button:
import styled from 'styled-components';
const Button = styled.button`
background-color: #3498db;
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
cursor: pointer;
&:hover {
background-color: #2980b9;
}
`;
Using props to create dynamic styles:
const DynamicButton = styled.button`
background-color: ${props => props.primary ? '#3498db' : '#e74c3c'};
color: white;
padding: 10px 20px;
border: none;
border-radius: 4px;
`;
// Usage
<DynamicButton primary>Primary Button</DynamicButton>
<DynamicButton>Secondary Button</DynamicButton>
Extending styles:
const BaseButton = styled.button`
padding: 10px 20px;
border: none;
border-radius: 4px;
`;
const PrimaryButton = styled(BaseButton)`
background-color: #3498db;
color: white;
`;
Getting Started
-
Install styled-components:
npm install styled-components
-
Import and use in your React component:
import styled from 'styled-components'; import React from 'react'; const StyledDiv = styled.div` background-color: #f1f1f1; padding: 20px; border-radius: 8px; `; const MyComponent = () => ( <StyledDiv> <h1>Hello, styled-components!</h1> </StyledDiv> ); export default MyComponent;
-
Use the component in your app as you would any other React component.
Competitor Comparisons
👩🎤 CSS-in-JS library designed for high performance style composition
Pros of Emotion
- Smaller bundle size and better performance
- More flexible with object styles and composition
- Better TypeScript support out of the box
Cons of Emotion
- Less intuitive API for some developers
- Fewer third-party ecosystem tools and extensions
- Requires additional setup for server-side rendering
Code Comparison
Styled Components:
const Button = styled.button`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'blue'};
padding: 10px 20px;
`;
Emotion:
const Button = styled.button`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'blue'};
padding: 10px 20px;
`
Both libraries offer similar syntax for creating styled components, with minor differences in implementation. Emotion provides additional ways to define styles, such as using objects:
const Button = styled.button({
backgroundColor: props => props.primary ? 'blue' : 'white',
color: props => props.primary ? 'white' : 'blue',
padding: '10px 20px',
})
This object syntax can be more convenient for some developers and allows for easier composition of styles.
Documentation about css-modules
Pros of CSS Modules
- Simpler learning curve for developers familiar with traditional CSS
- Better performance due to static extraction of CSS at build time
- Easier integration with existing CSS and build systems
Cons of CSS Modules
- Less dynamic styling capabilities compared to styled-components
- Requires additional build configuration and tooling
- Limited runtime manipulation of styles
Code Comparison
CSS Modules:
/* styles.module.css */
.button {
background-color: blue;
color: white;
}
import styles from './styles.module.css';
const Button = () => <button className={styles.button}>Click me</button>;
styled-components:
import styled from 'styled-components';
const Button = styled.button`
background-color: blue;
color: white;
`;
const MyComponent = () => <Button>Click me</Button>;
Both CSS Modules and styled-components offer solutions for component-scoped CSS in React applications. CSS Modules provides a more traditional approach with separate CSS files and class name hashing, while styled-components offers a more integrated, JavaScript-centric solution with dynamic styling capabilities. The choice between them often depends on project requirements, team preferences, and existing codebase structure.
A utility-first CSS framework for rapid UI development.
Pros of Tailwind CSS
- Rapid development with pre-defined utility classes
- Consistent design system out of the box
- Smaller bundle size due to purging unused styles
Cons of Tailwind CSS
- Steeper learning curve for utility-first approach
- Can lead to cluttered HTML with many class names
- Less flexibility for custom designs without configuration
Code Comparison
Tailwind CSS:
<button class="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">
Button
</button>
styled-components:
const Button = styled.button`
background-color: #3b82f6;
color: white;
font-weight: bold;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
&:hover {
background-color: #1d4ed8;
}
`;
Tailwind CSS uses utility classes to style elements directly in HTML, while styled-components allows for writing CSS-in-JS with more flexibility and dynamic styling. Tailwind CSS excels in rapid prototyping and maintaining a consistent design system, whereas styled-components offers more customization and cleaner HTML structure. The choice between the two often depends on project requirements and team preferences.
[Not Actively Maintained] CSS-in-JS with near-zero runtime, SSR, multi-variant support, and a best-in-class developer experience.
Pros of Stitches
- Near-zero runtime, with styles generated at build time
- TypeScript-first approach with better type inference
- Atomic CSS-in-JS for improved performance and smaller bundle sizes
Cons of Stitches
- Steeper learning curve due to its unique API
- Less community support and ecosystem compared to Styled Components
- Limited dynamic styling capabilities
Code Comparison
Styled Components:
const Button = styled.button`
background-color: ${props => props.primary ? 'blue' : 'white'};
color: ${props => props.primary ? 'white' : 'blue'};
padding: 10px 20px;
`;
Stitches:
const Button = styled('button', {
backgroundColor: 'white',
color: 'blue',
padding: '10px 20px',
variants: {
primary: {
true: {
backgroundColor: 'blue',
color: 'white',
},
},
},
});
Both Styled Components and Stitches offer powerful CSS-in-JS solutions, but they cater to different needs. Styled Components provides a more familiar API and extensive ecosystem, while Stitches focuses on performance and type safety. The choice between them depends on project requirements and developer preferences.
Zero-runtime Stylesheets-in-TypeScript
Pros of vanilla-extract
- Type-safe CSS with TypeScript integration
- Zero-runtime CSS-in-JS for better performance
- Static extraction of CSS at build time
Cons of vanilla-extract
- Steeper learning curve for developers used to traditional CSS
- Less dynamic styling capabilities compared to runtime CSS-in-JS
- Requires a build step, which may complicate some workflows
Code Comparison
vanilla-extract:
import { style } from '@vanilla-extract/css';
export const button = style({
backgroundColor: 'blue',
color: 'white',
padding: '10px 20px',
});
styled-components:
import styled from 'styled-components';
const Button = styled.button`
background-color: blue;
color: white;
padding: 10px 20px;
`;
Both vanilla-extract and styled-components offer ways to create reusable, scoped CSS styles in JavaScript. vanilla-extract focuses on type safety and zero-runtime performance, while styled-components provides a more dynamic, runtime-based approach to styling. The choice between them depends on project requirements, team preferences, and performance considerations.
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual CopilotREADME
Upgrading from v5? See the migration guide.
Utilizing tagged template literals (a recent addition to JavaScript) and the power of CSS, styled-components
allow you to write actual CSS code to style your components. It also removes the mapping between components and styles â using components as a low-level styling construct could not be easier!
const Button = styled.button`
color: grey;
`;
Alternatively, you may use style objects. This allows for easy porting of CSS from inline styles, while still supporting the more advanced styled-components capabilities like component selectors and media queries.
const Button = styled.button({
color: 'grey',
});
Equivalent to:
const Button = styled.button`
color: grey;
`;
styled-components
is compatible with both React (for web) and React Native â meaning it's the perfect choice even for truly universal apps! See the documentation about React Native for more information.
Supported by Front End Center. Thank you for making this possible!
Docs
See the documentation at styled-components.com/docs for more information about using styled-components
!
Quicklinks to some of the most-visited pages:
Example
import React from 'react';
import styled from 'styled-components';
// Create a <Title> react component that renders an <h1> which is
// centered, palevioletred and sized at 1.5em
const Title = styled.h1`
font-size: 1.5em;
text-align: center;
color: palevioletred;
`;
// Create a <Wrapper> react component that renders a <section> with
// some padding and a papayawhip background
const Wrapper = styled.section`
padding: 4em;
background: papayawhip;
`;
function MyUI() {
return (
// Use them like any other React component â except they're styled!
<Wrapper>
<Title>Hello World, this is my first styled component!</Title>
</Wrapper>
);
}
This is what you'll see in your browser:
Looking for v5?
The main
branch is for the most-current version of styled-components, currently v6. For changes targeting v5, please point your PRs at the legacy-v5
branch.
Built with styled-components
A lot of hard work goes into community libraries, projects, and guides. A lot of them make it easier to get started or help you with your next project! There are also a whole lot of interesting apps and sites that people have built using styled-components.
Make sure to head over to awesome-styled-components to see them all! And please contribute and add your own work to the list so others can find it.
Contributing
If you want to contribute to styled-components
please see our contributing and community guidelines, they'll help you get set up locally and explain the whole process.
Please also note that all repositories under the styled-components
organization follow our Code of Conduct, make sure to review and follow it.
Badge
Let everyone know you're using styled-components â
[![style: styled-components](https://img.shields.io/badge/style-%F0%9F%92%85%20styled--components-orange.svg?colorB=daa357&colorA=db748e)](https://github.com/styled-components/styled-components)
Contributors
This project exists thanks to all the people who contribute. [Contribute].
Backers
Thank you to all our backers! ð [Become a backer]
Sponsors
Support this project by becoming a sponsor. Your logo will show up here with a link to your website. [Become a sponsor]
License
Licensed under the MIT License, Copyright © 2016-present Glen Maddern and Maximilian Stoiber.
See LICENSE for more information.
Acknowledgements
This project builds on a long line of earlier work by clever folks all around the world. We'd like to thank Charlie Somerville, Nik Graf, Sunil Pai, Michael Chan, Andrey Popp, Jed Watson & Andrey Sitnik who contributed ideas, code or inspiration.
Special thanks to @okonet for the fantastic logo.
Top Related Projects
👩🎤 CSS-in-JS library designed for high performance style composition
Documentation about css-modules
A utility-first CSS framework for rapid UI development.
[Not Actively Maintained] CSS-in-JS with near-zero runtime, SSR, multi-variant support, and a best-in-class developer experience.
Zero-runtime Stylesheets-in-TypeScript
Convert designs to code with AI
Introducing Visual Copilot: A new AI model to turn Figma designs to high quality code using your components.
Try Visual Copilot