What are CSS modules and how do they work

CSS Modules are a popular way to write CSS that helps avoid conflicts and maintain modular styles. They allow developers to use local scope by automatically generating unique class names, which helps in building more maintainable and reusable styles in large applications.

Here’s how CSS Modules work:

  • Each CSS file is treated as a module.
  • Class names defined in a CSS Module are scoped locally by default.
  • When you import a CSS Module, the class names are transformed into a unique format.
// Importing CSS Module in JavaScript import styles from './styles.module.css'; function Button() { return ; }

CSS Modules modular CSS scoped styles unique class names maintainable CSS