Bird
Raised Fist0
Astroframework~20 mins

CSS Modules support in Astro - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
CSS Modules Mastery in Astro
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ component_behavior
intermediate
2:00remaining
How does CSS Modules scope styles in Astro components?

Consider an Astro component using CSS Modules. What happens to the CSS class names when the component is rendered in the browser?

Astro
<style module>
.button {
  color: red;
}
</style>

<button class={styles.button}>Click me</button>
AThe class name is replaced with a unique hashed name to avoid conflicts.
BThe class name remains exactly as 'button' in the browser DOM.
CThe class name is removed and styles are applied inline instead.
DThe class name is prefixed with the component file name but otherwise unchanged.
Attempts:
2 left
💡 Hint

Think about how CSS Modules prevent style conflicts by changing class names.

📝 Syntax
intermediate
2:00remaining
Correct syntax to import CSS Modules in Astro

Which of the following is the correct way to import and use a CSS Module file named Button.module.css in an Astro component?

A
import './Button.module.css';
&lt;button class='button'&gt;Click&lt;/button&gt;
B
import styles from './Button.module.css';
&lt;button class={styles.button}&gt;Click&lt;/button&gt;
C
const styles = require('./Button.module.css');
&lt;button class={styles.button}&gt;Click&lt;/button&gt;
D
&lt;style module src='./Button.module.css'&gt;&lt;/style&gt;
&lt;button class={styles.button}&gt;Click&lt;/button&gt;
Attempts:
2 left
💡 Hint

Astro supports ES module imports for CSS Modules.

🔧 Debug
advanced
2:00remaining
Why does the CSS Module class not apply styles?

Given this Astro component code, the styles from the CSS Module are not applied. What is the most likely cause?

---
import styles from './Card.module.css';
---

Card content
AThe class attribute should use curly braces: <code>class={styles.card}</code> instead of quotes.
BThe CSS Module file must be named <code>Card.css</code> without <code>.module</code>.
CThe import statement should use require() instead of import.
DThe styles object is undefined because CSS Modules are not supported in Astro.
Attempts:
2 left
💡 Hint

Check how dynamic class names are passed in JSX-like syntax.

❓ state_output
advanced
2:00remaining
What is the rendered class attribute value?

Given this Astro component using CSS Modules, what will be the exact value of the class attribute in the rendered HTML?

---
import styles from './Alert.module.css';
---

Warning!

Assume the original CSS class is alert and Astro generates a hash _Alert_alert_1a2b3.

A"styles.alert"
B"Alert_alert"
C"alert"
D"_Alert_alert_1a2b3"
Attempts:
2 left
💡 Hint

CSS Modules replace class names with unique hashed names.

🧠 Conceptual
expert
2:00remaining
Why use CSS Modules in Astro instead of global CSS?

Which of the following is the best explanation for why CSS Modules are preferred over global CSS in Astro projects?

ACSS Modules load styles faster because they are compiled to inline styles in the browser.
BGlobal CSS cannot be used in Astro projects because Astro does not support CSS files.
CCSS Modules automatically scope styles locally, preventing unintended style conflicts across components.
DCSS Modules allow writing CSS in JavaScript syntax, making styles easier to write.
Attempts:
2 left
💡 Hint

Think about how styles can accidentally affect other parts of a website.

Practice

(1/5)
1. What is the main benefit of using CSS Modules in Astro components?
easy
A. They keep styles local to components, avoiding conflicts.
B. They automatically minify CSS for faster loading.
C. They allow inline styles directly in HTML tags.
D. They enable global styles to override component styles.

Solution

  1. Step 1: Understand CSS Modules purpose

    CSS Modules scope styles locally to components to prevent style conflicts.
  2. Step 2: Compare options

    Only They keep styles local to components, avoiding conflicts. correctly describes this main benefit; others describe unrelated features.
  3. Final Answer:

    They keep styles local to components, avoiding conflicts. -> Option A
  4. Quick Check:

    CSS Modules = Local styles [OK]
Hint: Remember CSS Modules isolate styles to components [OK]
Common Mistakes:
  • Thinking CSS Modules minify CSS automatically
  • Confusing CSS Modules with inline styles
  • Assuming CSS Modules create global styles
2. Which is the correct way to import a CSS Module file named styles.module.css in an Astro component?
easy
A. import './styles.module.css';
B. import styles from './styles.css';
C. const styles = require('./styles.module.css');
D. import styles from './styles.module.css';

Solution

  1. Step 1: Identify correct import syntax for CSS Modules

    CSS Modules require importing the CSS file as an object, typically with import styles from './filename.module.css';.
  2. Step 2: Check file extension and import style

    import styles from './styles.module.css'; uses the correct file extension and import syntax. import './styles.module.css'; imports without assigning, C uses CommonJS which is not standard in Astro, and D imports a non-module CSS file.
  3. Final Answer:

    import styles from './styles.module.css'; -> Option D
  4. Quick Check:

    Import CSS Modules as objects with .module.css [OK]
Hint: Use import with .module.css and assign to a variable [OK]
Common Mistakes:
  • Forgetting to assign import to a variable
  • Using require instead of import
  • Importing non-module CSS files as modules
3. Given the CSS Module import import styles from './button.module.css'; and the CSS file containing .primary { color: blue; }, what will be the rendered class attribute for <button class={styles.primary}>Click</button>?
medium
A. class="button_primary__abc123"
B. class="primary"
C. class="styles.primary"
D. class="undefined"

Solution

  1. Step 1: Understand CSS Modules class naming

    CSS Modules transform class names to unique strings like button_primary__abc123 to avoid conflicts.
  2. Step 2: Analyze the rendered class attribute

    Using styles.primary applies the transformed unique class name, not the original 'primary'. So the class attribute will be a unique hashed string.
  3. Final Answer:

    class="button_primary__abc123" -> Option A
  4. Quick Check:

    CSS Modules generate unique class names [OK]
Hint: CSS Modules rename classes to unique strings [OK]
Common Mistakes:
  • Expecting original class name without transformation
  • Using the variable name as class string
  • Getting undefined if import is wrong
4. What is wrong with this Astro component code using CSS Modules?
import styles from './card.module.css';

export default function Card() {
  return <div class="styles.card">Content</div>;
}
medium
A. The component must be a class, not a function.
B. The class attribute should use curly braces, not quotes.
C. The import statement is missing the .module.css extension.
D. CSS Modules cannot be used in Astro components.

Solution

  1. Step 1: Check how class names are applied with CSS Modules

    CSS Modules require using curly braces to insert the imported class object value, e.g., class={styles.card}.
  2. Step 2: Identify the error in the code

    The code uses quotes around "styles.card", which treats it as a string literal, not the variable. This causes the class to be literally 'styles.card' instead of the hashed class.
  3. Final Answer:

    The class attribute should use curly braces, not quotes. -> Option B
  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 class binding
  • Forgetting .module.css in import
  • Thinking Astro requires class components
5. How can you apply multiple CSS Module classes styles.primary and styles.rounded to a single element in an Astro component?
hard
A. <div class={styles.primary, styles.rounded}>Content</div>
B. <div class="styles.primary styles.rounded">Content</div>
C. <div class={`${styles.primary} ${styles.rounded}`}>Content</div>
D. <div class={styles.primary + styles.rounded}>Content</div>

Solution

  1. Step 1: Understand combining multiple CSS Module classes

    To apply multiple classes, you join their values as strings separated by spaces, typically using template literals or string concatenation.
  2. Step 2: Analyze each option

    <div class={`${styles.primary} ${styles.rounded}`}>Content</div> correctly uses template literals to combine classes with a space. <div class="styles.primary styles.rounded">Content</div> treats class names as literal strings, not variables. <div class={styles.primary + styles.rounded}>Content</div> concatenates without space, merging class names incorrectly. <div class={styles.primary, styles.rounded}>Content</div> uses comma which is invalid syntax.
  3. Final Answer:

    <div class={`${styles.primary} ${styles.rounded}`}>Content</div> -> Option C
  4. Quick Check:

    Join CSS Module classes with space in template literals [OK]
Hint: Use template literals with spaces to combine CSS Module classes [OK]
Common Mistakes:
  • Using quotes instead of variables for class names
  • Concatenating without spaces
  • Using commas inside class attribute
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