You've just come from HTML, so you write <div class="box" style="color: red"> in JSX. React prints a warning about class, then refuses to render because of the style. In JSX, these two attributes are written differently.
There are two basic ways to style a React component. className points to a class in a CSS file. style takes a JavaScript object holding the styles for one element. It's like choosing clothes from a wardrobe. className names an outfit that's already prepared in the wardrobe (the CSS file): "wear the badge-success outfit". style puts one detail directly on one piece of clothing, without going through the wardrobe. Here the piece of clothing is the element, the wardrobe is the CSS file, and each outfit is one class. Because a component is a function that receives props, you can also pick the outfit based on a prop's value, like picking clothes to suit the weather.
className, not class
In HTML you write class="...". In JSX the attribute is className, because in JavaScript, class is a keyword for creating JavaScript classes, which are different from CSS classes.
This is a CSS file with two classes, then a component that uses one of them:
/* Badge.css */
.badge-success { background: #dcfce7; color: #166534; }
.badge-error { background: #fee2e2; color: #991b1b; }
import "./Badge.css"
function App() {
return <span className="badge-success">Success</span>
}
import "./Badge.css"loads the CSS file once. The classes in it apply to the whole page.className="badge-success"becomesclass="badge-success"in the HTML that's produced, so the.badge-successrule in the CSS applies to that element.
Inline style: an object, not a string
An inline style is a style written directly on one element:
function App() {
return (
<div id="box" style={{ backgroundColor: "skyblue", padding: "20px" }}>
Blue Box
</div>
)
}
- The outer curly braces mean "this is a JavaScript expression", just like
{count}. The inner curly braces are the object. That's why the curly braces look doubled:style={{ ... }}. - Property names are camelCase:
background-colorin CSS becomesbackgroundColor. A minus sign isn't allowed in an object's property name. - Values are strings, and sizes include their units:
"20px","skyblue". For sizes a bare number works too: React readspadding: 20as20px. - The separators are colons and commas, like any object, not
=or semicolons.
| In a CSS file | In style={{ }} |
|---|---|
background-color: skyblue; |
backgroundColor: "skyblue" |
padding: 20px; |
padding: "20px" |
font-size: 24px; |
fontSize: "24px" |
color: red; |
color: "red" |
If style is given a string such as style="color: red", React refuses to render and throws an error: style must be an object, not a string. The right version: style={{ color: "red" }}.
A class that changes with a prop
This Badge component picks its class from the prop type:
function Badge({ type, children }) {
return (
<span className={type === "success" ? "badge-success" : "badge-error"}>
{children}
</span>
)
}
function App() {
return <Badge type="success">Success</Badge>
}
className={...}uses curly braces, so what's inside is an expression that's calculated again on every render.className="badge-success"in quotes is always the same, whatevertypeis.type === "success" ? "badge-success" : "badge-error"picks the class name with a ternary, exactly as in the conditional rendering lesson.{children}is whatever<Badge>wraps, here the textSuccess.
What it renders: <span class="badge-success">Success</span>, green. Change it to type="error", and it renders <span class="badge-error">, red.
If one class is always used and another only sometimes, join the two with a template literal: className={`badge ${active ? "badge-active" : ""}`}. In this example the class badge is always there, and badge-active is only added when active is true.
📌 Other ways in big projects
Classes in a plain CSS file are global (they apply to the whole page): .badge in two different files can overwrite each other. Two popular ways solve this, and both still go through className:
- CSS Modules: a
Badge.module.cssfile is imported as an object, for example namedstyles, and then used asclassName={styles.success}. The class names are made unique per file automatically. - Tailwind: styles are written with small ready-made classes, for example
className="px-4 py-2 rounded bg-sky-200", without writing a CSS file of your own.
For conditional classes, many projects use the small library clsx: clsx("badge", active && "badge-active") gives "badge badge-active" when active is true, and "badge" when it is false.
⚠️ Styles that get refused, ignored, or show the wrong colour
styleas a string.style="color: red"makes React throw an error. Write an object:style={{ color: "red" }}.- Property names with a minus sign.
{{ font-size: "20px" }}is a JavaScript syntax error. WritefontSize.{{ margin = "8px" }}is a syntax error too: an object uses a colon,{{ margin: "8px" }}. :hoverand@mediain an inline style. An inline style can't use them. Put styles like that in a CSS class.- A fixed className when it should follow a prop.
className="badge-success"keeps an error badge green. Calculate it from the prop:className={type === "success" ? "badge-success" : "badge-error"}.
TL;DR: In JSX, the class attribute is written className, and the classes are defined in a CSS file. An inline style (a style written directly on one element) is an object inside double curly braces: style={{ backgroundColor: "skyblue", padding: "20px" }}. Its property names are camelCase, and its values are strings (sizes with units). Never give style a CSS string like "color: red", because React throws an error. A class that depends on a prop is calculated with an expression: className={type === "success" ? "badge-success" : "badge-error"}. An inline style can't use :hover or @media, so styles like that go in a class. Big projects often use CSS Modules or Tailwind (two other ways to write styles), which still go through className.
⚡ className, not class
Write:
import "./Badge.css"
function App() {
return <span className="badge-success">Success</span>
}
Meaning: In JSX, the class attribute is written className. The classes themselves are written in an ordinary CSS file, and import "./Badge.css" loads that file.
⚡ An inline style is an object
Write:
<div id="box" style={{ backgroundColor: "skyblue", padding: "20px" }}>
Blue Box
</div>
Guess first: why does React reject style="color: red", written exactly as in HTML?
In React, style must be an object, not a string. The outer curly braces mark JavaScript, and the inner ones are the object.
Meaning: An inline style is a style written directly on one element. Property names are written in camelCase (backgroundColor, not background-color), and the values are strings with units, such as "20px".
⚡ A class calculated from a prop
Write:
<span className={type === "success" ? "badge-success" : "badge-error"}>
{children}
</span>
Meaning: This code is inside the Badge component. type and children are its props, and children is whatever <Badge> wraps. className can be calculated inside curly braces too. A badge with type="success" gets the class badge-success, and any other type gets badge-error.