How to Add CSS in React: 4 Easy Ways for Beginners
Learn how to add CSS in React with className, inline styles, CSS Modules and state. Simple beginner examples with line-by-line explanations you can copy.
Learn how to add CSS in React with className, inline styles, CSS Modules and state. Simple beginner examples with line-by-line explanations you can copy.
You want to know how to add CSS in React. The short answer: React gives you four easy ways. You can import a normal CSS file, write inline styles with a style object, use CSS Modules, or toggle class names with state. This guide shows each one with small examples you can copy, and explains every line in plain words.
You do not need to be an expert. If you know what a React component is, you are ready. If not, read our guide on React components first. CSS stands for Cascading Style Sheets. It is the language that controls colors, sizes, spacing and layout on a web page.
In normal HTML you give an element a CSS class with the word class. In React you write JSX, which looks like HTML but is really JavaScript. The word class is already used by JavaScript, so React uses className instead.
function Welcome() {
return <h1 className="title">Hello, React!</h1>;
}
export default Welcome;Line 2 returns a heading. The part className="title" attaches a CSS class called title to it. Remember this one rule and half of React styling is done.
This is the easiest way and the best one to start with. You write CSS in a separate file, then import it into your component.
src/
Welcome.jsx
Welcome.cssThis shows two files side by side in the src folder. One holds the component, the other holds its styles.
/* Welcome.css */
.title {
color: royalblue;
font-size: 32px;
}// Welcome.jsx
import './Welcome.css';
function Welcome() {
return <h1 className="title">Hello, React!</h1>;
}
export default Welcome;In the CSS file, .title is a selector. It means every element with the class title gets blue text at 32 pixels. In the component, import './Welcome.css' loads the file. Then className="title" connects the heading to the rule.
One warning: a regular CSS file is global. A class called title in one file can affect every component that uses that name. For small projects this is fine.
An inline style is written directly on the element. In React, the style prop does not take a string. It takes a JavaScript object.
function Badge() {
return (
<span style={{ backgroundColor: 'green', color: 'white', padding: '4px 8px' }}>
New
</span>
);
}Look at the double curly braces. The outer pair says: here comes JavaScript. The inner pair is the object. Inside it, CSS names written with a dash become camelCase, so background-color turns into backgroundColor. Values are strings, such as 'green' or '4px 8px'.
Inline styles are quick for one-off changes. They cannot use hover effects or media queries, so use them sparingly.
A CSS Module is a CSS file whose class names only work inside the component that imports it. That avoids the global name clash from Method 1. Name the file with .module.css. Tools like Vite and Create React App support this already.
/* Card.module.css */
.box {
border: 1px solid #ccc;
border-radius: 8px;
padding: 16px;
}// Card.jsx
import styles from './Card.module.css';
function Card() {
return <div className={styles.box}>I am a card</div>;
}
export default Card;The import line gives you an object called styles. Each class in the file becomes a property on it. So styles.box is the box class. Behind the scenes React tools rename it to something unique, so no other component can clash with it. Notice we use curly braces, not quotes, because styles.box is JavaScript.
This is where React shines. State is data that can change while the app runs. You can change a class name based on state, and the style updates by itself.
import { useState } from 'react';
import './Toggle.css';
function Toggle() {
const [on, setOn] = useState(false);
return (
<button
className={on ? 'btn active' : 'btn'}
onClick={() => setOn(!on)}
>
{on ? 'ON' : 'OFF'}
</button>
);
}/* Toggle.css */
.btn {
padding: 8px 16px;
background: lightgray;
}
.btn.active {
background: seagreen;
color: white;
}Line 4 creates a state value called on, starting as false. Line 8 uses a ternary, a short if/else written as condition ? valueA : valueB. When on is true the button gets two classes, btn and active. Otherwise it gets only btn. Line 9 flips the value on each click. When state changes, React redraws the button and the CSS does the rest.
| Method | Best for | Downside |
|---|---|---|
| Imported CSS file | Small projects, learning | Class names are global |
| Inline style object | Quick one-off or dynamic values | No hover or media queries |
| CSS Modules | Growing apps with many components | Slightly more setup to read |
| State + className | Active, selected, error looks | Needs a CSS rule too |
Practice is the fastest way to learn. Change colors, break things, and fix them. That is how every developer learns.
Not sure what a component is yet? Start with our simple guide.
Read: What Is a React Component?Create a .css file, import it at the top of your component with import './file.css', and add className to your elements. The class in the file then styles those elements.
React uses JSX, which is JavaScript. The word class is reserved in JavaScript for creating classes. So React uses className for the HTML class attribute.
Yes. Pass an object to the style prop, like style={{ color: 'red' }}. Use camelCase names such as backgroundColor. Inline styles cannot do hover effects.
CSS Modules are CSS files named .module.css whose class names are local to the component that imports them. They stop styles from leaking between components.
Store a value in state with useState. Use it in className with a ternary, such as className={on ? 'btn active' : 'btn'}. Update the state in onClick and React updates the style.
Want help building a real website or web app with React? Talk to the Fepiq team.
Contact FepiqOccasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.
Keep reading
Learn SQL AND, OR and NOT operators with simple examples. See how to combine conditions in WHERE and avoid the common mistakes beginners make.
Learn JavaScript template literals with simple examples: backticks, ${} variables, and multi-line strings. A beginner-friendly guide you can copy and run.
Let's build something
Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.