Styling in React
React does not force a styling method. You can use plain CSS files, CSS Modules (locally scoped class names), inline style objects, utility CSS like Tailwind, or CSS-in-JS libra...
Introduction
React does not force a styling method. You can use plain CSS files, CSS Modules (locally scoped class names), inline style objects, utility CSS like Tailwind, or CSS-in-JS libraries. For most Vite projects, CSS Modules or Tailwind are the practical default choices because they avoid global class name collisions.
Subtopics
- Global CSS (
import "./index.css") classNameinstead ofclass- CSS Modules (
*.module.css) - Inline styles (
style={{ }}) - Conditional classes
- Tailwind CSS
- CSS variables and theming
- CSS-in-JS overview
- Responsive design and media queries
Syntax
// Button.module.css
// .btn { padding: 8px 16px; border-radius: 6px; }
// .primary { background: royalblue; color: white; }
import styles from "./Button.module.css";
function Button({ primary, children }) {
return (
<button className={`${styles.btn} ${primary ? styles.primary : ""}`}>
{children}
</button>
);
}
// Inline style: object with camelCase keys, numbers become px
<div style={{ backgroundColor: "teal", padding: 16, marginTop: "1rem" }} />
// Conditional classes
<li className={`item ${done ? "done" : ""} ${selected ? "selected" : ""}`} />
// Tailwind
<button className="rounded bg-blue-600 px-4 py-2 text-white hover:bg-blue-700">Save</button>
// CSS variable driven from state
<div style={{ "--accent": color }} className="card" />
Important Methods & Properties
| Method | Strength | Example |
|---|---|---|
| Global CSS | Simple, base styles | import "./index.css" |
| CSS Modules | Scoped class names | styles.card |
| Inline style | Dynamic values | style={{ width: pct + "%" }} |
| Tailwind | Fast utility classes | className="p-4 flex" |
| CSS variables | Themes | var(--accent) |
clsx / classnames | Clean conditional classes | clsx("btn", { active }) |
Common Use Cases
- Beginner: a single
App.cssfor the whole page. - Practical UI: scoped styles for a reusable
Card. - Real-world application: a design system with Tailwind or CSS Modules and theme variables.
Common Errors
- ❌
<div class="card" style="color: red">Hi</div>
Why it fails: JSX needs className, and style must be an object.
✅
<div className="card" style={{ color: "red" }}>Hi</div>
Explanation: Use JSX property names.
- ❌
<div style={{ background-color: "red" }} />
Why it fails: Hyphenated names are invalid JavaScript object keys. ✅
<div style={{ backgroundColor: "red" }} />
Explanation: Use camelCase for CSS properties.
- ❌
// Header.css: .title { color: red } Footer.css: .title { color: blue }
Why it fails: Plain CSS is global, so the two .title classes collide.
✅
import styles from "./Header.module.css";
<h1 className={styles.title}>...</h1>
Explanation: CSS Modules give each file its own scope.
- ❌
<div className="box" style={{ ":hover": { color: "red" } }} />
Why it fails: Inline styles cannot express hover, media queries or pseudo-elements. ✅
<div className="box" /> /* .box:hover { color: red } */
Explanation: Use CSS (or Tailwind variants) for pseudo-classes and media queries.
Common Mistakes
- Using
classinstead ofclassName. - Putting all styling inline.
- Global class names clashing across components.
- Building
"btn " + undefinedclass strings (useclsx). - Forgetting to import the CSS file.
Quick Reference
import "./App.css";
import styles from "./Card.module.css";
<div className={styles.card} />
<div className={clsx("btn", isActive && "active")} />
<div style={{ fontSize: 20, color: "tomato" }} />