Installation
Setting up Tailwind CSS in a TanStack Start project.
Start by creating a new TanStack Start project by following the Build a Project from Scratch guide on the TanStack Start website.
mkdir myAppcd myAppnpm init -y# Follow instructions in the "Build a Project from Scratch" guide
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 Vite config file.
import { defineConfig } from 'vite';import tsConfigPaths from 'vite-tsconfig-paths';import { tanstackStart } from '@tanstack/react-start/plugin/vite';// ...import tailwindcss from '@tailwindcss/vite'export default defineConfig({ plugins: [ tailwindcss(), tsConfigPaths(), tanstackStart(), ],});
Create a ./src/styles/app.css
file and add an @import
for Tailwind CSS.
@import "tailwindcss";
Add the triple-slash directive on top of the file and import the CSS file with the right query parameter.
/// <reference types="vite/client" />import type { ReactNode } from 'react'import { createRootRoute, Outlet } from '@tanstack/react-router'import { Meta, Scripts } from '@tanstack/start'import appCss from '../styles/app.css?url'export const Route = createRootRoute({ head: () => ({ meta: [ // ... ], links: [ { rel: 'stylesheet', href: appCss, }, ], }), component: RootComponent,})
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 { createFileRoute } from '@tanstack/react-router'export const Route = createFileRoute('/')({ component: Home,})function Home() { return ( <h1 class="text-3xl font-bold underline"> Hello World! </h1> )}