Astro critical CSS is mostly handled for you: Astro bundles only the styles each page uses, and the build.inlineStylesheets option decides whether that CSS ships in <style> tags or as external files. Set it to 'always' to inline every page’s CSS and remove the render-blocking stylesheet request, or leave the default 'auto', which inlines stylesheets smaller than 4 KB.
Key Takeaways
- Astro already splits CSS per page, so a page only gets the styles of the components it renders.
build.inlineStylesheetsaccepts'always','auto'(default) and'never'.'auto'inlines stylesheets under Vite’sbuild.assetsInlineLimit, 4 KB by default.- Inlining helps first visits; external files help repeat visits through caching.
The option
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
build: {
inlineStylesheets: 'always',
},
});
| Value | What ships | Best for |
|---|---|---|
'auto' (default) |
Small stylesheets inline, larger ones as <link> |
Most sites |
'always' |
All project CSS in <style> tags |
Landing pages, mostly single-visit traffic |
'never' |
All CSS as external files | Sites with many repeat page views per visitor |
Rebuild and view the source of a page in dist/ to see the result. With 'always', there is no stylesheet <link> left in the head.
Tune the auto threshold
'auto' uses Vite’s asset inline limit. Raise it to inline a little more without inlining everything:
// astro.config.mjs
import { defineConfig } from 'astro/config';
export default defineConfig({
vite: {
build: {
assetsInlineLimit: 8192,
},
},
});
The same limit also controls when small images and fonts imported from src/ become data URIs, so raising it affects those too.
How to decide
Inlining removes one round trip before first paint, which helps LCP and First Contentful Paint on slow connections. The cost is that CSS is no longer cached across pages: every HTML response carries its own copy.
- Inline (
'always') when pages are mostly visited once, such as landing pages and blog posts from search, and the per-page CSS is small (Tailwind sites often produce 10 to 30 KB per page before compression). - External (
'never') when visitors browse many pages, such as docs and dashboards, and a shared cached stylesheet saves more than it costs. 'auto'is a sensible middle ground and the right default until you measure otherwise.
Measure with Lighthouse or WebPageTest on a throttled mobile profile before and after. Our LCP guide and Core Web Vitals guide show how to read the results.
Shrink the CSS first
Inlining a large stylesheet only moves the problem. Before tuning, cut what ships:
- Remove unused global CSS; component
<style>blocks are already scoped and per page. Our remove unused CSS tip covers the rest. - Check large third-party CSS imports in your layout.
- Keep Tailwind content detection pointed at your source, so unused classes are not generated.
True above-the-fold extraction
Classic critical CSS tools extract only the rules needed for the first screen, inline those, and load the rest later. Astro does not do this itself. A post-build tool such as Beasties can process the HTML in dist/, but for most Astro sites, per-page CSS plus inlineStylesheets gets close with no extra step. The official configuration reference documents the option. Our Astro themes keep the default; the configuration docs show where to change it.
FAQ
Does Astro inline CSS automatically?
Yes, for small stylesheets. With the default build.inlineStylesheets: 'auto', stylesheets under 4 KB are inlined into <style> tags and larger ones are linked. Set 'always' to inline all of them.
Is inlining CSS good for performance?
It improves first paint on first visits by removing a blocking request. It can hurt repeat visits, because inlined CSS is not cached separately. Measure on your own pages before choosing.
How do I stop Astro from inlining CSS?
Set build.inlineStylesheets: 'never' in astro.config.mjs. Every page then links external, cacheable stylesheets.