npm install --save-dev css-loader style-loader
Create CSS file: Next, create a CSS file for your component and name it with a .module.css
extension (e.g., Component.module.css
).
Define styles: Use standard CSS syntax to define the styles for your component in the CSS file. You can use class names to style different elements within your component.
Import CSS module: In your component file, import the CSS module using the following syntax:
import styles from './Component.module.css';
<div className={styles.container}>
<h1 className={styles.title}>Hello World</h1>
</div>
const isPrimary = true;
<div className={isPrimary ? styles.primary : styles.secondary}>Hello World</div>
By following these steps, you can easily style your React components using CSS modules, which provide scoped styles and prevent style conflicts in your application.