Skip to content

Styling in React

CSS Modules, styled-components, Tailwind, and choosing a styling approach.

What you'll learn

  • The main styling approaches in React
  • CSS Modules for scoped, traditional CSS
  • CSS-in-JS with styled-components
  • Utility-first CSS with Tailwind

Concept

Styling Approaches

React does not prescribe a styling solution — there are several popular approaches, each with trade-offs. Plain CSS / CSS classes (via className) is the simplest: you write CSS files and apply class names. CSS Modules scope class names automatically to avoid collisions. CSS-in-JS libraries like styled-components and Emotion let you write CSS inside JavaScript with full access to props and theme. Utility-first frameworks like Tailwind CSS provide small, composable utility classes. The best choice depends on your team, project size, and preferences.

CSS Modules

CSS Modules are CSS files where class names are locally scoped by default. You import a CSS file as styles and apply classes as className={styles.card}. The build tool generates unique class names (like card_abc123), preventing collisions between components. This gives you the familiarity of plain CSS with the safety of scoping. CSS Modules work great with component-based architecture — each component has its own .module.css file.

CSS-in-JS (styled-components)

styled-components lets you define styled elements using tagged template literals: const Button = styled.buttoncolor: ${props => props.primary ? "blue" : "gray"};``. Styles are scoped to the component, can adapt based on props, and support theming via a ThemeProvider. The styles are generated at runtime (or extracted at build time with some setups). This approach keeps styles co-located with the component and enables dynamic styling based on props, but adds runtime overhead and can make debugging harder.

Utility-First (Tailwind CSS)

Tailwind CSS provides low-level utility classes like flex, p-4, text-lg, bg-blue-500 that you compose directly in your JSX: className="flex p-4 bg-blue-500 text-white rounded". This approach eliminates the need to name things and switch between CSS and JSX files. It produces small, optimized CSS bundles (only the classes you use are included). The trade-off is that className strings can get long, and there is a learning curve for the utility class names. Tailwind is increasingly the most popular choice for new React projects.

Example

              // Approach 1: CSS Modules — scoped, traditional CSS
import styles from "./Button.module.css";
function ButtonA({ label }) {
  return <button className={styles.primary}>{label}</button>;
}

// Approach 2: styled-components — CSS-in-JS with dynamic props
import styled from "styled-components";
const StyledButton = styled.button`
  padding: 8px 16px;
  border-radius: 4px;
  background: ${props => props.primary ? "#3b82f6" : "#6b7280"};
  color: white;
  font-size: 14px;
  cursor: pointer;
  &:hover { opacity: 0.9; }
`;
function ButtonB({ label, primary }) {
  return <StyledButton primary={primary}>{label}</StyledButton>;
}

// Approach 3: Tailwind CSS — utility classes composed in JSX
function ButtonC({ label, primary }) {
  return (
    <button className={`
      px-4 py-2 rounded text-sm text-white cursor-pointer
      hover:opacity-90 transition-opacity
      ${primary ? "bg-blue-500" : "bg-gray-500"}
    `}>
      {label}
    </button>
  );
}
            

Three approaches to the same button: CSS Modules for scoped CSS, styled-components for dynamic CSS-in-JS, and Tailwind for utility-class composition. Each has different trade-offs.

Try it

Common mistakes

The mistake

Using inline styles for everything instead of classes

The fix

Inline styles (style={{}}) cannot use pseudo-classes, media queries, or animations. Use className with CSS files, CSS Modules, or Tailwind for anything beyond simple one-off styles.

The mistake

Creating overly long Tailwind class strings that are hard to read

The fix

Extract repeated class combinations into reusable components or use Tailwind's @apply directive in a CSS file. Keep className strings readable by grouping related utilities and using multi-line template literals.

Look up unfamiliar terms

A beginner-friendly glossary explains jargon in plain English — variables, functions, DOM, Promise, and more.

View glossary

Build real projects

Apply what you learned by building guided projects with starter code and solutions.

View projects

Decode error messages

Plain-English explanations of common errors — what they mean, why they happen, and how to fix them.

View errors