A Beginner’s Guide to CSS Modules in React: Easy Styling with Scoped CSS

Navneet Singh
3 min readJun 26, 2023

CSS modules are a powerful tool for styling components in React applications. They provide a way to create modular and scoped CSS styles, eliminating the risk of class name clashes and allowing for easy reuse of styles across components. In this article, we will explore the fundamentals of CSS modules and learn how to use them in a React project. By the end, you’ll have a clear understanding of how CSS modules work and how to leverage their benefits in your own projects.

Getting Started: To start using CSS modules, you’ll be pleased to know that they come pre-installed with popular tools like Create-React-App and Vite. There is no need for any additional installations. Let’s dive into the process of using CSS modules step-by-step.

Creating a CSS Module: The first step is to create an external CSS file for each component. For example, if we have a component called PageNav, we would create a new file named “PageNav.module.css” in the component folder. The “.module.css” extension is crucial for identifying the file as a CSS module.

Defining Styles in the CSS Module: Within the CSS module file, you can define classes to style your component. For instance, let’s create a “nav” class with some basic styles. For demonstration purposes, we’ll use the “justify-content: space-between;” property.

/* PageNav.module.css */

.nav {
display: flex;
justify-content: space-between;
}

Connecting CSS Modules to Components: To connect the CSS module to the component, you need to import the styles defined in the module. In the component file (e.g., PageNav.js), import the styles object from the CSS module file. Typically, the styles object is assigned to the variable name “styles.” Now you can use the class names defined in the CSS module by accessing them through the styles object.

// PageNav.js

import React from 'react';
import styles from './PageNav.module.css';

const PageNav = () => {
return (
<nav className={styles.nav}>
{/* Content */}
</nav>
);
};

export default PageNav;

Using CSS Module Classes in Markup: Once you have imported the styles, you can apply the CSS module classes to the desired elements in your component’s JSX. Instead of passing a string as the class name, use the class names from the styles object. For example, if you defined a “nav” class in the CSS module, you can use it as className={styles.nav}.

// PageNav.js

import React from 'react';
import styles from './PageNav.module.css';

const PageNav = () => {
return (
<nav className={styles.nav}>
<ul className={styles.navList}>
<li className={styles.navItem}>Home</li>
<li className={styles.navItem}>About</li>
<li className={styles.navItem}>Contact</li>
</ul>
</nav>
);
};

export default PageNav;

Avoiding Global Class Name Clashes: CSS modules prevent class name clashes by appending a unique identifier to each class name. This ensures that styles are scoped to their respective components. However, it’s still possible to have multiple classes with the same name in different modules. CSS modules will generate unique class names to avoid conflicts.

Creating Global Styles with CSS Modules: While CSS modules excel at providing scoped styles, there are cases where you may need global CSS, such as a reset or global font styles. To include global CSS using CSS modules, create a regular CSS file (e.g., “global.css”) and import it in your entry file (e.g., “index.js”). The global styles will apply to the entire application, while the scoped styles will only affect the respective components.

// index.js

import React from 'react';
import ReactDOM from 'react-dom';
import './global.css';
import App from './App';

ReactDOM.render(<App />, document.getElementById('root'));

CSS modules offer a fantastic solution for managing component styles in React applications. They promote modularity, encapsulation, and reusability while eliminating the risks of class name clashes. By leveraging CSS modules, you can write cleaner and more maintainable styles in your React projects. Hopefully, this guide has given you a solid foundation to start using CSS modules effectively in your own applications.

With this updated article, you now have a comprehensive guide to understanding CSS modules in React, along with React code examples to illustrate each step. Happy coding!

--

--