Astro CSS modules need no setup: name a stylesheet something.module.css, import it in a component’s frontmatter, and use the returned object’s keys as class names. Vite, which Astro builds on, rewrites each class to a unique hashed name, so styles stay local to the component that imports them.
Key Takeaways
- Any file ending in
.module.css(or.module.scsswith Sass installed) is a CSS module.- Import it as an object:
import styles from './Card.module.css', thenclass={styles.card}.- In
.astrofiles, scoped<style>blocks usually do the same job with less ceremony.- CSS modules shine when you share one stylesheet between Astro and React, Preact or Vue islands.
Use a CSS module in an Astro component
/* src/components/Card.module.css */
.card {
border: 1px solid var(--border, #e2e8f0);
border-radius: 0.75rem;
padding: 1.5rem;
}
.title {
font-size: 1.25rem;
margin-block-end: 0.5rem;
}
.featured {
border-color: var(--accent, #0f766e);
}
---
// src/components/Card.astro
import styles from './Card.module.css';
interface Props {
title: string;
featured?: boolean;
}
const { title, featured = false } = Astro.props;
---
<article class:list={[styles.card, featured && styles.featured]}>
<h2 class={styles.title}>{title}</h2>
<slot />
</article>
In the build, styles.card becomes something like _card_1x2y3_1 and the CSS file is bundled with the page. class:list combines classes and drops falsy values, which keeps modifier logic tidy.
CSS modules or scoped styles?
Scoped <style> in .astro |
CSS module | |
|---|---|---|
| Setup | None, write CSS in the component | Separate .module.css file |
| Usage in markup | Plain class names | styles.name lookups |
| Works in framework islands | No, Astro components only | Yes, React, Preact, Solid, Vue, Svelte |
| Style a slotted child | :global() needed |
Pass the class name down |
| Type safety | Not needed | Possible with a typed declaration file |
For pure Astro components, scoped styles are simpler; our scoped versus global styles guide covers them. Pick CSS modules when a React or Preact island and an Astro wrapper should share the same styles:
// src/components/Counter.tsx
import { useState } from 'react';
import styles from './Card.module.css';
export default function Counter() {
const [n, setN] = useState(0);
return (
<div className={styles.card}>
<button onClick={() => setN(n + 1)}>Clicked {n} times</button>
</div>
);
}
Naming and composition
Class names with dashes become bracket lookups (styles['card-title']). To use camelCase keys for dashed names, set Vite’s option:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
vite: {
css: {
modules: { localsConvention: 'camelCaseOnly' },
},
},
});
CSS modules also support composes to reuse a class from another module:
/* src/components/Button.module.css */
.primary {
composes: base from './shared.module.css';
background: var(--accent, #0f766e);
color: white;
}
Global styles still apply
CSS modules only localize class selectors. Element selectors such as h2 { } inside a module are still global, so keep modules to classes. Site-wide resets and tokens belong in a global stylesheet; see our global stylesheet tip. The Vite CSS modules docs list every option, and the theme styling docs explain how our Astro themes organize their styles.
FAQ
Does Astro support CSS modules?
Yes, through Vite. Any file named *.module.css imported into a component returns an object of hashed class names. No integration or config is required.
Can I use CSS modules with Tailwind in Astro?
Yes. They coexist without conflict. Use Tailwind utilities in markup and a CSS module for component styles that are hard to express as utilities, or @apply inside the module if your setup supports it.
Are Astro scoped styles the same as CSS modules?
They solve the same problem differently. Scoped styles add a unique attribute to elements and selectors at build time, while CSS modules rename the classes themselves. Scoped styles only work in .astro files; CSS modules work in any framework component.