Lesson 26 of 37Real projects
Styling
Compare plain CSS, CSS modules, Tailwind, and CSS-in-JS, use design tokens and component libraries, and build accessible UI.
React has no opinion about styling, so every team picks an approach and you will meet several in your career. Knowing the trade-offs (and why most new projects in 2026 use Tailwind plus a headless component library) lets you join any codebase quickly. Accessibility belongs in the same conversation: a beautiful button that cannot be reached with a keyboard is a bug, and in many countries a legal risk.
Plain CSS and className
The simplest option: write a .css file, import it, and set className (JSX uses className because class is a reserved word in JavaScript). Vite bundles imported CSS for you.
(The <style> tag is only there because the preview has no CSS files. In a project, the CSS lives in PlanCard.css and you write import "./PlanCard.css".)
The problem appears at scale: CSS is global. Two components that both use .card or .title silently fight, and nobody dares to delete a rule because anything might depend on it.
CSS modules: scoped class names
Name the file Something.module.css and import it as an object. The build renames each class to something unique, like card_x7f2a, so names cannot collide.
.card { border: 1px solid #d4d4d8; border-radius: 12px; padding: 16px; }
.featured { border-color: #4f46e5; }import styles from "./PlanCard.module.css";
export function PlanCard({ featured }: { featured?: boolean }) {
return <div className={featured ? `${styles.card} ${styles.featured}` : styles.card}>...</div>;
}CSS modules are zero-runtime (it is just CSS at the end), work in Vite and Next.js out of the box, and keep real CSS syntax. You still write and name every rule yourself.
The style prop is the other built-in option: style={{ padding: 16, color: "crimson" }} takes an object with camelCased properties. Use it for truly dynamic values (a progress bar width, a user-chosen colour), not as your main styling approach: it cannot do :hover, media queries, or :focus-visible.
Tailwind CSS: utility-first
Tailwind flips the model. Instead of naming things and writing rules, you compose small single-purpose classes directly in JSX: p-4 is padding, rounded-lg is a border radius, hover:bg-indigo-700 applies on hover, md:flex applies from the medium breakpoint up. The build scans your files and ships only the classes you used.
export function PlanCard({ name, price }: { name: string; price: number }) {
return (
<div className="max-w-xs rounded-xl border border-zinc-300 p-4">
<h3 className="text-lg font-semibold">{name}</h3>
<p className="text-3xl font-bold">${price}/mo</p>
<button className="rounded-lg bg-indigo-600 px-4 py-2 text-white hover:bg-indigo-700 focus-visible:outline-2 focus-visible:outline-offset-2">
Choose {name}
</button>
</div>
);
}Tailwind v4 is configured in CSS, not JavaScript. You import it, and define your own design tokens in an @theme block; each token becomes utilities automatically (--color-brand gives you bg-brand, text-brand, border-brand).
@import "tailwindcss";
@theme {
--color-brand: oklch(0.55 0.2 270);
--font-display: "Inter", sans-serif;
--breakpoint-3xl: 120rem;
}In Vite you add the @tailwindcss/vite plugin. The long class lists look noisy at first; the payoff is that styles live next to markup, there are no names to invent, and a design system's spacing and colours are enforced by the class names that exist.
CSS-in-JS and its trade-offs
Libraries like styled-components and Emotion let you write CSS inside JavaScript, scoped to a component, with props driving styles:
const Button = styled.button<{ $primary?: boolean }>`
padding: 8px 14px;
background: ${(p) => (p.$primary ? "#4f46e5" : "transparent")};
`;They were popular for years, but runtime CSS-in-JS injects styles while rendering, which costs performance and works poorly with React Server Components and streaming. New projects mostly choose Tailwind, CSS modules, or zero-runtime CSS-in-JS (vanilla-extract, Panda CSS) that compiles to static CSS at build time. You will still meet styled-components in many existing codebases.
Match the approach to its defining trait.
Design tokens and component libraries
Design tokens are named design decisions: colours, spacing steps, radii, font sizes. Instead of #4f46e5 in forty places, components use --color-primary, so a rebrand or a dark mode is a change to the token values, not to forty files.
Primitives are the raw palette and spacing scale. They say nothing about meaning.
You rarely build accessible menus, dialogs, and comboboxes from scratch, because getting focus management and keyboard support right is hard. Headless primitive libraries such as Radix UI, Base UI, and React Aria give you unstyled, accessible behaviour. shadcn/ui builds on those primitives with Tailwind styles, and instead of installing a package it copies the component source into your repo (npx shadcn@latest add dialog), so you own and edit it. Fully styled kits like MUI and Mantine are faster to start with but harder to make look like your brand.
Accessibility basics
Accessible UI is mostly about using the right HTML and not breaking what the browser gives you for free.
- Use semantic elements. A
<button>is focusable, works with Enter and Space, and is announced as a button. A<div onClick>is none of those. Use<nav>,<main>,<h1>to<h6>in order,<ul>for lists. - Label every input.
<label htmlFor="email">connected to<input id="email">, or the input nested inside the label. Placeholder text is not a label. - Keep focus visible. Never write
outline: nonewithout a replacement.:focus-visibleshows a ring for keyboard users without showing it on mouse clicks. - Do not rely on colour alone, and keep text contrast at least 4.5:1 against its background (WCAG AA).
- Use ARIA to describe state, not to replace HTML.
aria-expanded,aria-controls, andaria-pressedtell assistive tech what a control is doing.
Press Tab in the preview. The real button gets a visible focus ring and works with the keyboard; the div is skipped entirely.
The @media rule makes the row stack vertically on narrow screens. Responsive design in practice means writing the narrow (mobile) layout first and adding wider layouts with min-width media queries (or Tailwind's sm:, md:, lg: prefixes), using flexible units (rem, %, fr), and testing at real phone widths, around 360 to 400 px.
<div className="icon" onClick={openMenu}><MenuIcon /></div>An accessible accordion
Build an FAQ accordion that works for keyboard and screen reader users.
For each question in FAQ:
- An
<h3>that contains a real<button>with the question text. - The button has
aria-expanded(true/false) andaria-controlspointing to its panel'sid. - When open, render the panel: a
<div role="region">with thatid,aria-labelledbypointing to the button'sid, and the answer text. When closed, do not render the panel at all.
Only one panel is open at a time. Clicking the open question closes it. All panels start closed.
- CSS modules
- CSS files whose class names are made unique per file at build time, imported as an object.
- utility-first
- Styling by composing small single-purpose classes (like Tailwind's
p-4) instead of writing named rules. - design token
- A named design decision, such as
--color-primary, used instead of raw values so themes change in one place. - headless component
- A component library piece that provides behaviour and accessibility with no styles, such as Radix or Base UI.
- :focus-visible
- A CSS pseudo-class that matches focus from keyboard navigation, used to show focus rings without affecting mouse clicks.
- semantic HTML
- Using elements for their meaning (
button,label,nav, headings) so browsers and assistive tech understand the page.