๐Ÿ”ฅ 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 Tailwind 4 Setup: Vite Plugin, Theme and Upgrade

Set up Astro Tailwind 4 in one command: the @tailwindcss/vite plugin, a CSS-first @theme config, where to import it, and how to move a Tailwind 3 project over.

#Astro#Tailwind CSS#Tailwind 4#Styling

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/tailwind integration is deprecated.
  • There is no tailwind.config.js by default. Tokens live in an @theme block in CSS.
  • Content detection is automatic, so there is no content array to maintain.
  • Upgrade Tailwind 3 projects with npx @tailwindcss/upgrade on 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

  1. Commit everything, then run npx @tailwindcss/upgrade. It converts tailwind.config.* into @theme CSS and renames changed utilities.
  2. Remove @astrojs/tailwind from integrations and add the Vite plugin as above.
  3. Delete tailwindcss from postcss.config.* if present.
  4. 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.