styled-components
styled-components is a CSS-in-JS library that enables writing CSS directly within JavaScript using tagged template literals, creating React components with styl…
Installation
npm install styled-components
yarn add styled-components
pnpm add styled-components
Import
import styled from 'styled-components';
Quick Example
import styled from 'styled-components';
const Button = styled.button<{ $primary?: boolean }>`
background: ${(p) => (p.$primary ? '#3b82f6' : '#6b7280')};
color: white;
padding: 8px 16px;
border-radius: 6px;
border: none;
cursor: pointer;
`;
function App() {
return <Button $primary>Click me</Button>;
}About styled-components
styled-components is a CSS-in-JS library that enables writing CSS directly within JavaScript using tagged template literals, creating React components with styles attached. Each styled component generates unique class names automatically, providing true style encapsulation without global CSS conflicts. The API is intuitive: const Button = styled.button`background: blue; color: white; padding: 8px 16px;` creates a React component with those styles. styled-components supports dynamic styling based on props (${props => props.primary ? 'blue' : 'gray'}), theming through a ThemeProvider context, global styles through createGlobalStyle, CSS animations via keyframes helper, and extending existing styled components. The library handles vendor prefixing automatically, supports server-side rendering with ServerStyleSheet for critical CSS extraction, and provides a Babel plugin for better debugging class names and smaller bundles. styled-components attaches styles to the DOM only when components are rendered, enabling code splitting of styles alongside component code. While the CSS-in-JS approach has faced criticism for runtime performance overhead compared to utility CSS (Tailwind) or CSS Modules, styled-components remains widely used for its developer experience and component-centric styling model.
Quick Facts
| Package | styled-components |
| Category | UI Component |
| Weekly Downloads | 5M+ |
| License | MIT |
| Install | npm install styled-components |
Related Packages
Emotion is a performant and flexible CSS-in-JS library that provides multiple APIs for styling React…
Tailwind CSS is a utility-first CSS framework that provides low-level utility classes for building c…
Sass (Syntactically Awesome Style Sheets) is the most mature and widely used CSS preprocessor, exten…
PostCSS is a tool for transforming CSS with JavaScript plugins, serving as the foundation for many m…
Browse npm Packages by Category
Explore our reference of 200 popular npm packages with install commands, examples, and quick-start guides.