Chapter 18: Styling in React

In React, there are many ways to style components. Each method has its strengths, trade-offs, and best use cases. Today we’ll cover the four most popular and modern approaches in 2026:

  1. Inline styles (quick & simple)
  2. CSS Modules (scoped, reliable)
  3. Styled-components / Emotion (CSS-in-JS, dynamic styles)
  4. Tailwind CSS (utility-first, fastest for modern apps)

We’ll go very slowly and clearly, like I’m sitting next to you in Mumbai explaining everything live with complete, copy-paste-ready examples.

1. Inline Styles (Quick & Simple – But Limited)

Inline styles = writing CSS directly in the style prop as a JavaScript object.

Pros:

  • Super fast to write
  • Dynamic styles (easy to use state/props)
  • No extra files

Cons:

  • No pseudo-classes (:hover, :focus)
  • No media queries
  • No nesting
  • Hard to maintain in big components
  • No reusability

Example:

tsx

Best use: Small one-off styles, dynamic values (colors from theme, sizes from props).

2. CSS Modules (Scoped CSS – Very Clean & Reliable)

CSS Modules = normal CSS files where class names are automatically scoped to the component (no global pollution).

Pros:

  • Scoped styles (no accidental overrides)
  • Full CSS power (pseudo-classes, media queries, nesting with PostCSS)
  • Works with any CSS preprocessor (Sass, Less…)
  • Easy to read & maintain

Cons:

  • Need to import CSS file
  • Class names become long (hashed)

Setup (Vite already supports it out of the box)

Create file: src/components/Button.module.css

CSS

Use in component:

tsx

Pro tip: Use camelCase for class names → styles.primaryButton

3. Styled-components / Emotion (CSS-in-JS – Dynamic & Powerful)

CSS-in-JS = write CSS inside JavaScript components.

Popular libraries:

  • styled-components (most popular)
  • Emotion (lighter, faster)

Pros:

  • Full dynamic styles (props, theme, state)
  • Scoped automatically
  • Supports pseudo-classes, media queries, nesting
  • Great developer experience (autocompletion)

Cons:

  • Extra runtime overhead (small)
  • Slightly larger bundle

Install styled-components:

Bash

Example:

tsx

With theme (best practice):

tsx

4. Tailwind CSS with React (Utility-First – Fastest Modern Way)

Tailwind CSS = utility-first CSS framework. You write styles directly in JSX using class names.

Why Tailwind is exploding in 2026:

  • Extremely fast development
  • Consistent design
  • No more writing CSS files
  • Great with TypeScript + Vite
  • shadcn/ui, daisyUI, Mantine… all built on Tailwind

Setup in Vite (easiest way):

Bash

Update tailwind.config.js:

JavaScript

Add to src/index.css:

CSS

Example:

tsx

Pro tip: Install Tailwind CSS IntelliSense extension in VS Code → autocomplete + hover previews.

Summary – Chapter 18 Key Takeaways

Method Best For Dynamic Styles Scoped Pseudo-classes Bundle Size Impact
Inline Quick one-offs, dynamic props Excellent Yes No Zero
CSS Modules Clean, traditional CSS, big teams Good Yes Yes Zero
Styled-components Dynamic, themeable, component-scoped Excellent Yes Yes Small runtime
Tailwind CSS Fast development, consistent design Good Yes Yes Small (purged)

My 2026 Recommendation for you:

  • Learning/small projects → CSS Modules or Tailwind
  • Production apps → Tailwind CSS (fastest & most popular today)
  • Need heavy dynamic styles → styled-components

Mini Homework

  1. Create a beautiful Card component using each method (4 versions)
  2. Try Tailwind + add hover effects, dark mode toggle
  3. Bonus: Combine Tailwind + custom theme context

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *