To set up Astro Tailwind 4, run npx astro add tailwind. It installs tailwindcss and @tailwindcss/vite, registers the Vite plugin in astro.config.mjs, and creates src/styles/global.css with @import "tailwindcss";. Import that file in your layout and every utility class works.
Key Takeaways
- Tailwind 4 runs as a Vite plugin; the old
@astrojs/tailwindintegration is deprecated.- There is no
tailwind.config.jsby default. Tokens live in an@themeblock in CSS.- Content detection is automatic, so there is no
contentarray to maintain.- Upgrade Tailwind 3 projects with
npx @tailwindcss/upgradeon a clean branch.
Install
npx astro add tailwind
The resulting config:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
vite: {
plugins: [tailwindcss()],
},
});
Then import the stylesheet once, in the layout every page uses:
---
// src/layouts/Layout.astro
import '../styles/global.css';
---
<html lang="en">
<body class="bg-white text-slate-900 antialiased">
<slot />
</body>
</html>
Configure your theme in CSS
Tailwind 4 is CSS-first. Define tokens as variables in @theme and Tailwind generates utilities from them:
/* src/styles/global.css */
@import 'tailwindcss';
@theme {
--color-brand: #0f766e;
--color-brand-dark: #115e59;
--font-sans: 'Inter Variable', ui-sans-serif, system-ui, sans-serif;
--breakpoint-3xl: 112rem;
}
Now bg-brand, text-brand-dark, font-sans and 3xl: work. Because tokens are real CSS variables, you can also use them in scoped styles: color: var(--color-brand);.
Plugins load from CSS too:
@plugin '@tailwindcss/typography';
@custom-variant dark (&:where(.dark, .dark *));
Our Tailwind Typography tip and dark mode tip cover those two lines.
Using utilities in scoped styles
@apply inside an Astro <style> block needs a reference to your theme, because each block is processed separately:
<style>
@reference '../styles/global.css';
.card { @apply rounded-xl border p-6; }
</style>
Without @reference, Tailwind cannot resolve your custom tokens in that block.
Upgrading from Tailwind 3
- Commit everything, then run
npx @tailwindcss/upgrade. It convertstailwind.config.*into@themeCSS and renames changed utilities. - Remove
@astrojs/tailwindfromintegrationsand add the Vite plugin as above. - Delete
tailwindcssfrompostcss.config.*if present. - Compare key pages visually; a few defaults changed, such as border color and ring width.
Tailwind 3 still works in Astro 7, so upgrade when it suits you. Our Tailwind CSS setup guide goes deeper, and the official Tailwind upgrade guide lists every change. Almost all of our Astro themes use Tailwind CSS, and the theme styling docs show how their tokens are organized.
FAQ
Does Astro support Tailwind CSS 4?
Yes. npx astro add tailwind sets up Tailwind 4 with the @tailwindcss/vite plugin. No Astro integration is needed.
Where is tailwind.config.js in Tailwind 4?
There is none by default. Configure tokens in an @theme block in your CSS. If you need a JavaScript config for a plugin, load it with @config './tailwind.config.mjs';.
Why do my custom colors not work with @apply in Astro?
Each <style> block is compiled on its own. Add @reference pointing at your main stylesheet at the top of the block so Tailwind can see your @theme tokens.