Adding CSS
Adding CSS to React components means giving each piece of your interface its own styles while keeping the JSX readable. React does not invent a new styling language; it uses normal CSS, but how you attach it changes slightly. You will usually import a stylesheet into a component file and apply classes with the className prop.
Importing a stylesheet into a component
In projects built with Vite or Create React App, you can import a CSS file directly at the top of a component. The build tool bundles it and injects it into the page. A common convention is one stylesheet per component, sitting next to it with the same name, such as ExpenseItem.jsx and ExpenseItem.css.
// ExpenseItem.jsx
import "./ExpenseItem.css";
function ExpenseItem({ title, amount }) {
return (
<div className="expense-item">
<h2 className="expense-item__title">{title}</h2>
<div className="expense-item__price">Rs {amount}</div>
</div>
);
}
export default ExpenseItem;
/* ExpenseItem.css */
.expense-item {
display: flex;
justify-content: space-between;
align-items: center;
padding: 12px 16px;
border-radius: 10px;
background: #4b4b4b;
color: #fff;
}
.expense-item__price {
font-weight: 700;
padding: 6px 10px;
border-radius: 8px;
background: #40005d;
}
Why it is className, not class
JSX compiles to JavaScript, and class is a reserved word in JavaScript. React therefore uses className, which matches the DOM property name. If you write class, React shows a warning in the console asking whether you meant className. Similarly, the label's for attribute becomes htmlFor.
Imported CSS is global
This catches nearly every beginner. Importing ExpenseItem.css into one component does not limit its rules to that component. Once bundled, the stylesheet applies to the whole page. If two components both define .title, whichever loads later can override the other, and you get mysterious style clashes.
Two common ways to avoid that:
- Naming conventions: prefix classes with the component name, as in
expense-item__title. This is the BEM style and needs no tooling. - CSS Modules: name the file
ExpenseItem.module.css, import it as an object (import styles from "./ExpenseItem.module.css") and useclassName={styles.title}. The build tool generates unique class names, so clashes become impossible.
Inline styles in JSX
You can also pass a style prop, but it takes a JavaScript object, not a string. Property names are written in camelCase and values are usually strings: style={{ backgroundColor: "teal", fontSize: "1.2rem" }}. The double braces are just a JavaScript object inside a JSX expression. Inline styles are handy for values computed at runtime, but they cannot express hover states or media queries, so normal classes remain the main tool.
Combining several classes
Since className is just a string, you can build it with a template literal: className={`card ${props.className}`}. This lets a wrapper component accept extra classes from its parent while keeping its own base class, a pattern you will use often when building reusable cards and buttons.
Global styles still have a place
Not everything belongs to a component. Fonts, colours for body, a CSS reset and shared custom properties usually live in one file such as index.css, imported once in main.jsx. Keeping those base rules in one place and component rules beside each component makes it easy to find where any style comes from.
Takeaway: import a CSS file per component, use className, remember that plain imports are global, and switch to prefixed names or CSS Modules when clashes appear.
Ready to master real-world web development?
Learn HTML, CSS3, Bootstrap and JavaScript hands-on with mentor-led sessions.