DEV Community

Cover image for How to Use Tailwind CSS v4 in Astro
Dipankar Maikap
Dipankar Maikap

Posted on • Edited on • Originally published at dipankarmaikap.com

1

How to Use Tailwind CSS v4 in Astro

Tailwind CSS is a popular utility-first CSS framework that helps you build modern and responsive web designs efficiently. In this tutorial, we'll walk through how to set up Tailwind CSS v4 in an Astro project using the @tailwindcss/vite plugin.

Install Tailwind CSS

To get started, install Tailwind CSS and the @tailwindcss/vite plugin via npm:

npm install tailwindcss @tailwindcss/vite
Enter fullscreen mode Exit fullscreen mode

Configure the Vite Plugin

Next, add the @tailwindcss/vite plugin to your Astro configuration file (astro.config.mjs):

// @ts-check
import { defineConfig } from "astro/config";
import tailwindcss from "@tailwindcss/vite";

// https://astro.build/config
export default defineConfig({
  vite: {
    plugins: [tailwindcss()],
  },
});
Enter fullscreen mode Exit fullscreen mode

Import Tailwind CSS

Create a CSS file (e.g., tailwind.css) in your src directory and import Tailwind CSS by adding the following line:

@import "tailwindcss";
Enter fullscreen mode Exit fullscreen mode

Include Tailwind in Your Astro Project

Make sure to reference the tailwind.css file you created in the previous step within your layout file (layout.astro). This ensures that Tailwind styles are correctly applied to your project. You can include it using the following method:

Import in layout.astro

---
import "../tailwind.css";
---
Enter fullscreen mode Exit fullscreen mode

A complete Layout.astro file may look like this:

---
import "~/tailwind.css";
---

<!doctype html>
<html lang="en" class="light">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width" />
    <link rel="icon" type="image/svg+xml" href="/favicon.svg" />
    <meta name="generator" content={Astro.generator} />
  </head>
  <body>
    <slot />
  </body>
</html>
Enter fullscreen mode Exit fullscreen mode

Start Using Tailwind CSS

Once everything is set up, you can start using Tailwind’s utility classes in your Astro components. For example:

<div class="bg-blue-500 text-white p-4 rounded-lg">
  Welcome to Tailwind CSS in Astro!
</div>
Enter fullscreen mode Exit fullscreen mode

Tailwind CSS v4 Breaking Changes

Tailwind CSS v4 introduces some breaking changes. If you're upgrading from v3, you might notice differences in styling or some features behaving differently. I recommend checking out the official upgrade guide from Tailwind to ensure a smooth transition: Tailwind CSS Upgrade Guide

That's it! You've successfully set up Tailwind CSS v4 in your Astro project. Happy coding! 🚀

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

Sentry image

See why 4M developers consider Sentry, “not bad.”

Fixing code doesn’t have to be the worst part of your day. Learn how Sentry can help.

Learn more