An Introduction to CSS Modules in React

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.
Loves sharing knowledge through technical articles.
Dev.to: https://dev.to/myogeshchavan97
https://linktr.ee/myogeshchavan97
Search for a command to run...

Full Stack Developer | Freelancer | JavaScript | React | Nodejs.
Loves sharing knowledge through technical articles.
Dev.to: https://dev.to/myogeshchavan97
https://linktr.ee/myogeshchavan97
No comments yet. Be the first to comment.
In this tutorial, you will build a Task Management App using Next.js 16 and Prisma 7 from scratch. By creating this app, you will learn: How to set up Prisma 7 with the new Rust-free architecture How to configure a database with Prisma's driver ada...

If you're a React/Next.js developer who diligently updated your packages last week after the critical React2Shell vulnerability (CVE-2025-55182), I have some frustrating news: you need to update again. Security researchers, while probing the patches ...

Is your React app dragging its feet, but you can’t pinpoint the cause? You might think you’re following the best practices, but some common development patterns are actually degrading your app’s performance behind the scenes. In this article, we’ll u...

How to Use Separate SSH Keys for Company and Personal GitHub Accounts Managing multiple GitHub accounts is a common challenge for developers who juggle both personal and professional projects. It becomes even more complex when you want to use differe...

Have you ever built a React app only to realize that users from different countries can't use it effectively because it only supports English? In this tutorial, you'll learn how to build truly global React application using internationalization (i18n...

When it comes to React, following are some of the ways of styling in React.
In this article, we will explore how to use CSS Modules.
CSS module is a CSS file in which all class names and animation names are scoped locally by default
In short, all the CSS declared in the file are local to the file in which this CSS file is imported.
We will use CSS modules in the context of React but they are not limited to just React.
You can use the CSS modules with any module bundler like webpack or browserify or any other way which supports importing CSS files.
An alternative to CSS modules in React to create local scope is to use styled components.
CSS Modules are the preferred way of styling in Gatsby.js and Next.js.
In the React application, we usually create a single .css file and import it to the main file so the CSS will be applied to all the components.
But using CSS modules helps to create separate CSS files for each component and is local to that particular file and avoids class name collision.
That's enough about the Introduction. Let's see how to use it.
CSS modules support is provided by create-react-app out-of-the-box so we don't need to install or configure anything to get it working.
When working with React, it's a convention to name the CSS file with .module.css extension.
Suppose, we have a header.module.css file in the components folder with the following content:
.title {
font-size: 2.5rem;
}
then in the components/Header.js file, we import this file in the following way:
import headerStyles from "./header.module.css";
and use it like this:
<div>
<h1 className={headerStyles.title}>CSS Modules</h1>
</div>
CodeSandbox demo:
Check out the preview for the above code sandbox here.
If you check the preview above, you will see that even though we have given as title as the class for h1, the final CSS class is different when displayed in the browser.

So CSS Modules helps to create random classes when displayed in the browser.
It's common to name a CSS class with hyphen like menu-items.
So to use that class inside components we need to use the bracket notation like this:
<div className={headerStyles["menu-items"]}>Some text...</div>
CodeSandbox demo:
Consider we have two separate classes like this:
.bold {
font-weight: bold;
}
.active {
color: orange;
}
Then to use both of these classes for the same element we need to use the ES6 template literal syntax like this:
<div className={`${headerStyles["bold"]} ${headerStyles["active"]}`}>Some text...</div>
CodeSandbox demo:
You can find the complete GitHub source code for this article in this repository.
Learning Modern JavaScript is not an easy task. But don't worry, this guide will help you to become an expert in Modern JavaScript and better at React.
Don't forget to subscribe to get my weekly newsletter with amazing tips, tricks, and articles directly in your inbox here.