LearnFrenzy
Sign in Take a free test

Styling and UI Patterns · Lesson 25 of 31

Styling React Components

5 min read Updated Nov 8, 2026 Saurabh Samir

React doesn't come with a styling system – you choose one. The good news is that all the popular options work with Vite and Next.js out of the box. This lesson compares the main approaches so you can pick the right one for a project and recognise each in a codebase.

In this lesson

  • Plain CSS and global styles
  • CSS Modules for scoped class names
  • Tailwind CSS utility classes
  • Inline styles and conditional classes

Plain CSS

// src/components/Button.jsx
import "./Button.css";                             // Vite injects it into the page

export function Button({ children }) {
  return <button className="btn">{children}</button>;
}
/* src/components/Button.css – global: any .btn anywhere gets these styles */
.btn { padding: 8px 16px; border-radius: 8px; background: #087ea4; color: white; }

Simple and familiar, but every class is global. In a big app, two components that both use .card or .title will clash.

CSS Modules: scoped by default

Name the file *.module.css. The import gives you an object whose keys are your class names, and the build tool makes each one unique:

/* src/components/CourseCard.module.css */
.card { border: 1px solid #ddd; border-radius: 12px; padding: 16px; }
.title { font-size: 1.125rem; margin: 0 0 4px; }
.featured { border-color: #087ea4; box-shadow: 0 0 0 2px #087ea433; }
// src/components/CourseCard.jsx
import styles from "./CourseCard.module.css";

export function CourseCard({ title, featured }) {
  return (
    <article className={featured ? `${styles.card} ${styles.featured}` : styles.card}>
      <h3 className={styles.title}>{title}</h3>
    </article>
  );
}
// rendered HTML: <article class="_card_x7f2k _featured_x7f2k">…</article>

No naming conflicts and no extra library – a solid default for component-based apps.

Tailwind CSS

Tailwind provides small utility classes that you combine directly in JSX. Unused classes are removed from the production CSS, so the final file stays small.

npm install tailwindcss @tailwindcss/vite
// vite.config.js
import { defineConfig } from "vite";
import react from "@vitejs/plugin-react";
import tailwindcss from "@tailwindcss/vite";

export default defineConfig({ plugins: [react(), tailwindcss()] });

/* src/index.css */
@import "tailwindcss";
export function CourseCard({ title, lessons, featured }) {
  return (
    <article className={`rounded-xl border p-4 ${featured ? "border-sky-600 ring-2 ring-sky-600/20" : "border-gray-200"}`}>
      <h3 className="mb-1 text-lg font-semibold">{title}</h3>
      <p className="text-sm text-gray-600">{lessons} lessons</p>
      <button className="mt-3 rounded-lg bg-sky-700 px-4 py-2 text-white hover:bg-sky-800 md:px-6">Start</button>
    </article>
  );
}

Tailwind is the most popular choice in new React and Next.js projects: no switching between files, consistent spacing and colours, and responsive (md:) and state (hover:) variants inline. The trade-off is long class strings – extract repeated UI into components rather than repeating classes.

Conditional classes

Template strings with several conditions get messy. The tiny clsx package (or your own helper) joins only the truthy classes:

import { useState } from "react";

// a 3-line version of clsx: keep strings and the keys of truthy object values
function cx(...args) {
  return args.flatMap((a) => (typeof a === "string" ? [a] : Object.keys(a ?? {}).filter((k) => a[k]))).join(" ");
}

function Tab({ label, active, disabled, onClick }) {
  const className = cx("tab", { "tab--active": active, "tab--disabled": disabled });
  return (
    <button className={className} disabled={disabled} onClick={onClick}>
      {label} · {className}
    </button>
  );
}

export default function App() {
  const [tab, setTab] = useState("lessons");
  return (
    <nav>
      <Tab label="Lessons" active={tab === "lessons"} onClick={() => setTab("lessons")} />
      <Tab label="Quiz" active={tab === "quiz"} onClick={() => setTab("quiz")} />
      <Tab label="Certificate" disabled />
    </nav>
  );
}

Result (class names shown for the demo)

[Lessons · tab tab--active] [Quiz · tab] [Certificate · tab tab--disabled (disabled)]

› click “Quiz · tab”
[Lessons · tab] [Quiz · tab tab--active] [Certificate · tab tab--disabled (disabled)]

Inline styles

export default function App() {
  const percent = 64;
  return (
    <div>
      <p>Course progress: {percent}%</p>
      <div style={{ background: "#e5e7eb", borderRadius: 999, height: 8 }}>
        <div style={{ width: `${percent}%`, background: "#087ea4", height: "100%", borderRadius: 999 }} />
      </div>
    </div>
  );
}

Result

Course progress: 64%

Inline styles are perfect for truly dynamic values like a progress width or a user-chosen colour. They can't do :hover, media queries or animations, so use classes for everything else. A CSS custom property is a neat middle ground: style={{ "--progress": `${percent}%` }} with the rest in CSS.

ApproachScopedBest for
Plain CSSnosmall apps, global base styles
CSS Modulesyescomponent apps that prefer writing CSS
Tailwind CSSn/a (utilities)fast UI building, design consistency, most new projects
Inline stylesyesdynamic values only
CSS-in-JS (styled-components, Emotion)yesexisting codebases; runtime libraries fit poorly with Server Components

Component libraries

Many teams don't style every button by hand. shadcn/ui (copy-in components built on Tailwind and Radix), MUI, Chakra UI and Mantine give you accessible, ready-made components. Learn the basics above first – you'll still customise them with these same techniques.

Practice

Try it yourself

  1. Make an Alert whose class is alert alert--success, alert--warning or alert--error based on a type prop, defaulting to success.

    Show solution
    function Alert({ type = "success", children }) {
      const className = `alert alert--${type}`;
      return <p className={className} role="status">{children} ({className})</p>;
    }
    
    export default function App() {
      return (
        <>
          <Alert>Profile saved</Alert>
          <Alert type="warning">Your trial ends in 3 days</Alert>
          <Alert type="error">Payment failed</Alert>
        </>
      );
    }

    Result

    Profile saved (alert alert--success)
    Your trial ends in 3 days (alert alert--warning)
    Payment failed (alert alert--error)

Quick check

How does a CSS Module avoid class name clashes?

The build tool rewrites each class to a unique name, and you reference it as styles.name.

Can inline styles use :hover or media queries?

No – use classes for those.

Why keep Tailwind class strings inside components?

So repeated UI is reused as a component instead of copying long class lists.

Summary

  • Plain CSS is global; CSS Modules scope class names per component.
  • Tailwind composes utilities in JSX and is the common choice for new projects.
  • Use clsx-style helpers for conditional classes and inline styles only for dynamic values.

Finished this lesson?

Mark it complete to track your progress, then carry on with the next lesson.

Next lesson

Search LearnFrenzy

Esc

Quick links