Open History to see the last commit
1---
2id: tailwind-integration
3title: Tailwind CSS Integration
4---
5β
6_So you want to use Tailwind CSS in your TanStack Start project?_
7β
8This guide will help you use Tailwind CSS in your TanStack Start project.
9β
10## Tailwind CSS Version 4 (Latest)
11β
12The latest version of Tailwind CSS is 4. And it has some configuration changes that majorly differ from Tailwind CSS Version 3. It's **easier and recommended** to set up Tailwind CSS Version 4 in a TanStack Start project with either Vite or Rsbuild.
13β
14### Install Tailwind CSS
15β
16Install Tailwind CSS and the integration for your build tool.
17β
18<!-- ::start:tabs variant="bundler" -->
19β
20# Vite
21β
22```shell
23npm install tailwindcss @tailwindcss/vite
24```
25β
26# Rsbuild
27β
28```shell
29npm install tailwindcss @tailwindcss/postcss postcss
30```
31β
32<!-- ::end:tabs -->
33β
34### Configure Tailwind CSS
35β
36Add Tailwind CSS to your build tool configuration.
37β
38<!-- ::start:tabs variant="bundler" -->
39β
40# Vite
41β
42```ts title="vite.config.ts"
43import { defineConfig } from 'vite'
44import tsConfigPaths from 'vite-tsconfig-paths'
45import { tanstackStart } from '@tanstack/react-start/plugin/vite'
46import tailwindcss from '@tailwindcss/vite'
47import viteReact from '@vitejs/plugin-react'
48β
49export default defineConfig({
50 server: {
51 port: 3000,
52 },
53 plugins: [tsConfigPaths(), tanstackStart(), viteReact(), tailwindcss()],
54})
55```
56β
57# Rsbuild
58β
59```ts title="rsbuild.config.ts"
60import { defineConfig } from '@rsbuild/core'
61import { pluginReact } from '@rsbuild/plugin-react'
62import { tanstackStart } from '@tanstack/react-start/plugin/rsbuild'
63β
64export default defineConfig({
65 server: {
66 port: 3000,
67 },
68 plugins: [pluginReact(), tanstackStart()],
69})
70```
71β
72```js title="postcss.config.mjs"
73export default {
74 plugins: {
75 '@tailwindcss/postcss': {},
76 },
77}
78```
79β
80<!-- ::end:tabs -->
81β
82### Import Tailwind in your CSS file
83β
84You need to create a CSS file to configure Tailwind CSS instead of the configuration file in version 4. You can do this by creating a `src/styles/app.css` file or name it whatever you want.
85β
86```css
87/* src/styles/app.css */
88@import 'tailwindcss' source('../');
89```
90β
91## Import the CSS file in your `__root.tsx` file
92β
93Import the CSS file in your `__root.tsx` file.
94β
95<!-- ::start:tabs variant="bundler" -->
96β
97# Vite
98β
99Use the `?url` query and make sure to add the **triple slash** directive to the top of the file.
100β
101```tsx
102// src/routes/__root.tsx
103/// <reference types="vite/client" />
104// other imports...
105β
106import appCss from '../styles/app.css?url'
107β
108export const Route = createRootRoute({
109 head: () => ({
110 meta: [
111 // your meta tags and site config
112 ],
113 links: [{ rel: 'stylesheet', href: appCss }],
114 // other head config
115 }),
116 component: RootComponent,
117})
118```
119β
120# Rsbuild
121β
122Use a side-effect import so Start can discover the generated stylesheet as a route asset.
123β
124```tsx
125// src/routes/__root.tsx
126import { createRootRoute } from '@tanstack/react-router'
127import '../styles/app.css'
128β
129export const Route = createRootRoute({
130 head: () => ({
131 meta: [
132 // your meta tags and site config
133 ],
134 // other head config
135 }),
136 component: RootComponent,
137})
138```
139β
140<!-- ::end:tabs -->
141β
142## Use Tailwind CSS anywhere in your project
143β
144You can now use Tailwind CSS anywhere in your project.
145β
146```tsx
147// src/routes/index.tsx
148import { createFileRoute } from '@tanstack/react-router'
149β
150export const Route = createFileRoute('/')({
151 component: Home,
152})
153β
154function Home() {
155 return <div className="bg-red-500 text-white p-4">Hello World</div>
156}
157```
158β
159That's it! You can now use Tailwind CSS anywhere in your project π.
160β
161## Tailwind CSS Version 3 (Legacy)
162β
163If you are want to use Tailwind CSS Version 3, you can use the following steps.
164β
165### Install Tailwind CSS
166β
167Install Tailwind CSS and it's peer dependencies.
168β
169```shell
170npm install -D tailwindcss@3 postcss autoprefixer
171```
172β
173Then generate the Tailwind and PostCSS configuration files.
174β
175```shell
176npx tailwindcss init -p
177```
178β
179### Configure your template paths
180β
181Add the paths to all of your template files in the `tailwind.config.js` file.
182β
183```js
184// tailwind.config.js
185/** @type {import('tailwindcss').Config} */
186export default {
187 content: ['./src/**/*.{js,ts,jsx,tsx}'],
188 theme: {
189 extend: {},
190 },
191 plugins: [],
192}
193```
194β
195### Add the Tailwind directives to your CSS file
196β
197Add the `@tailwind` directives for each of Tailwind's layers to your `src/styles/app.css` file.
198β
199```css
200/* src/styles/app.css */
201@tailwind base;
202@tailwind components;
203@tailwind utilities;
204```
205β
206> [!NOTE]
207> Jump to [Import the CSS file in your `__root.tsx` file](#import-the-css-file-in-your-__roottsx-file) to see how to import the CSS file in your `__root.tsx` file.