What is styled-components?
- React course
- Beginner
- Lesson 57/84
- 4 min
Learn the basics of writing styles inside JS with styled-components.
What is styled-components?
styled-components embeds styles into JavaScript. You define a component as styled.button\`...\` and write CSS inside.
The result is an isolated component with its own class names; no className clashes. Style and markup live in one place.
This method belongs to the "CSS-in-JS" family; it is powerful for theming and dynamic props. You install the package to use it.
Template syntax
styled-components uses a JavaScript template literal. You write CSS between backticks (`); it is normal CSS syntax.
The result is a React component; you use it as <Btn />. The class name stays stable across renders; isolation is guaranteed.
Later we will send props to this template and make colors dynamic.
Practice Styled button
Complete the skeleton. Create a styled component BlueButton defined with styled.button\`color: blue;\`. Return <BlueButton>Send</BlueButton>. Export it.
Starter code
// Define BlueButton hereSample solution
const BlueButton = styled.button`color: blue;`;
function App() { return <BlueButton>Send</BlueButton>; }
export default BlueButton;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which family does styled-components belong to?
CSS is written inside a JavaScript file.- inline style
- CSS Modules
- CSS-in-JS Correct answer
- global CSS
Explanation: The styled component is defined inside JS.
2 What is styled-components: fill in the missing part
const Btn = styled.button`____`
const Btn = styled.button`color: blue;`
Explanation: You wrote CSS in the template.
3 What is styled-components: match the terms
| Term | Meaning |
|---|---|
styled.button`...` |
Styled component |
`...` |
Template literal |
<Btn /> |
Usage |
CSS-in-JS |
Family |