Performance: CSS Modules
CSS Modules affect how styles are loaded and scoped, impacting page load speed and style recalculation.
import styles from './Button.module.css'; function Button() { return <button className={styles.primary}>Click me</button>; }
import './styles.css'; function Button() { return <button className="btn primary">Click me</button>; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global CSS import | Low (styles applied globally) | Low (styles trigger once) | Medium (large CSS size) | [!OK] |
| CSS Modules import | Low (scoped styles) | Low (minimal style recalculation) | Low (smaller CSS size) | [OK] Good |
CSS Modules in a Next.js project?Button.module.css in a Next.js component?.module.css.import styles from './Button.module.css'; correctly imports the module as an object.styles.module.css with content:.title { color: red; }import styles from './styles.module.css';
export default function Header() {
return <h1 className={styles.title}>Hello</h1>
}.title color to red.className={styles.title}, so the red color applies.import styles from './Card.module.css';
export default function Card() {
return <div className="styles.card">Content</div>
}className="styles.card", which is a string literal, not the variable.className={styles.card} to use the imported styles object.primary and large from Button.module.css. Which is the correct way to apply both classes to a <button> element?Pass the test and this pattern moves unknown