CSS Modules help keep styles organized and avoid conflicts by making CSS local to components.
CSS Modules support in Astro
import styles from './Component.module.css'; --- <div class={styles.className}>Content</div>
Import the CSS file as a module using import styles from './file.module.css'.
Use the imported styles object to apply class names with class={styles.className}.
primary to a button.import styles from './Button.module.css'; --- <button class={styles.primary}>Click me</button>
import styles from './Card.module.css'; --- <section class={styles.card}> <h2 class={styles.title}>Title</h2> </section>
import styles from './Alert.module.css'; --- <div class={`${styles.alert} ${styles.warning}`}>Warning message</div>
This Astro component imports a CSS module and applies local styles to a message box and its text.
import styles from './Message.module.css'; --- <div class={styles.message}> <p class={styles.text}>Hello from CSS Modules!</p> </div>
CSS Modules automatically generate unique class names to avoid conflicts.
Use the .module.css file extension to enable CSS Modules in Astro.
Inspect the rendered HTML in browser DevTools to see the generated unique class names.
CSS Modules keep styles local to components, preventing conflicts.
Import CSS files as modules and use the imported object to apply classes.
Combine multiple classes by joining them in a string if needed.
Practice
CSS Modules in Astro components?Solution
Step 1: Understand CSS Modules purpose
CSS Modules scope styles locally to components to prevent style conflicts.Step 2: Compare options
Only They keep styles local to components, avoiding conflicts. correctly describes this main benefit; others describe unrelated features.Final Answer:
They keep styles local to components, avoiding conflicts. -> Option AQuick Check:
CSS Modules = Local styles [OK]
- Thinking CSS Modules minify CSS automatically
- Confusing CSS Modules with inline styles
- Assuming CSS Modules create global styles
styles.module.css in an Astro component?Solution
Step 1: Identify correct import syntax for CSS Modules
CSS Modules require importing the CSS file as an object, typically withimport styles from './filename.module.css';.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.Final Answer:
import styles from './styles.module.css'; -> Option DQuick Check:
Import CSS Modules as objects with .module.css [OK]
- Forgetting to assign import to a variable
- Using require instead of import
- Importing non-module CSS files as modules
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>?Solution
Step 1: Understand CSS Modules class naming
CSS Modules transform class names to unique strings likebutton_primary__abc123to avoid conflicts.Step 2: Analyze the rendered class attribute
Usingstyles.primaryapplies the transformed unique class name, not the original 'primary'. So the class attribute will be a unique hashed string.Final Answer:
class="button_primary__abc123" -> Option AQuick Check:
CSS Modules generate unique class names [OK]
- Expecting original class name without transformation
- Using the variable name as class string
- Getting undefined if import is wrong
import styles from './card.module.css';
export default function Card() {
return <div class="styles.card">Content</div>;
}Solution
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}.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.Final Answer:
The class attribute should use curly braces, not quotes. -> Option BQuick Check:
Use curly braces to apply CSS Module classes [OK]
- Using quotes instead of curly braces for class binding
- Forgetting .module.css in import
- Thinking Astro requires class components
styles.primary and styles.rounded to a single element in an Astro component?Solution
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.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.Final Answer:
<div class={`${styles.primary} ${styles.rounded}`}>Content</div> -> Option CQuick Check:
Join CSS Module classes with space in template literals [OK]
- Using quotes instead of variables for class names
- Concatenating without spaces
- Using commas inside class attribute
Ready to prove it?
Pass the test and this pattern moves unknown
