Discover how to stop your styles from fighting each other and keep your website looking perfect!
Why CSS Modules in NextJS? - Purpose & Use Cases
Imagine styling a large website where many pages share similar class names like button or header. You write CSS in one big file, but suddenly changing the style of one button breaks the look on another page.
Using global CSS means styles can clash and override each other unexpectedly. It's hard to track which styles affect which parts of your site. This leads to bugs, messy code, and lots of time spent debugging.
CSS Modules let you write CSS that is scoped locally to each component. This means class names are unique and won't conflict, so you can style components safely without worrying about breaking others.
.button { background: blue; } /* affects all buttons globally */import styles from './Button.module.css'; function Button() { return <button className={styles.button}>Click me</button>; /* styles.button is unique */ }
It enables building scalable, maintainable styles where each component controls its own look without accidental interference.
Think of a website with a navigation bar and multiple buttons. With CSS Modules, you can style each button differently in their own files without worrying about one button's style breaking another's.
Global CSS can cause style conflicts and bugs.
CSS Modules scope styles locally to components.
This makes styling safer, cleaner, and easier to maintain.
Practice
CSS Modules in a Next.js project?Solution
Step 1: Understand CSS Modules purpose
CSS Modules scope CSS locally to components, so styles don't leak or conflict.Step 2: Compare options with this purpose
Only They scope CSS styles locally to components, avoiding conflicts. describes this key benefit. Others describe unrelated features.Final Answer:
They scope CSS styles locally to components, avoiding conflicts. -> Option CQuick Check:
CSS Modules = Local scoped styles [OK]
- Thinking CSS Modules minify CSS automatically
- Confusing CSS Modules with CSS-in-JS
- Believing CSS Modules create global styles
Button.module.css in a Next.js component?Solution
Step 1: Identify correct file extension for CSS Modules
Next.js requires CSS Modules files to end with.module.css.Step 2: Check import syntax
Importing withimport styles from './Button.module.css';correctly imports the module as an object.Final Answer:
import styles from './Button.module.css'; -> Option BQuick Check:
Import CSS Modules with .module.css and assign to variable [OK]
- Using wrong file extension like .css or .cssmodule
- Importing without assigning to a variable
- Forgetting the .module part in filename
styles.module.css with content:.title { color: red; }And the React component:
import styles from './styles.module.css';
export default function Header() {
return <h1 className={styles.title}>Hello</h1>
}What color will the <h1> text be when rendered?
Solution
Step 1: Check CSS Module style for .title
The CSS sets.titlecolor to red.Step 2: Confirm component uses styles.title as className
The component appliesclassName={styles.title}, so the red color applies.Final Answer:
Red -> Option AQuick Check:
CSS Module class applied = red text [OK]
- Assuming default black color without checking CSS
- Confusing CSS Modules with global CSS
- Not using styles.title but a string instead
import styles from './Card.module.css';
export default function Card() {
return <div className="styles.card">Content</div>
}Solution
Step 1: Identify how className is assigned
The code usesclassName="styles.card", which is a string literal, not the variable.Step 2: Correct syntax for applying CSS Module class
It should beclassName={styles.card}to use the imported styles object.Final Answer:
The className should use curly braces, not quotes, to apply styles.card. -> Option DQuick Check:
Use curly braces to apply CSS Module classes [OK]
- Using quotes instead of curly braces for className
- Renaming CSS Module file incorrectly
- Thinking CSS Modules require class components
primary and large from Button.module.css. Which is the correct way to apply both classes to a <button> element?Solution
Step 1: Understand how to combine multiple CSS Module classes
Use template literals or string concatenation to combine classes as strings.Step 2: Evaluate each option
Click uses template literals correctly to combine classes with a space.
Click uses a string literal, so classes won't apply.
Click concatenates without space, merging class names incorrectly.
Click passes an array, which React does not accept as className.Final Answer:
<button className={`${styles.primary} ${styles.large}`}>Click</button> -> Option AQuick Check:
Combine CSS Module classes with template literals [OK]
- Using quotes instead of variables for className
- Concatenating without spaces between classes
- Passing arrays directly to className
Ready to prove it?
Pass the test and this pattern moves unknown
