In traditional HTML, inline styles are declared as semicolon-delimited text strings:
In React JSX, inline styles are passed as JavaScript objects.
This gives styles the full power of JavaScript: dynamic variables, ternary operators, and computed calculations can be seamlessly blended into your component presentation.
Because property keys in JavaScript objects cannot contain hyphens without being wrapped in quotes, React converts CSS property names to camelCase:
| Standard CSS | React JSX Style Key |
|---|---|
background-color | backgroundColor |
font-size | fontSize |
margin-top | marginTop |
border-radius | borderRadius |
z-index | zIndex |
box-shadow | boxShadow |
When passing numeric values to dimension properties (such as width, height, margin, padding, fontSize), React automatically appends the 'px' suffix for you:
:hover, :focus, :active, ::before, or ::after.@media (max-width: 768px)) cannot be written in inline style objects.width: 250?rem units in an inline style object?Continue learning with hands-on practice, examples, and exercises in the upcoming topic.
| Previous Lesson | Next Lesson |
|---|---|
| Cleanup Functions in useEffect | CSS Stylesheets and Global Class Names |
Test your understanding of this lesson with 5 questions. Each question has one correct answer.
Practice in live mentor-led sessions with real projects, mock tests, and verifiable certification at MSK Institute.