🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Styling••By Komal Nakrani

Astro Global CSS vs Scoped Styles: When to Use Each

Astro global CSS vs scoped styles: how scoping works, when to use is:global, :global() and imported stylesheets, and how to style child components and Markdown.

#Astro#CSS#Global CSS#Scoped Styles#Styling

For Astro global CSS, import a stylesheet in your base layout (import '../styles/global.css';) or mark a <style> block with is:global. Everything else should use Astro’s default scoped styles: a plain <style> block in a component only affects that component’s own markup, so class names never collide across the site.

Most real projects use both. This guide explains how scoping works under the hood, the three ways to write global styles, how to reach into child components and rendered Markdown, and how the cascade decides which rule wins.

Key Takeaways

  • A plain <style> in an .astro file is scoped to that component’s template.
  • Global CSS comes from an imported .css file, <style is:global>, or :global() on part of a selector.
  • Scoped styles do not reach into child components; pass a class prop or use :global() under a scoped parent.
  • Scoped selectors get +1 specificity from a data-astro-cid-* attribute by default.
  • Put resets, tokens and typography in one global file; put everything else in components.

How scoped styles work in Astro

When Astro compiles a component, it adds a unique attribute to every HTML element in that component’s template and rewrites the selectors in its <style> block to match. You write this:

---
// src/components/Card.astro
const { title } = Astro.props;
---
<article class="card">
  <h2>{title}</h2>
  <slot />
</article>

<style>
  .card { padding: 1.5rem; border-radius: 1rem; background: var(--surface); }
  h2 { font-size: 1.25rem; margin: 0 0 0.5rem; }
</style>

And Astro outputs CSS roughly like this:

/* generated */
.card[data-astro-cid-x7k2m1] { padding: 1.5rem; border-radius: 1rem; background: var(--surface); }
h2[data-astro-cid-x7k2m1] { font-size: 1.25rem; margin: 0 0 0.5rem; }

The h2 rule only matches headings inside Card.astro, so you can write short element selectors without a naming convention like BEM. The CSS is extracted and bundled at build time, and Astro only includes it on pages that render the component.

The strategy is configurable with scopedStyleStrategy in astro.config.mjs. The default is 'attribute'. The other values are 'class' (scoping with a class, same +1 specificity) and 'where' (wrapping the scope in :where(), adding zero specificity). Most projects never change it.

Three ways to write Astro global CSS

Global rules apply to every matching element on the page, whatever component it came from. Astro gives you three ways to write them.

1. Import a stylesheet

This is the most common and the easiest to maintain:

/* src/styles/global.css */
:root {
  --surface: #ffffff;
  --text: #0f172a;
  --font-sans: system-ui, sans-serif;
}

*, *::before, *::after { box-sizing: border-box; }

body {
  margin: 0;
  font-family: var(--font-sans);
  color: var(--text);
  line-height: 1.6;
}

a { color: inherit; text-underline-offset: 0.2em; }
---
// src/layouts/BaseLayout.astro
import '../styles/global.css';
---
<html lang="en">
  <body><slot /></body>
</html>

An import in frontmatter is processed by Vite, bundled with the rest of the page’s CSS, and added to every page that uses the layout. You can import from node_modules the same way, for example a font package or a syntax highlighting theme.

2. A style block with is:global

---
// src/layouts/BaseLayout.astro
---
<style is:global>
  html { scroll-behavior: smooth; }
  ::selection { background: #fde68a; }
</style>

is:global switches scoping off for the whole block. It is convenient for a handful of rules that belong to one layout, but a separate .css file is easier to find later.

3. The:global() selector

:global() makes part of a selector unscoped while the rest stays scoped. That is the tool for styling content you do not render directly:

---
// src/layouts/PostLayout.astro
---
<article class="post">
  <slot />
</article>

<style>
  .post :global(h2) { margin-top: 2.5rem; }
  .post :global(pre) { padding: 1rem; border-radius: 0.5rem; overflow-x: auto; }
  .post :global(a:hover) { text-decoration-thickness: 2px; }
</style>

Here .post stays scoped to the layout, but the h2, pre and a inside it match anything, including HTML generated from Markdown or MDX. This is the cleanest way to style article content without leaking those rules to the rest of the site.

Styling child components

Scoped styles stop at component boundaries. If Page.astro renders <Button />, a .button rule in Page.astro does not reach the button’s markup, because the button’s elements carry the button’s scope attribute, not the page’s.

There are two good fixes. The first is to let the child accept a class prop and apply it:

---
// src/components/Button.astro
interface Props {
  href: string;
  class?: string;
}
const { href, class: className, ...rest } = Astro.props;
---
<a href={href} class:list={['button', className]} {...rest}><slot /></a>

<style>
  .button { display: inline-block; padding: 0.6rem 1rem; border-radius: 0.5rem; }
</style>
---
// src/pages/index.astro
import Button from '../components/Button.astro';
---
<Button href="/contact/" class="hero-cta">Get in touch</Button>

<style>
  .hero-cta { background: #111827; color: white; }
</style>

When the page passes class="hero-cta", Astro also passes the page’s scope along as a data-astro-cid-* prop. Spreading ...rest onto the anchor applies it, so the page’s scoped .hero-cta rule matches the anchor inside Button.astro. Forget the spread and the class arrives but the scoped rule never matches. The child stays in control of its base styles, and the parent adds context-specific ones.

The second fix is :global() under a scoped parent, as in the post layout example. Use it when you cannot change the child, such as a third-party component.

Slotted content is the opposite case. Markup passed into a <slot /> is rendered by the parent, so it carries the parent’s scope. Style it in the parent, not in the component that receives it. The components guide covers props and slots in depth.

Specificity and the cascade

Scoping adds specificity, which matters when global and scoped rules target the same element. With the default 'attribute' strategy:

Selector Where Specificity
h2 global.css 0,0,1
h2[data-astro-cid-…] scoped 0,1,1
.post h2 global.css 0,1,1
.title[data-astro-cid-…] scoped 0,2,0

So a scoped h2 beats a global h2. That is usually what you want: a component’s own styles override site defaults. When it is not, you have a few options:

  • Raise the global selector’s specificity on purpose, or move the rule into the component.
  • Wrap global resets in a cascade layer (@layer reset { … }). Unlayered styles, including scoped component styles, always win over layered ones, whatever the specificity.
  • Set scopedStyleStrategy: 'where' if you want scoped styles to add no specificity at all, then manage order with layers.

Avoid !important. In a component-based project, it starts a race that nobody wins.

Passing values into styles

Scoped styles are static CSS, but sometimes a value comes from props. Astro’s define:vars turns frontmatter values into CSS custom properties for that block:

---
// src/components/Badge.astro
const { color = '#2563eb' } = Astro.props;
---
<span class="badge"><slot /></span>

<style define:vars={{ color }}>
  .badge { background: var(--color); color: white; padding: 0.2rem 0.5rem; border-radius: 999px; }
</style>

The define:vars tip explains its limits, including why it makes <script> tags inline. For site-wide values, plain custom properties in global.css are simpler, and design tokens with CSS variables shows how to organise them.

Where Astro puts the CSS

At build time Astro collects the CSS each page needs, global and scoped, and either links it as a .css file or inlines it into a <style> tag in the head. The build.inlineStylesheets option controls this. The default, 'auto', inlines small stylesheets below Vite’s assetsInlineLimit and links larger ones. Set it to 'always' or 'never' if you need predictable output for caching.

Scoped styles for components that never appear on a page are left out of that page. Global CSS imported in a shared layout lands on every page, so keep it small: tokens, resets, base typography. Large global files are a common reason a fast Astro site ships more CSS than it needs.

A practical split for real projects

A simple three-layer split holds up in most projects:

  1. Global file: custom properties, reset, body and heading typography, link defaults, focus styles, and utility classes such as .visually-hidden.
  2. Layout-scoped with :global(): styles for Markdown and MDX content inside an article wrapper.
  3. Component-scoped: everything else. Cards, buttons, navigation, footers, forms.

If you use Tailwind CSS, the same split applies: Tailwind’s base layer acts as your global file, and utility classes replace most component styles. The Tailwind CSS in Astro guide shows how @apply and scoped blocks fit together, and the Astro styling guide compares every approach. For where these files sit in a theme, see the styling docs.

A good way to learn this split is to open a finished project and trace where each rule lives; any of the Astro themes in the catalog is a full site you can read through. The official Astro styling guide on docs.astro.build is the reference for every directive mentioned here.

FAQ

How do I add global CSS in Astro?

Create a stylesheet such as src/styles/global.css and import it in the frontmatter of your base layout with import '../styles/global.css';. Every page using that layout gets the styles.

Are styles in Astro components scoped by default?

Yes. A plain <style> block in an .astro file only applies to elements in that component’s template. Add is:global to the block, or wrap part of a selector in :global(), to opt out.

Why don’t my Astro styles apply to a child component?

Scoped styles stop at component boundaries. Pass a class prop to the child and apply it with class:list, or use :global() under a scoped parent selector such as .wrapper :global(.child).

How do I style Markdown content in Astro?

Wrap the rendered content in an element with a class, then use :global() for the inner elements: .post :global(h2). The wrapper stays scoped while the generated headings, lists and code blocks get styled.

Should I use is:global or a separate CSS file?

Use a separate CSS file for anything site-wide; it is easier to find and reuse. Keep is:global for a few rules that only make sense next to one layout.