CSS Modules
CSS Modules
Using CSS Modules in React lets us scope our CSS styles to a single component preventing clashes with other styles in our application.
To use CSS Modules in components the css file must follow this naming convention: [name].module.css
, with name
typically being the component name.
Example Card Component w/ CSS Modules
Card.module.css
Style normally using CSS classes:
Card.jsx
Import the class names as styles
and apply to the JSX elements.
After compilation (built-in to Vite), unique class names will be generated and applied to the HTML and CSS.
This improves encapsulation and makes it easier to maintain and style components especially in bigger applications.
Last updated
Was this helpful?