Add Tailwind in Svelte
Set up Tailwind CSS in a Svelte project.
Tailwind CSS is a utility-first CSS framework that allows you to style your application directly in your markup using predefined utility classes.
In SvelteKit, Tailwind CSS can be integrated using the official @tailwindcss/vite plugin.
This plugin integrates Tailwind directly into Vite’s build pipeline, eliminating the need
for a separate PostCSS configuration.
There are two ways to install Tailwind CSS in a SvelteKit project:
1. Using the Svelte CLI (Recommended)
Run the following command from your project directory:
npx sv add tailwindcss The CLI automatically configures Tailwind CSS for your project, including:
- Installing the required dependencies.
- Configuring the official @tailwindcss/vite plugin.
- Creating or updating
layout.cssand+layout.svelte. - Integration with prettier (if installed).
2. Manual Installation
If you prefer to configure the integration yourself, follow these steps.
Step 1: Install Tailwind CSS
Install Tailwind and its Vite plugin using your preferred package manager.
pnpm add -D tailwindcss @tailwindcss/vite The two packages serve different purposes:
- tailwindcss: Provides the Tailwind CSS framework.
- @tailwindcss/vite: Integrates Tailwind CSS into Vite’s build process.
Step 2: Configure the Vite Plugin
Open vite.config.ts and register the Tailwind plugin alongside the SvelteKit plugin.
+ import tailwindcss from '@tailwindcss/vite';
import adapter from '@sveltejs/adapter-vercel';
import { sveltekit } from '@sveltejs/kit/vite';
import { defineConfig } from 'vite';
export default defineConfig({
plugins: [
+ tailwindcss(),
sveltekit({
compilerOptions: {
// Force runes mode for the project, except for libraries. Can be removed in svelte 6.
runes: ({ filename }) =>
filename.split(/[/\]/).includes('node_modules') ? undefined : true
},
adapter: adapter()
})
]
}); The tailwindcss() plugin processes Tailwind CSS during development and production builds.
Step 3: Import Tailwind CSS
Create a global CSS file at src/routes/layout.css and add the following import:
@import "tailwindcss"; This single import loads Tailwind CSS, including its theme, preflight (CSS reset), and utility styles.
Step 4: Import the Global Stylesheet
Open the root layout file at src/routes/+layout.svelte and import the global stylesheet.
import "./layout.css"; The root layout is shared across your application’s routes. Importing the stylesheet here ensures that Tailwind’s styles are available throughout the application.
3. Verify the Installation
Start the development server:
pnpm run dev If the content appears visibly styled, Tailwind CSS is working correctly.
Bonus : Installing Tailwind Typography
Tailwind CSS Typography is an official Tailwind CSS plugin that provides a collection of prose utility classes for styling long-form, text-heavy content.
It is particularly useful when rendering content such as:
- Blog posts and articles.
- Markdown and MDX content.
- Documentation pages.
- Rich text generated by a CMS.
- Dynamically rendered HTML content.
There are two ways to install the plugin in a SvelteKit project.
Option 1 : Using the Svelte CLI (Recommended)
If you are using the Svelte CLI, you can add the Typography plugin with:
npx sv add tailwindcss="plugins:typography" The CLI handles the plugin installation and configuration automatically.
Option 2 : Manual Setup
If Tailwind CSS is already installed in your project, you can install and register the Typography plugin manually.
Step 1: Install the plugin
Install @tailwindcss/typography as a development dependency:
pnpm install -D @tailwindcss/typography Step 2: Register the plugin
Register the plugin. Open your global CSS file i.e src/layout.css and
register the plugin using Tailwind’s @plugin directive.
@import "tailwindcss";
+ @plugin "@tailwindcss/typography"; The @plugin directive loads the Typography plugin and makes its utilities available throughout your application.
> cd ..