Installation
Setting up Tailwind CSS in a SvelteKit project.
Start by creating a new SvelteKit project if you don't have one set up already. The most common approach is outlined in the SvelteKit documentation.
npx sv create my-projectcd my-project
Install @tailwindcss/vite
and its peer dependencies via npm.
npm install tailwindcss @tailwindcss/vite
Add the @tailwindcss/vite
plugin to your Vite configuration.
import { sveltekit } from '@sveltejs/kit/vite';import { defineConfig } from 'vite';import tailwindcss from '@tailwindcss/vite';export default defineConfig({ plugins: [ tailwindcss(), sveltekit(), ],});
Create a ./src/app.css
file and add an @import
that imports Tailwind CSS.
@import "tailwindcss";
Create a ./src/routes/+layout.svelte
file and import the newly-created app.css
file.
<script> let {children} = $props(); import "../app.css";</script>{@render children()}
Run your build process with npm run dev
.
npm run dev
Start using Tailwind’s utility classes to style your content, making sure to import your Tailwind CSS theme for any <style>
blocks that need to be processed by Tailwind.
<h1 class="text-3xl font-bold underline"> Hello world!</h1><style> @reference "tailwindcss/theme"; :global(html) { background-color: theme(--color-gray-100); }</style>