codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
▲

Next.js

16 / 73 topics
16CSS Modules17Global Styles18Sass Support19Styled JSX20Theming with Next.js
Tutorials/Next.js/CSS Modules
▲Next.js

CSS Modules

Updated 2026-04-20
3 min read

CSS Modules in Next.js

CSS Modules are a powerful feature that helps you write scoped styles in your React applications, reducing the risk of class name collisions and making your styles more maintainable. In this section, we'll explore how to use CSS Modules in Next.js, including setting up, writing styles, and best practices.

Introduction to CSS Modules

CSS Modules allow you to write CSS that is scoped to a single component. This means that the styles defined in one module do not leak into other components or global stylesheets. Each class name is automatically transformed into a unique identifier, ensuring that styles are encapsulated within their respective components.

Setting Up CSS Modules in Next.js

Next.js supports CSS Modules out of the box, so you don't need to install any additional packages. To use CSS Modules, simply rename your CSS files with the .module.css extension.

Example: Creating a CSS Module

  1. Create a CSS file: Create a new file named Button.module.css in your project directory.
/* styles/Button.module.css */
.button {
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;
}

.button:hover {
  background-color: darkblue;
}
  1. Import and use the CSS Module in a component: Import the CSS module into your React component and apply the styles.
// components/Button.js
import styles from '../styles/Button.module.css';

const Button = ({ children }) => {
  return <button className={styles.button}>{children}</button>;
};

export default Button;

In this example, styles.button is a unique class name generated by Next.js. This ensures that the .button style only applies to the Button component and does not affect other components.

Using CSS Modules with Dynamic Styles

CSS Modules also support dynamic styles using template literals or conditional logic within your components.

Example: Dynamic Styling

// components/DynamicButton.js
import styles from '../styles/Button.module.css';

const DynamicButton = ({ primary, children }) => {
  const buttonClass = primary ? styles.buttonPrimary : styles.buttonSecondary;
  return <button className={buttonClass}>{children}</button>;
};

export default DynamicButton;
/* styles/Button.module.css */
.buttonPrimary {
  background-color: blue;
  color: white;
}

.buttonSecondary {
  background-color: gray;
  color: black;
}

In this example, the DynamicButton component applies different styles based on the primary prop.

Best Practices for Using CSS Modules

1. Use Descriptive Class Names

When writing CSS Modules, use descriptive and meaningful class names to make your code more readable and maintainable.

/* Good */
.userProfile {
  display: flex;
  align-items: center;
}

/* Bad */
.box {
  display: flex;
  align-items: center;
}

2. Avoid Global Styles

CSS Modules are designed to avoid global styles. If you need to use global styles, consider using the :global selector.

/* styles/global.module.css */
:global(body) {
  font-family: Arial, sans-serif;
}

:global(a) {
  color: blue;
}

3. Organize Your Styles

Organize your CSS files and modules logically to keep your project maintainable. Group related styles together in the same file or directory.

4. Use CSS Preprocessors

Next.js supports CSS preprocessors like Sass, Less, and Stylus with CSS Modules. To use a preprocessor, install the necessary package and rename your CSS files accordingly.

npm install sass

Then, create a .scss file:

/* styles/Button.module.scss */
.button {
  background-color: blue;
  color: white;
  padding: 10px 20px;
  border: none;
  border-radius: 5px;

  &:hover {
    background-color: darkblue;
  }
}

5. Optimize for Performance

While CSS Modules help avoid style conflicts, they can increase the size of your CSS bundle. To optimize performance, consider:

  • Minifying CSS: Use tools like css-minimizer-webpack-plugin to minify your CSS.
  • Tree Shaking: Ensure that unused styles are removed from the final build.

Conclusion

CSS Modules in Next.js provide a robust solution for scoped styling in React applications. By following best practices and leveraging the features of CSS Modules, you can create maintainable, scalable, and efficient styles for your components. Whether you're building a small application or a large-scale project, CSS Modules are an essential tool in your Next.js development toolkit.

By now, you should have a solid understanding of how to use CSS Modules in Next.js, including setting up, writing styles, and applying best practices. Happy coding!


PreviousSWR Library for Client-Side Data FetchingNext Global Styles

Recommended Gear

SWR Library for Client-Side Data FetchingGlobal Styles