At its core, a CSS Module is a CSS file in which all class names and animation names are scoped locally by default. This means that when you write CSS in a .module.css file, those styles are unique to the component that imports them, preventing unintended side effects across your application.
In a Next.js project, you can create a file like MyComponent.module.css:
.image-center{
height:100px;
width:100px;
}
.container {
background-color: blue;
padding: 20px;
}
.title {
color: white;
font-size: 2em;
}
Then, import and use it in your React component:
import styles from './MyComponent.module.css';
function MyComponent() {
return (
<div className={styles.container}>
<h1 className={styles.title}>Hello, CSS Modules!</h1>
</div>
);
}
export default MyComponent;
CSS Modules provide a robust way to manage styles in large applications, leading to more organized and scalable codebases.