React › Components & JSX

How do you add a CSS class to an element in JSX, and why is the attribute name different from regular HTML?

Beginner

Answer

In JSX you use className instead of class, because class is already a reserved word in JavaScript used for defining classes. Everything else about applying a CSS class works exactly the same as in regular HTML.
function Button() {
  return <button className="primary-button">Click Me</button>;
}

/* In your CSS file */
.primary-button {
  background-color: blue;
  color: white;
}
Real-world example A team styling their app's buttons uses className to apply a shared primary button style, keeping their JSX consistent with how normal HTML and CSS work together.

Common follow-ups: What other HTML attribute names are different or renamed in JSX?;How would you apply more than one CSS class to a single element in JSX?