πŸ”₯ 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: Import a Site-Wide Stylesheet Correctly

Add Astro global CSS by importing a stylesheet in your base layout's frontmatter. Where to put it, ordering, is:global, public/ links and fixing styles that don't load.

#Astro#Global CSS#Styling#CSS

To add Astro global CSS, create a stylesheet such as src/styles/global.css and import it in the frontmatter of the layout every page uses: import '../styles/global.css';. Astro bundles and minifies it, adds the <link> (or inlines it if small) on every page that uses the layout, and reloads it instantly in dev.

Key Takeaways

  • Import global CSS in a shared layout, not in each page.
  • Imported CSS is processed by Vite: bundled, minified, hashed and run through PostCSS or Sass if configured.
  • Files in public/ are not processed; link them with a <link> tag only if you need an unbundled file.
  • Import order sets cascade order: global styles first, component styles after.

Import it in the layout

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

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

body {
  margin: 0;
  font-family: var(--font-body);
  color: var(--color-text);
  background: var(--color-bg);
  line-height: 1.6;
}
---
// src/layouts/Layout.astro
import '../styles/global.css';

interface Props { title: string }
const { title } = Astro.props;
---
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1" />
    <title>{title}</title>
  </head>
  <body>
    <slot />
  </body>
</html>

You do not write a <link> tag. Astro adds it to <head> for every page that renders this layout. With the default build.inlineStylesheets: 'auto', small stylesheets are inlined into a <style> tag instead, which saves a request.

Three ways to write global styles

Method Processed by Vite Use for
import './global.css' in frontmatter Yes Resets, tokens, base typography
<style is:global> in a component Yes A few global rules owned by one component
<link rel="stylesheet" href="/x.css"> to public/ No Third-party CSS you must serve as-is

<style is:global> is useful for styling content you do not control, such as Markdown output, inside the component that renders it. For targeted cases, :global() is narrower; see our :global() tip.

Import from a package

Package stylesheets import the same way:

---
// src/layouts/Layout.astro
import '@fontsource-variable/inter';
import '../styles/global.css';
---

Order matters. Later imports win when specificity is equal, so import third-party CSS first and your own global CSS after it. Component <style> blocks load after both.

Styles not loading?

  • Imported in a page, not the layout. Only pages that import it get it. Move the import into the shared layout.
  • Path wrong. Frontmatter imports are relative to the file. From src/layouts/, the path is ../styles/global.css.
  • Linked from src/. <link href="/src/styles/global.css"> works in dev but breaks in the build. Import it instead.
  • Overridden by scoped styles. Scoped selectors carry an extra attribute, so they beat equal global selectors. See our scoped versus global styles guide.

The official styling docs cover each method, and our theme styling docs show where the global stylesheet lives in our Astro themes.

FAQ

Where do I put global CSS in Astro?

Put it in src/styles/ (any folder in src/ works) and import it in your base layout’s frontmatter. Every page using that layout gets the styles.

Can I import CSS in an Astro page instead of the layout?

Yes, but then only that page loads it. Import in the layout for site-wide styles and in a page only for styles that page alone needs.

What is the difference between is:global and importing a CSS file?

Both produce unscoped CSS processed by Vite. A CSS file is easier to share and organize; <style is:global> keeps a few global rules next to the component that needs them.