Skip to main content
Brave Programmer Logo

BraveProgrammer

BraveProgrammer

HomeProjectsBlogsCoursesLessonsAbout

Site footer

BraveProgrammer

Free coding courses, practical tutorials, and real projects from BraveProgrammer. Learn web development with React, Next.js, and TypeScript.

Navigation

  • Home
  • Projects
  • Blogs
  • Courses

Resources

  • About
  • Lessons
  • Teach on BraveProgrammer

© 2026 BraveProgrammer. All rights reserved.

CSS Modules (Scoped CSS) | CSS Basics to Advanced lesson | BraveProgrammer
  1. Courses
  2. /
  3. CSS Basics to Advanced

Lesson 42 of 50

·css

CSS Modules (Scoped CSS)

8 mins

Course progress84%

What you'll learn

Keep these ideas in mind as you work through the lesson.

3 points
  • Ideal for component‑based UI libraries.
  • No runtime overhead – class names are generated once during bundling.
  • Combine with **CSS‑in‑JS** solutions if you need dynamic values.

CSS Modules is a build‑time technique that scopes class names to a component, preventing global leakage.

How It Works

  1. You write a normal .module.css file with local class names.
  2. During bundling (Webpack, Vite, Parcel) each class is hashed into a unique identifier.
  3. The generated identifier is exported as a JavaScript object.

Example – React Component

// Button.module.css
.primary {
  background:#0066cc; color:#fff; padding:0.5rem 1rem; border:none;
}
import styles from './Button.module.css';
function Button({children}) {
  return <button className={styles.primary}>{children}</button>;
}

Rendered HTML → <button class="Button_primary__3X1yZ">…</button>


Configuration (Webpack)

module.exports = {
  module: {
    rules: [
      {
        test: /\.module\.css$/,
        use: [
          'style-loader',
          {
            loader: 'css-loader',
            options: { modules: true }
          }
        ]
      }
    ]
  }
};

Advantages

  • No runtime overhead – class names are generated once at build time.
  • Guarantees no accidental global overrides.
  • Works with CSS‑in‑JS solutions (e.g., you can still use styled-components for dynamic values).

When to Use

  • Component libraries (React, Vue, Svelte) where each component owns its styles.
  • Large apps where naming collisions become a real risk.

Limitations

  • You cannot reference a module class from a global stylesheet.
  • Dynamic class composition requires a helper (classnames library) or template literals.

Pro Tip – Combine with Design Tokens

Export your design token variables as a JS object (tokens.js) and import them into the component to keep colours consistent across CSS Modules and JS style objects.

Accessibility Note

Because CSS Modules are just a naming convention, all accessibility considerations (focus styles, contrast) remain the same – just make sure you don’t unintentionally hide a globally‑scoped rule.

Make it stickPractice what you've learned1 quiz · 6 practical tests · Optional activities from CSS Basics to AdvancedBrowse activities
Previous lessonPostCSS & AutoprefixerContinue to lesson 43CSS‑in‑JS (Styled‑Components, Emotion)Back to CSS Basics to Advanced curriculum