A CSS Module is a CSS file where class and animation names are scoped locally by default. In Next.js, it helps avoid style conflicts by generating unique class names for each component.
Click to reveal answer
beginner
How do you import a CSS Module in a Next.js component?
You import it like a JavaScript object using: <code>import styles from './Component.module.css'</code>. Then you use the styles as <code>className={styles.className}</code> in your JSX.
Click to reveal answer
intermediate
Why use CSS Modules instead of global CSS?
CSS Modules prevent style conflicts by scoping styles locally to components. This means styles won’t accidentally affect other parts of the app, making maintenance easier and safer.
Click to reveal answer
intermediate
How does Next.js generate unique class names with CSS Modules?
Next.js automatically appends a unique hash to class names from CSS Modules. For example, <code>.button</code> might become <code>button_xyz123</code>, ensuring styles are local and don’t clash.
Click to reveal answer
intermediate
Can you use CSS Modules with global styles in Next.js?
Yes, you can use global CSS files for styles that apply everywhere, and CSS Modules for component-specific styles. Global CSS must be imported only in _app.js or _app.tsx.
Click to reveal answer
How do you apply a CSS Module class named 'header' to a div in Next.js?
A<div className={styles.header}></div>
B<div class='header'></div>
C<div className='header'></div>
D<div style={styles.header}></div>
✗ Incorrect
You must use the imported styles object and set className to styles.header to apply the CSS Module class.
What file extension is used for CSS Modules in Next.js?
A.module.css
B.css
C.scss
D.cssmodule
✗ Incorrect
CSS Modules in Next.js use the '.module.css' extension to distinguish them from global CSS files.
Where should you import global CSS files in a Next.js project?
AIn the page components
BOnly in _app.js or _app.tsx
CIn any component
DIn CSS Modules
✗ Incorrect
Global CSS files must be imported only in the custom App component (_app.js or _app.tsx) in Next.js.
What is the main benefit of using CSS Modules?
AAutomatically minifies CSS
BFaster CSS loading
CSupports inline styles
DScoped styles to avoid conflicts
✗ Incorrect
CSS Modules scope styles locally to components, preventing style conflicts across the app.
If you have a class named 'button' in a CSS Module, what will Next.js do?
AUse 'button' as is globally
BIgnore the class
CGenerate a unique class name like 'button_xyz123'
DConvert it to inline styles
✗ Incorrect
Next.js generates unique class names by appending a hash to avoid conflicts.
Explain how CSS Modules help manage styles in a Next.js project.
Think about how styles stay inside components without affecting others.
You got /4 concepts.
Describe the difference between global CSS and CSS Modules in Next.js and when to use each.
Consider the scope and where you import the styles.
You got /4 concepts.
Practice
(1/5)
1. What is the main benefit of using CSS Modules in a Next.js project?
easy
A. They enable global CSS styles to override component styles.
B. They automatically minify CSS files for faster loading.
C. They scope CSS styles locally to components, avoiding conflicts.
D. They allow CSS to be written inside JavaScript files only.
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 C
Quick Check:
CSS Modules = Local scoped styles [OK]
Hint: Remember: CSS Modules isolate styles per component [OK]
Common Mistakes:
Thinking CSS Modules minify CSS automatically
Confusing CSS Modules with CSS-in-JS
Believing CSS Modules create global styles
2. Which of the following is the correct way to import a CSS Module file named Button.module.css in a Next.js component?
easy
A. import styles from './Button.css';
B. import styles from './Button.module.css';
C. import './Button.module.css';
D. import styles from './Button.cssmodule';
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 with import styles from './Button.module.css'; correctly imports the module as an object.
Final Answer:
import styles from './Button.module.css'; -> Option B
Quick Check:
Import CSS Modules with .module.css and assign to variable [OK]
Hint: Always use .module.css extension and import with 'from' [OK]
Common Mistakes:
Using wrong file extension like .css or .cssmodule
Importing without assigning to a variable
Forgetting the .module part in filename
3. Given the CSS Module 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?
medium
A. Red
B. Black (default)
C. Blue
D. Green
Solution
Step 1: Check CSS Module style for .title
The CSS sets .title color to red.
Step 2: Confirm component uses styles.title as className
The component applies className={styles.title}, so the red color applies.
Final Answer:
Red -> Option A
Quick Check:
CSS Module class applied = red text [OK]
Hint: ClassName from styles object applies CSS Module styles [OK]
Common Mistakes:
Assuming default black color without checking CSS
Confusing CSS Modules with global CSS
Not using styles.title but a string instead
4. What is wrong with this Next.js component using CSS Modules?
import styles from './Card.module.css';
export default function Card() {
return <div className="styles.card">Content</div>
}
medium
A. The component must be a class component to use CSS Modules.
B. The CSS file should be imported with a different name.
C. The CSS Module file must be named Card.css, not Card.module.css.
D. The className should use curly braces, not quotes, to apply styles.card.
Solution
Step 1: Identify how className is assigned
The code uses className="styles.card", which is a string literal, not the variable.
Step 2: Correct syntax for applying CSS Module class
It should be className={styles.card} to use the imported styles object.
Final Answer:
The className should use curly braces, not quotes, to apply styles.card. -> Option D
Quick Check:
Use curly braces to apply CSS Module classes [OK]
Hint: Use {styles.className}, not "styles.className" for CSS Modules [OK]
Common Mistakes:
Using quotes instead of curly braces for className
Renaming CSS Module file incorrectly
Thinking CSS Modules require class components
5. You want to style a Next.js component with two CSS Module classes: primary and large from Button.module.css. Which is the correct way to apply both classes to a <button> element?
hard
A. Click
B. Click
C. Click
D. Click
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 A
Quick Check:
Combine CSS Module classes with template literals [OK]
Hint: Use backticks and ${} to combine multiple CSS Module classes [OK]
Common Mistakes:
Using quotes instead of variables for className
Concatenating without spaces between classes
Passing arrays directly to className
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.