Home / Learn React / Styling Options: CSS Modules, Tailwind and CSS-in-JS
Styling Options: CSS Modules, Tailwind and CSS-in-JS
Besides plain CSS and inline styles, React projects often use CSS Modules, Tailwind CSS or a CSS-in-JS library. Each solves the "styles leak everywhere" problem in a different way.
Theory in plain words
What it is
A styling approach is how you write and attach CSS to components: global files, scoped files, utility classes or JavaScript objects.
Why we use it
Global CSS can clash between components. A clear approach keeps styles close to the component, avoids name collisions and keeps the bundle small.
How it works
CSS Modules rename classes at build time (.btn becomes btn_x7a2). Tailwind scans your classes and generates only the CSS you use. CSS-in-JS creates style tags at runtime or build time.
Where it is used
- Design systems, dashboards, any component library
A CSS variable set from state is a clean way to theme a component without building long inline style objects.
/* Button.module.css */
.btn { padding: 8px 14px; border-radius: 8px; }
// Button.jsx
import s from "./Button.module.css";
export default function Button() { return <button className={s.btn}>Save</button>; }
// Tailwind (v4 with Vite)
npm install tailwindcss @tailwindcss/vite
// vite.config.js -> plugins: [tailwindcss()]
// index.css -> @import "tailwindcss";clsx library makes conditional class names tidy.Quick quiz
1. What do CSS Modules do to class names?
They prevent collisions.
2. Tailwind is...
Small classes you combine.
3. A CSS variable set from state is good for...
Cleaner than long style objects.