Skip to content

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.

15 min read0/4 questions

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.

ReactPlainCss.tsx
const css = ` .card { border: 1px solid #d4d4d8; border-radius: 12px; padding: 16px; max-width: 320px; font-family: system-ui; } .card h3 { margin: 0 0 4px; } .card .price { font-size: 28px; font-weight: 700; } .btn { padding: 8px 14px; border-radius: 8px; border: 0; background: #4f46e5; color: white; cursor: pointer; } .btn:hover { background: #4338ca; } .btn-ghost { background: transparent; color: #4f46e5; border: 1px solid #4f46e5; }`; function PlanCard({ name, price, featured }: { name: string; price: number; featured?: boolean }) { return ( <div className="card"> <h3>{name}</h3> <p className="price">${price}/mo</p> <button className={featured ? "btn" : "btn btn-ghost"}>Choose {name}</button> </div> );} export default function App() { return ( <div style={{ display: "flex", gap: 12, flexWrap: "wrap" }}> <style>{css}</style> <PlanCard name="Starter" price={0} /> <PlanCard name="Pro" price={12} featured /> </div> );}

(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.

PlanCard.module.css
.card { border: 1px solid #d4d4d8; border-radius: 12px; padding: 16px; }
.featured { border-color: #4f46e5; }
PlanCard.tsx
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.

PlanCard.tsx
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).

src/index.css
@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.

Quick check
A teammate worries that Tailwind will make the CSS bundle huge because there are thousands of utilities. What actually happens?
Need a nudge? A hint rules out a wrong answer.

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 pairs0/5 matched

Match the approach to its defining trait.

Choose an item, then its match on the other side.

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.

Layers of a design system
  1. Components: <Button variant="primary">use semantic tokens only
  2. Semantic tokens: --color-primary, --color-danger, --space-cardwhat a value is for
  3. Primitive tokens: --indigo-600, --red-500, --space-4the raw palette and scale

Primitives are the raw palette and spacing scale. They say nothing about meaning.

1/3
Quick check
Your app uses the three token layers above. To add a dark theme, what should change?
Need a nudge? A hint rules out a wrong answer.

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: none without a replacement. :focus-visible shows 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, and aria-pressed tell 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.

ReactFocusAndSemantics.tsx
import { useState } from "react"; const css = ` .btn { padding: 8px 14px; border-radius: 8px; border: 1px solid #4f46e5; background: white; color: #4f46e5; font: inherit; } .btn:focus-visible { outline: 3px solid #f59e0b; outline-offset: 2px; } .fake { display: inline-block; padding: 8px 14px; border-radius: 8px; background: #e4e4e7; cursor: pointer; } .row { display: flex; gap: 12px; flex-wrap: wrap; align-items: center; } @media (max-width: 480px) { .row { flex-direction: column; align-items: stretch; } }`; export default function App() { const [clicks, setClicks] = useState({ real: 0, fake: 0 }); return ( <div> <style>{css}</style> <div className="row"> <label htmlFor="name">Name</label> <input id="name" type="text" /> <button className="btn" onClick={() => setClicks({ ...clicks, real: clicks.real + 1 })}> Real button ({clicks.real}) </button> <div className="fake" onClick={() => setClicks({ ...clicks, fake: clicks.fake + 1 })}> Div "button" ({clicks.fake}) </div> </div> <p style={{ fontSize: 14 }}>Tab through: the div never receives focus, so keyboard users cannot press it.</p> </div> );}

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.

Quick check
Which change fixes this for keyboard and screen reader users? <div className="icon" onClick={openMenu}><MenuIcon /></div>
Need a nudge? A hint rules out a wrong answer.
ChallengeMedium

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) and aria-controls pointing to its panel's id.
  • When open, render the panel: a <div role="region"> with that id, aria-labelledby pointing to the button's id, 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.

React
import { useState } from "react"; const FAQ = [ { id: "shipping", question: "How long does shipping take?", answer: "Orders arrive in 3 to 5 working days.", }, { id: "returns", question: "Can I return an item?", answer: "Yes, within 30 days, unused and in the original box.", }, { id: "payment", question: "Which payments do you accept?", answer: "Cards, PayPal, and bank transfer.", },]; export default function App() { // Track which item is open (or null), and render a heading + button + panel per item. return <div>{/* your accordion */}</div>;}
Key terms
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.