Styled Components and CSS-in-JS
0 564
๐จ What Are Styled Components?
styled-components is a popular CSS-in-JS library that lets you write actual CSS code inside your JavaScript (React) files. It removes the separation between styling and logic by allowing you to style components directly. โ๏ธโจ
๐ก Why Use Styled Components?
Here are some key benefits:
- โ Scoped styles by default
- โ Dynamic styling based on props
- โ No class name collisions
- โ Cleaner, more readable components
โ๏ธ Installing Styled Components
npm install styled-components
or
yarn add styled-components
๐ฆ Basic Usage
Here's how you create a styled component:
import styled from 'styled-components';
const Button = styled.button`
background-color: #3498db;
color: white;
padding: 10px 16px;
border: none;
border-radius: 5px;
cursor: pointer;
&:hover {
background-color: #2980b9;
}
`;
function App() {
return <Button>Click Me</Button>;
}
๐ฏ Dynamic Styling with Props
You can make styles responsive to props like this:
const Button = styled.button`
background-color: ${props => props.primary ? '#2ecc71' : '#e74c3c'};
color: white;
`;
<Button primary>Submit</Button>
<Button>Cancel</Button>
๐ Theming with ThemeProvider
Using ThemeProvider from styled-components, you can manage consistent styles across your app:
import { ThemeProvider } from 'styled-components';
const theme = {
primary: '#1abc9c',
secondary: '#2c3e50',
};
const Container = styled.div`
background-color: ${props => props.theme.primary};
color: white;
`;
<ThemeProvider theme={theme}>
<Container>Styled with Theme!</Container>
</ThemeProvider>
๐ Nesting and Media Queries
You can nest styles and add media queries directly inside styled components:
const Card = styled.div`
padding: 20px;
border-radius: 10px;
background: white;
h2 {
margin-bottom: 10px;
}
@media (max-width: 600px) {
padding: 10px;
}
`;
๐ CSS-in-JS Alternatives
styled-components is just one way to write CSS-in-JS. Other popular libraries include:
- ๐จ Emotion
- ๐ฏ Stitches
- ๐งต Linaria
- ๐ฅ JSS (less common today)
๐ง Styled Components vs CSS Modules
| Feature | Styled Components | CSS Modules |
| Scoped Styles | โ Yes | โ Yes |
| Dynamic Styling | โ Built-in via props | ๐ซ Manual or external logic |
| Theme Support | โ Built-in with ThemeProvider | โ ๏ธ Requires context or manual setup |
| Tooling Support | โ Works well with JS tooling | โ Native CSS support |
| Code Separation | โ Mixed style and logic | โ Keeps CSS and JS separate |
| Learning Curve | โ ๏ธ Slightly steeper | โ Beginner-friendly |
๐ Summary
- โ Write CSS directly in JS with full control
- โ Styles are scoped, dynamic, and maintainable
- โ Easy to manage themes and global styles
- โ Great for building design systems and component libraries
๐ Final Thoughts
styled-components and CSS-in-JS in general are game-changers for modern React development. They offer flexibility, reusability, and style encapsulation all in one package โ making your components not only beautiful but smart. ๐
๐ฅ
If youโre passionate about building a successful blogging website, check out this helpful guide at Coding Tag โ How to Start a Successful Blog. It offers practical steps and expert tips to kickstart your blogging journey!
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:




Comments
Waiting for your comments