Styling Components — React

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…

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>
}

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>
  )
}
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>
}

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:

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

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.

What you will learn