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 CSS | Component-level styling |
|---|---|
| Class names collide with each other | Each component has its own scoped classes |
| You cannot tell which CSS is used where | Styles live next to the component |
| Unused CSS keeps piling up | Remove the component and its style goes too |
| Dynamic styling is hard | Changing classes based on state is easy |
3. Where is it used?
| Approach | When it is better |
|---|---|
| Plain CSS + className | Small apps, global base styles |
| Inline style | Dynamic runtime values, like a progress width |
| CSS Modules | Scoped classes, when you like writing plain CSS |
| Tailwind CSS | Fast development, design systems, most modern projects |
4. How does it work?
Inline style takes a JavaScript object, not a CSS string. Property names are camelCase: backgroundColor, fontSize.
5. Code example
className and a conditional class
import "./Button.css";
function Button({ variant = "primary", children }) {
return <button className={`btn btn-${variant}`}>{children}</button>;
}Inline style (dynamic value)
function ProgressBar({ percent }) {
return (
<div className="track">
<div style={{ width: `${percent}%`, backgroundColor: "teal" }} />
</div>
);
}CSS Modules
/* Card.module.css */
.card {
padding: 16px;
border-radius: 8px;
}import styles from "./Card.module.css";
function Card({ children }) {
return <div className={styles.card}>{children}</div>;
}Tailwind CSS
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?
| Part | Meaning |
|---|---|
className | HTML's class, renamed in JSX because class is a reserved word |
style={{ ... }} | The outer braces are JSX, the inner ones are the object |
styles.card | CSS Modules generates a unique class name |
| Template string | Joins classes based on state or props |
| Tailwind classes | Each 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
| Stage | Chapters |
|---|---|
| Before | JSX, Setup with Vite |
| Alongside | Conditional Rendering, Props |
| After | TypeScript with React |
Final quick revision
| Point | Detail |
|---|---|
| Class | className |
| Inline | An object with camelCase properties |
| Scoped CSS | *.module.css |
| Utility first | Tailwind CSS |
| Dynamic | Choose a complete class with a condition |