Installation
Setting up Tailwind CSS in an Astro project.
Start by creating a new Astro project if you don't have one set up already. The most common approach is to use create astro.
npm create astro@latest my-project
cd my-project
Install @tailwindcss/vite
and its peer dependencies via npm.
npm install tailwindcss @tailwindcss/vite
Add the @tailwindcss/vite
plugin to your Vite plugins in your Astro config file.
// @ts-check
import { defineConfig } from "astro/config";
import tailwindcss from "@tailwindcss/vite";
// https://astro.build/config
export default defineConfig({
vite: {
plugins: [tailwindcss()],
},
});
Create a ./src/styles/global.css
file and add an @import
for Tailwind CSS.
@import "tailwindcss";
Run your build process with npm run dev
.
npm run dev
Start using Tailwind's utility classes to style your content while making sure to import the newly created CSS file.
---
import "../styles/global.css";
---
<h1 class="text-3xl font-bold underline">
Hello world!
</h1>