Bird
Raised Fist0
NextJSframework~10 mins

CSS Modules in NextJS - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - CSS Modules
Write CSS in .module.css file
↓
Next.js compiles CSS Module
↓
Generates unique class names
↓
Import CSS Module in React component
↓
Apply className={styles.className}
↓
Styles scoped locally, no global conflicts
CSS Modules let you write CSS that is local to a component by generating unique class names, avoiding style conflicts.
Execution Sample
NextJS
/* styles.module.css */
.title {
  color: blue;
}

// Component.jsx
import styles from './styles.module.css';

export default function Title() {
  return <h1 className={styles.title}>Hello</h1>;
}
This code imports a CSS Module and applies a locally scoped class to an h1 element.
Execution Table
StepActionInput/CodeOutput/Result
1Read CSS filestyles.module.css with .title { color: blue; }CSS parsed, ready for processing
2Compile CSS ModuleNext.js compiles CSS ModuleGenerates unique class name, e.g. title_xyz123
3Import CSS Moduleimport styles from './styles.module.css'styles = { title: 'title_xyz123' }
4Render component<h1 className={styles.title}>Hello</h1><h1 class="title_xyz123">Hello</h1> rendered
5Apply stylesclass="title_xyz123"Text color is blue, scoped locally
6ExitComponent rendered with scoped stylesNo global style conflicts
💡 Component rendered with locally scoped CSS class, preventing style conflicts
Variable Tracker
VariableStartAfter ImportAfter RenderFinal
stylesundefined{ title: 'title_xyz123' }{ title: 'title_xyz123' }{ title: 'title_xyz123' }
classNameundefinedundefined'title_xyz123''title_xyz123'
Key Moments - 3 Insights
Why does the class name in the HTML look different from the CSS file?
Next.js compiles the CSS Module and generates a unique class name (like title_xyz123) to keep styles local, as shown in execution_table step 2 and 4.
Can styles from CSS Modules affect other components?
No, because CSS Modules generate unique class names scoped to the component, preventing global conflicts (see execution_table step 5).
What happens if I use a normal CSS file instead of a .module.css file?
Normal CSS files apply styles globally, so class names are not changed and can cause conflicts, unlike CSS Modules which scope styles locally.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of styles after import?
A{ title: 'title_xyz123' }
B'title'
Cundefined
Dnull
💡 Hint
Check execution_table row 3 under Output/Result column
At which step does Next.js generate the unique class name?
AStep 1
BStep 2
CStep 4
DStep 5
💡 Hint
Look at execution_table step describing compilation and unique class name generation
If you change the CSS file to normal .css (not .module.css), what changes in the execution?
AComponent fails to render
BUnique class names are still generated
CClass names remain unchanged and styles apply globally
DStyles become scoped automatically
💡 Hint
Refer to key_moments about difference between CSS Modules and normal CSS
Concept Snapshot
CSS Modules in Next.js:
- Write CSS in .module.css files
- Next.js compiles and generates unique class names
- Import styles object in component
- Use className={styles.className} to apply scoped styles
- Prevents global CSS conflicts
- Styles apply only to the component
Full Transcript
CSS Modules let you write CSS that is local to a component by generating unique class names. In Next.js, you write CSS in a .module.css file. Next.js compiles this file and creates unique class names to avoid conflicts. You import the styles object in your React component and apply classes using className={styles.className}. This ensures styles only affect that component and do not leak globally. The execution steps show reading the CSS file, compiling it, importing styles, rendering the component with the unique class name, and applying the scoped styles. This approach helps keep your styles organized and conflict-free.

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

  1. Step 1: Understand CSS Modules purpose

    CSS Modules scope CSS locally to components, so styles don't leak or conflict.
  2. 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.
  3. Final Answer:

    They scope CSS styles locally to components, avoiding conflicts. -> Option C
  4. 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

  1. Step 1: Identify correct file extension for CSS Modules

    Next.js requires CSS Modules files to end with .module.css.
  2. Step 2: Check import syntax

    Importing with import styles from './Button.module.css'; correctly imports the module as an object.
  3. Final Answer:

    import styles from './Button.module.css'; -> Option B
  4. 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

  1. Step 1: Check CSS Module style for .title

    The CSS sets .title color to red.
  2. Step 2: Confirm component uses styles.title as className

    The component applies className={styles.title}, so the red color applies.
  3. Final Answer:

    Red -> Option A
  4. 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

  1. Step 1: Identify how className is assigned

    The code uses className="styles.card", which is a string literal, not the variable.
  2. Step 2: Correct syntax for applying CSS Module class

    It should be className={styles.card} to use the imported styles object.
  3. Final Answer:

    The className should use curly braces, not quotes, to apply styles.card. -> Option D
  4. 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

  1. Step 1: Understand how to combine multiple CSS Module classes

    Use template literals or string concatenation to combine classes as strings.
  2. 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.
  3. Final Answer:

    <button className={`${styles.primary} ${styles.large}`}>Click</button> -> Option A
  4. 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 unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard