🔥 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)
Performance••By Komal Nakrani

Remove Unused CSS in Astro: Scoped Styles, Tailwind, PurgeCSS

Find and remove unused CSS in Astro: how scoped styles already drop dead CSS, Tailwind content paths, PurgeCSS for global frameworks, and checking coverage.

#Astro#Performance#CSS#Unused CSS

Astro already removes most unused CSS for you: styles in a component’s <style> tag are scoped to that component and only ship on pages that render it. Unused CSS in an Astro site usually comes from global stylesheets, such as a CSS framework or a large global.css, and you fix it by fixing Tailwind’s content paths, running PurgeCSS on the global file, or splitting global styles by layout.

Key Takeaways

  • Component <style> blocks are scoped and bundled per page, so they rarely cause unused CSS.
  • Tailwind CSS only generates classes it finds in your source; wrong content globs cause both missing and extra CSS.
  • For Bootstrap-style global frameworks, add @fullhuman/postcss-purgecss to PostCSS.
  • Use the Chrome DevTools Coverage panel on the built site to see what is actually unused.

Where unused CSS comes from in Astro

Astro’s bundler collects the styles of every component used on a page and emits them as hashed files in dist/_astro/, or inlines small ones (build.inlineStylesheets: 'auto' inlines stylesheets under 4 kB). A component that is not on the page contributes no CSS. So check these first:

  1. A global stylesheet imported in the base layout. Everything in it ships on every page.
  2. A CSS framework loaded as one file. Bootstrap, Bulma or a UI kit’s full CSS.
  3. is:global styles in a shared component. They apply site-wide and ship wherever that component renders.
  4. Tailwind scanning too much. A content glob that includes node_modules or old folders generates classes nobody uses.

Our scoped vs global styles guide explains how each kind of style is bundled.

Measure before you remove anything

Build and preview the production site, since dev mode serves CSS differently:

# Terminal
npm run build && npx astro preview

Open the page in Chrome, press Cmd/Ctrl+Shift+P, run “Show Coverage”, and reload. The panel lists each stylesheet with the share of bytes that went unused on that page. Click through a few page types, since CSS unused on the homepage may be needed on a blog post.

Tailwind CSS: get the content paths right

Tailwind CSS 3 generates only the classes it finds in files matched by content. Point it at your source and nothing else:

// tailwind.config.mjs
export default {
  content: ['./src/**/*.{astro,html,js,jsx,md,mdx,ts,tsx,vue,svelte}'],
  theme: { extend: {} },
  plugins: [],
};

Avoid building class names from strings, such as `text-${color}-600`, because Tailwind cannot see them. Write full class names in a lookup object instead. Tailwind CSS 4 detects sources automatically and ignores files in .gitignore; add extra paths with @source in your CSS if needed. See the Tailwind setup guide for both versions.

PurgeCSS for global CSS frameworks

For a framework loaded as plain CSS, PurgeCSS removes selectors that never appear in your built output. Add it to PostCSS, which Astro runs through Vite automatically:

# Terminal
npm install -D @fullhuman/postcss-purgecss
// postcss.config.mjs
import purgeCSSPlugin from '@fullhuman/postcss-purgecss';

export default {
  plugins: [
    process.env.NODE_ENV === 'production' &&
      purgeCSSPlugin({
        content: ['./src/**/*.{astro,html,md,mdx,js,ts,jsx,tsx}'],
        safelist: {
          standard: [/^is-/, /^has-/],
        },
        defaultExtractor: (content) => content.match(/[\w-/:]+(?<!:)/g) ?? [],
      }),
  ].filter(Boolean),
};

Two cautions. Safelist classes that scripts add at runtime (open, active and similar states), since PurgeCSS only sees source files. And PostCSS also processes your component styles, so check pages in the built site after enabling it; if a scoped style goes missing, add its selector to the safelist.

Split global styles by layout

Often the cleanest fix is structural. Move styles that only the blog needs into the blog layout, and import them there:

---
// src/layouts/BlogPost.astro
import '../styles/prose.css';
import Base from './Base.astro';
---
<Base><slot /></Base>

Now prose.css only ships on pages using that layout. The same idea works for docs pages, landing pages and forms. The styling docs show where global styles live in a Komal theme, and most themes in our catalog pair scoped component styles with Tailwind CSS, which keeps unused CSS low from the start.

FAQ

Does Astro remove unused CSS automatically?

For component styles, effectively yes: scoped <style> blocks only ship on pages that use the component. Astro does not analyze global stylesheets, so a large global.css or framework file ships whole unless you purge it.

Why is my Tailwind CSS file large in Astro?

Usually the content globs match too many files, or a plugin adds many utilities. Check the globs, remove unused plugins, and inspect the built CSS in dist/_astro/.

Is PurgeCSS safe to use with Astro?

Yes, if you safelist classes added by JavaScript at runtime and test the built site. Run it only in production builds so development stays fast.

Does unused CSS affect Core Web Vitals?

Large render-blocking stylesheets delay First Contentful Paint and can delay Largest Contentful Paint. Removing unused CSS helps most on slow mobile connections.