All posts
ReactNovember 7, 20267 min read

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.

F
Fepiq Team
Fepiq

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.

The big idea: className instead of class

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.

jsx
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.

Method 1: Import a normal CSS file

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.

bash
src/
  Welcome.jsx
  Welcome.css

This shows two files side by side in the src folder. One holds the component, the other holds its styles.

jsx
/* Welcome.css */
.title {
  color: royalblue;
  font-size: 32px;
}
jsx
// 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.

Method 2: Inline styles with a style object

An inline style is written directly on the element. In React, the style prop does not take a string. It takes a JavaScript object.

jsx
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.

Method 3: CSS Modules (styles that stay local)

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.

jsx
/* Card.module.css */
.box {
  border: 1px solid #ccc;
  border-radius: 8px;
  padding: 16px;
}
jsx
// 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.

Method 4: Change styles with state

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.

jsx
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>
  );
}
jsx
/* 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.

Which method should you use?

MethodBest forDownside
Imported CSS fileSmall projects, learningClass names are global
Inline style objectQuick one-off or dynamic valuesNo hover or media queries
CSS ModulesGrowing apps with many componentsSlightly more setup to read
State + classNameActive, selected, error looksNeeds a CSS rule too

Common beginner mistakes

  • Writing class instead of className. React will show a warning.
  • Writing style="color: red" as a string. It must be an object: style={{ color: 'red' }}.
  • Using font-size inside a style object. Use fontSize instead.
  • Forgetting to import the CSS file, so nothing changes.
  • Forgetting px units in CSS files. Inline numbers like fontSize: 20 are fine; React adds px for you.

Try it yourself

  1. Create a new React app with Vite.
  2. Make a Welcome component and a Welcome.css file.
  3. Import the CSS and style the heading with className.
  4. Add a button that toggles a class using useState.
  5. Move one style into a CSS Module and compare.

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?

Frequently asked questions

How do I add CSS to 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.

Why does React use className instead of class?+

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.

Can I use inline styles in React?+

Yes. Pass an object to the style prop, like style={{ color: 'red' }}. Use camelCase names such as backgroundColor. Inline styles cannot do hover effects.

What are CSS Modules in React?+

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.

How do I change a style when a button is clicked?+

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 Fepiq

Get new posts in your inbox

Occasional, no-fluff notes on shipping modern software — startups, automation, Laravel, Shopify and more. No spam, unsubscribe anytime.

Keep reading

Related posts

All posts

Let's build something

Ready to ship your next product with Fepiq?

Book a free discovery call. We'll listen, ask sharp questions, and send you a proposal within 3 business days.