Performance: Why styling options matter
Styling options affect page load speed, rendering performance, and visual stability by influencing CSS size, selector complexity, and reflow frequency.
import styles from './Page.module.css'; export default function Page() { return <div className={styles.container}>Hello World</div>; } /* Page.module.css */ .container { padding: 20px; }
import '../styles/global.css'; export default function Page() { return <div className="container big-padding">Hello World</div>; } /* global.css */ .container { padding: 20px; } .big-padding { padding: 100px; }
| Pattern | DOM Operations | Reflows | Paint Cost | Verdict |
|---|---|---|---|---|
| Global CSS with broad selectors | Low | Multiple on style changes | High due to large repaint areas | [X] Bad |
| CSS Modules with scoped selectors | Low | Single on load | Low due to smaller repaint areas | [OK] Good |
| Inline dynamic styles changing layout | Low | One per interaction | High due to layout thrashing | [X] Bad |
| CSS classes with transitions | Low | None on toggle | Low, handled by compositor | [OK] Good |
import styles from './filename.module.css'; to import CSS modules.export default function Button() {
return <button className="bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded">Click Me</button>;
}hover:bg-blue-700 means the background color changes to blue-700 shade on hover.import styles from './Button.module.css';
export default function Button() {
return <button className="styles.button">Press</button>;
}className={styles.button}.className="styles.button" which treats it as a string, not a variable, so styles are not applied.Pass the test and this pattern moves unknown