Skip to content
Core ConceptsChapter 13 / 37Beginner4 min read

Styling in React

Learn the ways to style React apps with className, inline style objects, CSS Modules and Tailwind CSS, and how to choose between them.

On this page

1. What is styling in React?

React does not ship its own styling system. You use the same CSS that works on the web, except in JSX you write className instead of class.

2. Why does it matter?

Components are small and reusable, so their styling should be organized too. A single global CSS file quickly becomes messy.

One big global CSSComponent-level styling
Class names collide with each otherEach component has its own scoped classes
You cannot tell which CSS is used whereStyles live next to the component
Unused CSS keeps piling upRemove the component and its style goes too
Dynamic styling is hardChanging classes based on state is easy

3. Where is it used?

ApproachWhen it is better
Plain CSS + classNameSmall apps, global base styles
Inline styleDynamic runtime values, like a progress width
CSS ModulesScoped classes, when you like writing plain CSS
Tailwind CSSFast development, design systems, most modern projects

4. How does it work?

From style to screen
You write a CSS file or Tailwind classes
The component sets className or the style prop
The build tool bundles the CSS
The browser matches the classes and applies the styles

Inline style takes a JavaScript object, not a CSS string. Property names are camelCase: backgroundColor, fontSize.

5. Code example

className and a conditional class

jsx
import "./Button.css";

function Button({ variant = "primary", children }) {
  return <button className={`btn btn-${variant}`}>{children}</button>;
}

Inline style (dynamic value)

jsx
function ProgressBar({ percent }) {
  return (
    <div className="track">
      <div style={{ width: `${percent}%`, backgroundColor: "teal" }} />
    </div>
  );
}

CSS Modules

css
/* Card.module.css */
.card {
  padding: 16px;
  border-radius: 8px;
}
jsx
import styles from "./Card.module.css";

function Card({ children }) {
  return <div className={styles.card}>{children}</div>;
}

Tailwind CSS

jsx
function Badge({ active }) {
  return (
    <span
      className={`rounded-full px-3 py-1 text-sm ${
        active ? "bg-green-600 text-white" : "bg-gray-200 text-gray-700"
      }`}
    >
      {active ? "Active" : "Inactive"}
    </span>
  );
}

How does the code work?

PartMeaning
classNameHTML's class, renamed in JSX because class is a reserved word
style={{ ... }}The outer braces are JSX, the inner ones are the object
styles.cardCSS Modules generates a unique class name
Template stringJoins classes based on state or props
Tailwind classesEach class is one small CSS rule

Setting up Tailwind v4 with Vite is simple: add the @tailwindcss/vite plugin and write @import "tailwindcss"; in your CSS file.

6. Common mistakes

Mistake: Writing class in JSX

Why? JSX is JavaScript and class is a reserved word, so React expects className.

Better: Always write className.

Mistake: Passing a CSS string to inline style, like style='color: red'

Why? React wants an object in the style prop, and a string causes an error.

Better: Write style={{ color: 'red' }} and keep property names in camelCase.

Mistake: Doing everything with inline style

Why? Hover, media queries and pseudo classes do not work in inline style, and the code gets heavy.

Better: Use classes for static styling and inline style only for dynamic runtime values.

Mistake: Building Tailwind class names dynamically by joining strings, like bg- plus a color

Why? Tailwind looks for complete class names at build time, so CSS is not generated for partial names.

Better: Write the full class names and choose between them with a condition.

7. What to learn before and after

Final quick revision

PointDetail
ClassclassName
InlineAn object with camelCase properties
Scoped CSS*.module.css
Utility firstTailwind CSS
DynamicChoose a complete class with a condition