Skip to main content

CSS Modules

CSS Modules is a popular approach to styling in React applications. It is a module system for CSS that allows local scoping of styles by default, thus avoiding global scope issues.

With CSS Modules, each component's CSS styles are scoped to the component. This helps prevent conflicts between styles in different components, since the styles only apply to the specific component they were defined in. It also makes it easier to understand which styles are being used and where they are defined, which can help with code maintainability.

CSS Modules uses a special syntax to define styles in the component files. Styles are imported as an object, and can be referenced using a naming convention that maps to the generated class names. This class name is unique to the component and not shared globally, which ensures the correct styles are applied to the correct component.

For example, consider the following component:

import styles from './Button.module.css';

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

In this example, the styles are imported from a separate CSS module file called Button.module.css. The className attribute is set to styles.button, which references the button class defined in the CSS module.

CSS Modules has become a popular choice in the React community due to its benefits in local scoping, maintainability, and ease of use. It can also be used in conjunction with other CSS tools and libraries, such as CSS preprocessors like Sass or Less.