Menu
×
   ❮     
HTML CSS JAVASCRIPT SQL PYTHON JAVA PHP HOW TO W3.CSS C C++ C# BOOTSTRAP REACT MYSQL JQUERY EXCEL XML DJANGO NUMPY PANDAS NODEJS DSA TYPESCRIPT ANGULAR GIT POSTGRESQL MONGODB ASP AI R GO KOTLIN SASS VUE GEN AI SCIPY CYBERSECURITY DATA SCIENCE INTRO TO PROGRAMMING BASH RUST

React CSS Modules


CSS Modules let you write CSS that is scoped locally to a specific component.

This prevents CSS class name conflicts and makes your styles more maintainable.


What are CSS Modules?

In React, CSS Modules are CSS files where class names are scoped locally by default.

Note: CSS Modules are not a part of the React core library, but are supported by many React build tools.

The CSS file have to have the .module.css extension and can be used by importing it into your React file(s).


Creating a CSS Module

Let's create a CSS module called Button.module.css, where we style some buttons.

Example

Create a file named Button.module.css, and insert some styles in it:

.mybutton {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

Using a CSS Module

Import and use the CSS Module in your component:

Example

Create a Button component that uses the CSS Module:

import styles from './Button.module.css';

function App() {
  return (
    <div>
      <button className={styles.mybutton}>
        My Button
      </button>
    </div>
  );
}

Run Example »

Example Explained

  • We import the styles object from the CSS Module
  • We use styles.mybutton to access the mybutton class
  • The actual class name of the button will be unique (e.g., _mybutton_q1obu_1)

Multiple Classes

In the example above, we only used one class, but let's add more classes:

Example

Add more styles in Button.module.css:

.mybutton {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.primary {
  background-color: #007bff;
  color: white;
}

.secondary {
  background-color: #6c757d;
  color: white;
}

To demostrate the changes, we need to have two buttons, with two classes each:

Example

An example with two buttons, with different styling:

import styles from './Button.module.css';

function App() {
  return (
    <div>
      <button className={`${styles.mybutton} ${styles.primary}`}>
        My Primary Button
      </button>
      <button className={`${styles.mybutton} ${styles.secondary}`}>
        My Secondary Button
      </button>
    </div>
  );
}

Run Example »


Composing Classes

CSS Modules allow you to combine classes using the composes keyword:

Which means that one class can inherit the styles of another class.

For the previous example, both the primary and the secondary classes are depending on the styles of the mybutton class.

This can be done by adding composes: mybutton to the primary and secondary classes:

Example

.mybutton {
  padding: 10px 20px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
}

.primary {
  composes: mybutton;
  background-color: #007bff;
  color: white;
}

.secondary {
  composes: mybutton;
  background-color: #6c757d;
  color: white;
}

Now it is enough to use the primary and secondary classes in the component:

Example

import styles from './Button.module.css';

function App() {
  return (
    <div>
      <button className={styles.primary}>
        Primary Button
      </button>
      <button className={styles.secondary}>
        Secondary Button
      </button>
    </div>
  );
}

Run Example »


Global Classes

When using CSS Modules, the classes in the .module.css file can only be used in the component that imports them. This is done by prefixing the class name with a hash of the file name and a unique identifier. It is safe to use the same class name in different files, as the names will be unique.

However, sometimes you want your classes to be available globally, and use them in other components.

You can do this with the :global syntax:

Example

Here is a CSS Module with a global class named .myheader:

:global(.myheader) {
  padding: 10px 20px;
  font-size: 50px;
  color: white;
  background-color: dodgerblue;
}

The :global wrapper makes the class available for everyone.

It is simply called .myheader and not prefixed and added a unique identifier like _myheader_q1obu_1

You can use it in your components like this:

Example

import styles from './BlueHeader.module.css';

function App() {
  return (
    <div>
      <h1 className="myheader">
        My Header
      </h1>
    </div>
  );
}

Run Example »


Combine Global and Local Classes

You can combine global and local classes in the same CSS Module:

Example

A CSS Module with both global and local classes:

:global(.myheader) {
  padding: 10px 20px;
  font-size: 50px;
  color: white;
  background-color: dodgerblue;
}

.myparagraph {
  font-size: 20px;
  color: white;
  background-color: purple;
}

Use it in your components like this:

Example

import styles from './MyStyles.module.css';

function App() {
  return (
    <div>
      <h1 className="myheader">
        My Header
      </h1>
      <p className={styles.myparagraph}>
        My Paragraph
      </p>
    </div>
  );
}

Run Example »



×

Contact Sales

If you want to use W3Schools services as an educational institution, team or enterprise, send us an e-mail:
sales@w3schools.com

Report Error

If you want to report an error, or if you want to make a suggestion, send us an e-mail:
help@w3schools.com

W3Schools is optimized for learning and training. Examples might be simplified to improve reading and learning. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. While using W3Schools, you agree to have read and accepted our terms of use, cookie and privacy policy.

Copyright 1999-2025 by Refsnes Data. All Rights Reserved. W3Schools is Powered by W3.CSS.