The Astro :global() selector lets a component’s scoped styles reach elements it does not render itself, such as child components, slotted content or Markdown output. Write the scoped part first and wrap the rest: .prose :global(h2) { ... } styles every h2 inside this component’s .prose element, and nothing outside it.
Key Takeaways
- Astro scopes
<style>rules by adding adata-astro-cid-*attribute to selectors and to the elements the component renders.- Elements rendered by a child component or by Markdown do not get the parent’s attribute, so scoped rules miss them.
:global()removes scoping from part of a selector; keep a scoped ancestor in front so the rule stays local.- For a whole block of global rules, use
<style is:global>.
Why scoped styles miss children
---
// src/pages/post.astro
import Card from '../components/Card.astro';
---
<section class="list">
<Card title="One" />
</section>
<style>
.list h3 { color: teal; } /* does not reach the h3 inside Card */
</style>
Astro compiles .list h3 to roughly .list[data-astro-cid-abc] h3[data-astro-cid-abc]. The h3 belongs to Card, which carries its own attribute, so the rule never matches.
Reach in with:global()
<style>
.list :global(h3) {
color: teal;
}
</style>
Now it compiles to .list[data-astro-cid-abc] h3. The .list part stays scoped to this page, so only h3 elements inside this page’s .list are affected.
Style Markdown or MDX content
The most common use is content rendered by <Content />:
---
// src/layouts/PostLayout.astro
---
<article class="post">
<slot />
</article>
<style>
.post :global(h2) { margin-block: 2.5rem 1rem; }
.post :global(a) { text-decoration-thickness: 1px; text-underline-offset: 3px; }
.post :global(pre) { padding: 1rem; border-radius: 0.5rem; overflow-x: auto; }
.post :global(img) { max-width: 100%; height: auto; }
</style>
With Tailwind, the Typography plugin does the same job with one class; see our Tailwind Typography tip.
Slotted content
Content passed into a slot is rendered by the parent that passes it, so it carries the parent’s scope, not yours. Use :global() in the receiving component:
---
// src/components/Panel.astro
---
<div class="panel"><slot /></div>
<style>
.panel > :global(* + *) { margin-block-start: 1rem; }
</style>
Prefer props or classes when you can
Reaching into a child couples the parent to the child’s markup. If you own the child, pass a class instead, which keeps the parent’s scope:
---
// src/components/Card.astro
const { title, class: className, ...rest } = Astro.props;
---
<div class:list={['card', className]} {...rest}><h3>{title}</h3></div>
<Card title="One" class="highlight" />
<style>
.highlight { border-color: teal; }
</style>
Astro passes the parent’s scope along with the props (as a data-astro-cid-* attribute with the default strategy), so spread the remaining props onto the root element and the parent’s .highlight rule matches. Our scoped versus global styles guide covers this in depth, and the official scoped styles docs explain the compiled output. Components in our Astro themes follow the class-prop pattern, as described in the theme styling docs.
FAQ
How do I style a child component in Astro?
Use :global() for the child’s elements behind a scoped ancestor, for example .wrapper :global(h3). If you own the child, accepting a class prop is cleaner.
What does:global() do in Astro?
It tells the compiler not to add the scope attribute to the part of the selector inside the parentheses, so that part matches elements from any component.
Is:global() the same as is:global?
No. :global() unscopes part of one selector and keeps the rest scoped. <style is:global> makes every rule in the block global.