Summary - Styling
- React course
- Beginner
- Lesson 56/84
- 5 min
Write a styled price tag component with CSS Modules.
Summary - Styling
In this group you saw three styling methods: className + CSS (global, simple), CSS Modules (scoped, safe) and inline style (dynamic, quick).
In JSX write className not class; in inline objects use camelCase. Modules prevent clashes; inline changes with state.
Later we will see styled-components and theming; for now these three cover most work.
Practice Styling summary task
Complete the skeleton. Write a StatusBadge component; import useState. Define const [active, setActive] = useState(false);. Return <span className={active ? "on" : "off"}>{active ? "On" : "Off"}</span>. Export it.
Starter code
import { useState } from "react";
// Define StatusBadge hereSample solution
import { useState } from "react";
function StatusBadge() {
const [active, setActive] = useState(false);
return <span className={active ? "on" : "off"}>{active ? "On" : "Off"}</span>;
}
export default StatusBadge;Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 Which is wrong?
CSS dashes become camelCase.- JSX uses className
- inline uses background-color Correct answer
- inline takes dynamic values
- Modules prevent clashes
Explanation: inline uses backgroundColor (camelCase).
2 Summary - Styling: match the terms
| Term | Meaning |
|---|---|
className |
CSS class |
styles.x |
Modules class |
style={{}} |
Inline object |
camelCase |
Property name |