Styling React apps
Overview
React components need styles, and there’s no single “React way” to write them. The JSX className attribute and the style prop are all React provides — the rest is up to your CSS tooling. This lesson compares the common approaches so you can pick one confidently.
Plain CSS
Vite lets you import CSS files from JavaScript:
import './CourseCard.css';
export function CourseCard({ course }) {
return (
<article className="course-card">
<h2 className="course-card__title">{course.title}</h2>
</article>
);
}/* CourseCard.css */
.course-card { border: 1px solid #e6e6e3; border-radius: 12px; padding: 16px; }
.course-card__title { font-size: 1.25rem; }It’s the simplest option, but every class is global: a .title in one file affects every .title in the app. Naming conventions like BEM (block__element--modifier) help avoid clashes. Plain CSS is still ideal for global styles — resets, typography, CSS variables — in index.css.
CSS Modules
Name a file *.module.css and Vite turns its classes into locally scoped names:
/* CourseCard.module.css */
.card { border: 1px solid #e6e6e3; border-radius: 12px; padding: 16px; }
.title { font-size: 1.25rem; }
.featured { border-color: #2d5bd7; }import styles from './CourseCard.module.css';
export function CourseCard({ course }) {
return (
<article className={course.featured ? `${styles.card} ${styles.featured}` : styles.card}>
<h2 className={styles.title}>{course.title}</h2>
</article>
);
}styles.card is a generated, unique name like _card_1x9k2_1, so .title here never collides with .title elsewhere. You write normal CSS — media queries, pseudo-classes, CSS variables all work — and it ships as real CSS files. No extra setup needed.
Tailwind CSS
Tailwind provides small utility classes that you combine directly in JSX:
export function CourseCard({ course }) {
return (
<article className="rounded-xl border border-gray-200 p-4 hover:shadow-md">
<h2 className="text-xl font-semibold">{course.title}</h2>
<p className="mt-1 text-sm text-gray-500">{course.lessons} lessons</p>
</article>
);
}Setup with Vite (Tailwind v4):
npm install tailwindcss @tailwindcss/vite// vite.config.js
import react from '@vitejs/plugin-react';
import tailwindcss from '@tailwindcss/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [react(), tailwindcss()],
});/* src/index.css */
@import "tailwindcss";Tailwind only generates the classes you use, so the CSS stays small. It pairs naturally with components: the component is the abstraction, so long class lists live in one place. The trade-off is busier JSX and a vocabulary to learn. It’s one of the most popular choices for new React projects, and component kits like shadcn/ui build on it.
Inline styles
The style prop takes an object (see Writing JSX). Use it for values that are computed at runtime and can’t be known in advance:
<div className="progress">
<div className="progress-bar" style={{ width: `${percent}%` }} />
</div>Avoid inline styles for everything else: they can’t use media queries or :hover, and they’re harder to override. A good middle ground for dynamic values is a CSS variable:
<div className="avatar" style={{ '--avatar-size': `${size}px` }} />.avatar { width: var(--avatar-size); height: var(--avatar-size); }Conditional class names
Joining class names by hand gets messy. The tiny clsx library (or a five-line helper) handles conditions:
import clsx from 'clsx';
<button
className={clsx('btn', {
'btn-primary': variant === 'primary',
'btn-loading': isLoading,
}, className)}
>Falsy values are ignored, objects include keys whose values are truthy, and nested arrays are flattened.
Designing component variants
A design system’s components expose a small set of variants rather than accepting arbitrary styles:
<Button variant="primary">Enrol</Button>
<Button variant="secondary" size="sm">Preview</Button>
<Button variant="danger" onClick={deleteCourse}>Delete</Button>const variants = {
primary: 'bg-blue-600 text-white hover:bg-blue-700',
secondary: 'bg-white text-gray-900 border border-gray-300 hover:bg-gray-50',
danger: 'bg-red-600 text-white hover:bg-red-700',
};
export function Button({ variant = 'primary', className, ...props }) {
return (
<button
className={clsx('rounded-lg px-4 py-2 font-semibold disabled:opacity-50', variants[variant], className)}
{...props}
/>
);
}This keeps the UI consistent and makes design changes a one-file edit. Accepting a className prop as an escape hatch for one-off layout tweaks (margins, widths) is common and useful.
CSS-in-JS
Libraries such as styled-components and Emotion write CSS inside JavaScript. They were very popular, but runtime CSS-in-JS doesn’t work well with Server Components and adds work at runtime, so new projects increasingly prefer CSS Modules, Tailwind, or zero-runtime alternatives (vanilla-extract, Panda CSS). You’ll still meet them in existing codebases.
Theming and dark mode
CSS variables make theming easy regardless of approach:
:root { --bg: #ffffff; --text: #1b1d21; --accent: #2d5bd7; }
[data-theme='dark'] { --bg: #16181c; --text: #e6e8ee; --accent: #8fb4ff; }
body { background: var(--bg); color: var(--text); }useEffect(() => {
document.documentElement.dataset.theme = theme;
}, [theme]);Respect the user’s system setting by default with @media (prefers-color-scheme: dark).
Choosing
| Approach | Good when |
|---|---|
| Plain CSS | small apps, global styles |
| CSS Modules | you like writing real CSS and want zero naming conflicts |
| Tailwind | fast iteration, design-system consistency, component-heavy apps |
| Inline styles | runtime values only |
Any of these is a fine choice. The important thing is to pick one per project and use it consistently.
Accessibility counts as styling too: make sure focus outlines stay visible (
:focus-visible), text has enough contrast, and hover-only effects have keyboard equivalents.
A Button with variants
Write a Button component styled with a CSS Module (Button.module.css) that supports variant (primary | secondary | danger, default primary), size (sm | md, default md) and fullWidth. Combine the classes with a small cx(...) helper that ignores falsy values, forward any other props (onClick, type, disabled) to the <button>, and allow a custom className to be added.
import styles from './Button.module.css';
function cx(...classes) {
// your code here
}
export function Button(/* props */) {
// your code here
}/* Button.module.css
.button { border-radius: 8px; font-weight: 600; border: 1px solid transparent; cursor: pointer; }
.md { padding: 10px 16px; font-size: 1rem; }
.sm { padding: 6px 10px; font-size: 0.875rem; }
.primary { background: #2d5bd7; color: white; }
.secondary { background: white; color: #1b1d21; border-color: #d6d6d2; }
.danger { background: #c0392b; color: white; }
.fullWidth { width: 100%; }
.button:disabled { opacity: 0.5; cursor: not-allowed; }
*/
import styles from './Button.module.css';
function cx(...classes) {
return classes.filter(Boolean).join(' ');
}
export function Button({
variant = 'primary',
size = 'md',
fullWidth = false,
className,
type = 'button',
...props
}) {
return (
<button
type={type}
className={cx(styles.button, styles[variant], styles[size], fullWidth && styles.fullWidth, className)}
{...props}
/>
);
}Summary
- React doesn’t prescribe a styling approach — every option ends up as CSS the browser applies.
- Plain CSS is simple but global; CSS Modules scope class names to a file automatically.
- Tailwind CSS styles with utility classes in JSX and is very popular for component-based apps.
- Use inline
stylefor truly dynamic values; build conditional class names with a helper likeclsx. - Design components with a small set of variants instead of accepting arbitrary styles.