Skip to content
Source
Sign inSign up
TanStack/router
Open History to see the last commit

tailwind-integration.md

207 lines4.5 KB

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 },
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>
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: [],
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.