To add Tailwind CSS to Astro, run npx astro add tailwind. The command installs Tailwind CSS 4 and its @tailwindcss/vite plugin, registers the plugin in astro.config.mjs, and creates src/styles/global.css with @import "tailwindcss";. Import that file in your base layout and every utility class works in .astro, MDX and framework components.
The rest of this guide covers the manual setup, theming with @theme, dark mode, using @apply inside Astro <style> blocks, class:list, plugins, and how to handle projects still on Tailwind CSS 3.
Key Takeaways
npx astro add tailwindis the supported setup for Tailwind CSS 4 in Astro 7.- Tailwind 4 runs as a Vite plugin; the old
@astrojs/tailwindintegration is deprecated.- Design tokens live in CSS with
@theme, not intailwind.config.js.- Inside a component
<style>block, add@referencebefore using@apply.- Tailwind CSS 3 projects keep working; upgrade when you have time to test, not mid-project.
Install Tailwind CSS in Astro
The one-command setup is the fastest path and the one Astro maintains:
npx astro add tailwind
It asks before changing files, then leaves your config looking like this:
// astro.config.mjs
import { defineConfig } from 'astro/config';
import tailwindcss from '@tailwindcss/vite';
export default defineConfig({
vite: {
plugins: [tailwindcss()],
},
});
And it creates the stylesheet:
/* src/styles/global.css */
@import "tailwindcss";
Import the stylesheet once in the layout every page uses:
---
// src/layouts/BaseLayout.astro
import '../styles/global.css';
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 class="bg-white text-slate-900 antialiased">
<slot />
</body>
</html>
Run npm run dev, add a class like text-3xl font-bold to any element, and it should apply immediately. If it does not, the layout is not importing global.css, or the page is not using that layout.
Manual setup, step by step
If you prefer to see every step, or astro add cannot edit an unusual config file, do it by hand:
- Install the packages:
npm install tailwindcss @tailwindcss/vite. - Import
tailwindcssfrom@tailwindcss/viteinastro.config.mjsand addtailwindcss()tovite.plugins. - Create
src/styles/global.csscontaining@import "tailwindcss";. - Import
../styles/global.cssin your base layout’s frontmatter.
There is no content array to maintain in Tailwind 4. It scans your project for class names automatically and skips files in .gitignore. If classes come from a package in node_modules, register that path with @source in global.css.
The official Tailwind integration guide and Tailwind’s Astro install page both document this exact setup.
Theme tokens with @theme
Tailwind 4 moves configuration into CSS. Brand colors, fonts and spacing go in an @theme block, and each variable generates matching utilities:
/* src/styles/global.css */
@import "tailwindcss";
@theme {
--color-brand-50: oklch(0.97 0.02 260);
--color-brand-500: oklch(0.55 0.2 260);
--color-brand-900: oklch(0.28 0.1 260);
--font-sans: "Inter Variable", ui-sans-serif, system-ui, sans-serif;
--font-display: "Fraunces Variable", ui-serif, Georgia, serif;
--radius-card: 1rem;
}
You now have bg-brand-500, text-brand-900, font-display and rounded-card. Because these are real CSS custom properties, you can also read them in plain CSS with var(--color-brand-500), which keeps Tailwind classes and hand-written styles on the same palette. The design tokens with CSS variables guide goes further into naming and layering tokens.
If you load fonts through Astro’s Fonts API, point the Tailwind font variable at the CSS variable you registered there. The Astro fonts guide shows that wiring.
Dark mode with Tailwind in Astro
By default the dark: variant follows the operating system’s prefers-color-scheme. For a user-controlled toggle, switch it to a class on the root element:
/* src/styles/global.css */
@import "tailwindcss";
@custom-variant dark (&:where(.dark, .dark *));
Now dark:bg-slate-950 applies whenever <html class="dark"> is set. Setting that class before the first paint is what prevents the white flash on load, and it needs a small inline script in the head. The full pattern, including View Transitions, is in dark mode in Astro without a flash.
Using @apply in Astro style blocks
Astro compiles each component’s <style> block separately. In Tailwind 4, those blocks do not know about your theme unless you reference the main stylesheet:
---
// src/components/Button.astro
const { href } = Astro.props;
---
<a href={href} class="btn"><slot /></a>
<style>
@reference "../styles/global.css";
.btn {
@apply inline-flex items-center gap-2 rounded-card bg-brand-500 px-4 py-2 font-semibold text-white;
}
.btn:hover {
@apply bg-brand-900;
}
</style>
@reference imports the theme for lookups without emitting Tailwind’s CSS a second time. Without it, @apply fails with an unknown utility error for any custom token.
Use @apply sparingly. Astro components already give you a reusable unit, so a Button.astro with utility classes in its markup is usually clearer than a .btn class built from @apply. Keep @apply for cases where you cannot control the markup, such as styling HTML rendered from Markdown.
Conditional classes with class:list
Astro’s class:list directive takes strings, arrays and objects and joins the truthy ones. It pairs well with utility classes:
---
// src/components/NavLink.astro
interface Props {
href: string;
}
const { href } = Astro.props;
const isActive = Astro.url.pathname === href;
---
<a
href={href}
class:list={[
'rounded-md px-3 py-2 text-sm font-medium',
{ 'bg-brand-50 text-brand-900': isActive, 'text-slate-600 hover:text-slate-900': !isActive },
]}
aria-current={isActive ? 'page' : undefined}
>
<slot />
</a>
When a parent passes extra classes, accept a class prop and append it inside class:list. Name the destructured variable something else, because class is a reserved word: const { class: className } = Astro.props;.
Styling Markdown and MDX content
Utility classes cannot reach elements that Markdown generates. The official typography plugin solves this with a single prose class:
npm install @tailwindcss/typography
/* src/styles/global.css */
@import "tailwindcss";
@plugin "@tailwindcss/typography";
---
// src/layouts/PostLayout.astro
---
<article class="prose prose-slate lg:prose-lg dark:prose-invert mx-auto">
<slot />
</article>
Tailwind 4 loads plugins with @plugin in CSS rather than a plugins array in a JavaScript config. If you need finer control, scope selectors under the article, for example .prose :where(pre), in a global stylesheet.
Best practices that keep Tailwind maintainable
A few habits keep a Tailwind Astro project readable as it grows:
- Extract components, not classes. When a cluster of utilities repeats, make an
.astrocomponent. It keeps markup and styles together and needs no extra CSS. - Keep tokens in one place. Colors and fonts belong in
@theme. Arbitrary values likebg-[#1e40af]scattered through templates are hard to change later. - Sort classes automatically.
prettier-plugin-tailwindcsssorts classes in a consistent order and works alongsideprettier-plugin-astro. - Do not build class names from strings. Tailwind detects classes by scanning source text, so
`text-${color}-500`is never generated. Map props to full class names instead. - Mix in scoped styles when they are clearer. Complex animations or
:has()selectors are often easier to read in a<style>block. The scoped vs global styles guide explains how the two coexist.
The broader trade-offs between Tailwind, scoped CSS and Sass are covered in which styling approach to use in Astro, and the styling docs show where styles live in a theme.
Tailwind CSS 3 projects
Many existing Astro projects, including most Komal themes, use Tailwind CSS 3 with a tailwind.config.mjs file. Those projects still build and run on Astro 7. You do not need to migrate to keep shipping.
When you do upgrade, Tailwind’s official npx @tailwindcss/upgrade tool converts the config to @theme, updates renamed utilities, and rewrites imports. Replace the deprecated @astrojs/tailwind integration with the @tailwindcss/vite plugin at the same time, then check every page visually, because a few default values such as border colors and ring widths changed between versions. Do the upgrade on a branch, compare screenshots, and merge once nothing has shifted.
If you would rather start from a finished Tailwind-styled site than wire this up yourself, browse Astro themes and edit the tokens to match your brand.
FAQ
Do I still need @astrojs/tailwind in Astro 7?
No. The @astrojs/tailwind integration is deprecated. Tailwind CSS 4 runs as a Vite plugin, @tailwindcss/vite, which npx astro add tailwind installs and registers for you.
Where is tailwind.config.js in Tailwind 4?
There isn’t one by default. Tailwind 4 reads configuration from CSS: theme tokens go in an @theme block and plugins load with @plugin, both in src/styles/global.css.
Why are my Tailwind classes not working in Astro?
The most common causes are a layout that does not import global.css, a page that does not use that layout, or class names built from template strings, which Tailwind cannot detect. Check the import first.
Can I use @apply in an Astro component?
Yes. Add @reference "../styles/global.css"; at the top of the component’s <style> block so Tailwind knows your theme, then use @apply as usual.
Does Tailwind CSS add JavaScript to an Astro site?
No. Tailwind generates CSS at build time and ships only the classes you use. Astro still sends zero client JavaScript unless a component opts in with a client:* directive.