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.
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.
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.
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;
}
// 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.
CSS Modules also support dynamic styles using template literals or conditional logic within your components.
// 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.
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;
}
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;
}
Organize your CSS files and modules logically to keep your project maintainable. Group related styles together in the same file or directory.
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;
}
}
While CSS Modules help avoid style conflicts, they can increase the size of your CSS bundle. To optimize performance, consider:
css-minimizer-webpack-plugin to minify your CSS.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!