|
| 1 | +--- |
| 2 | +id: tailwind-integration |
| 3 | +title: Tailwind CSS Integration |
| 4 | +--- |
| 5 | + |
| 6 | +_So you want to use Tailwind CSS in your TanStack Start project?_ |
| 7 | + |
| 8 | +This guide will help you use Tailwind CSS in your TanStack Start project. |
| 9 | + |
| 10 | +## Tailwind CSS Version 4 (Latest) |
| 11 | + |
| 12 | +The 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, as TanStack Start uses Vite as its build tool. |
| 13 | + |
| 14 | +### Install Tailwind CSS |
| 15 | + |
| 16 | +Install Tailwind CSS and it's Vite plugin. |
| 17 | + |
| 18 | +```shell |
| 19 | +npm install tailwindcss @tailwindcss/vite |
| 20 | +``` |
| 21 | + |
| 22 | +### Configure The Vite Plugin |
| 23 | + |
| 24 | +Add the `@tailwindcss/vite` plugin to your Vite configuration. |
| 25 | + |
| 26 | +```ts |
| 27 | +// vite.config.ts |
| 28 | +import { defineConfig } from 'vite' |
| 29 | +import tsConfigPaths from 'vite-tsconfig-paths' |
| 30 | +import { tanstackStart } from '@tanstack/solid-start/plugin/vite' |
| 31 | +import tailwindcss from '@tailwindcss/vite' |
| 32 | + |
| 33 | +export default defineConfig({ |
| 34 | + server: { |
| 35 | + port: 3000, |
| 36 | + }, |
| 37 | + plugins: [tsConfigPaths(), tanstackStart(), tailwindcss()], |
| 38 | +}) |
| 39 | +``` |
| 40 | + |
| 41 | +### Import Tailwind in your CSS file |
| 42 | + |
| 43 | +You 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. |
| 44 | + |
| 45 | +```css |
| 46 | +/* src/styles/app.css */ |
| 47 | +@import "tailwindcss"; |
| 48 | +``` |
| 49 | + |
| 50 | +## Import the CSS file in your `__root.tsx` file |
| 51 | + |
| 52 | +Import the CSS file in your `__root.tsx` file with the `?url` query and make sure to add the **triple slash** directive to the top of the file. |
| 53 | + |
| 54 | +```tsx |
| 55 | +// src/routes/__root.tsx |
| 56 | +/// <reference types="vite/client" /> |
| 57 | +// other imports... |
| 58 | + |
| 59 | +import appCss from '../styles/app.css?url' |
| 60 | + |
| 61 | +export const Route = createRootRoute({ |
| 62 | + head: () => ({ |
| 63 | + meta: [ |
| 64 | + // your meta tags and site config |
| 65 | + ], |
| 66 | + links: [ |
| 67 | + { rel: 'stylesheet', href: appCss }, |
| 68 | + ] |
| 69 | + // other head config |
| 70 | + }), |
| 71 | + component: RootComponent, |
| 72 | +}) |
| 73 | +``` |
| 74 | + |
| 75 | +## Use Tailwind CSS anywhere in your project |
| 76 | + |
| 77 | +You can now use Tailwind CSS anywhere in your project. |
| 78 | + |
| 79 | +```tsx |
| 80 | +// src/routes/index.tsx |
| 81 | +import { createFileRoute } from '@tanstack/solid-router' |
| 82 | + |
| 83 | +export const Route = createFileRoute('/')({ |
| 84 | + component: Home, |
| 85 | +}) |
| 86 | + |
| 87 | +function Home() { |
| 88 | + return ( |
| 89 | + <div className="bg-red-500 text-white p-4"> |
| 90 | + Hello World |
| 91 | + </div> |
| 92 | + ) |
| 93 | +} |
| 94 | +``` |
| 95 | + |
| 96 | +That's it! You can now use Tailwind CSS anywhere in your project 🎉. |
| 97 | + |
| 98 | +## Tailwind CSS Version 3 (Legacy) |
| 99 | + |
| 100 | +If you are want to use Tailwind CSS Version 3, you can use the following steps. |
| 101 | + |
| 102 | +### Install Tailwind CSS |
| 103 | + |
| 104 | +Install Tailwind CSS and it's peer dependencies. |
| 105 | + |
| 106 | +```shell |
| 107 | +npm install -D tailwindcss@3 postcss autoprefixer |
| 108 | +``` |
| 109 | + |
| 110 | +Then generate the Tailwind and PostCSS configuration files. |
| 111 | + |
| 112 | +```shell |
| 113 | +npx tailwindcss init -p |
| 114 | +``` |
| 115 | + |
| 116 | +### Configure your template paths |
| 117 | + |
| 118 | +Add the paths to all of your template files in the `tailwind.config.js` file. |
| 119 | + |
| 120 | +```js |
| 121 | +// tailwind.config.js |
| 122 | +/** @type {import('tailwindcss').Config} */ |
| 123 | +export default { |
| 124 | + content: [ |
| 125 | + "./src/**/*.{js,ts,jsx,tsx}", |
| 126 | + ], |
| 127 | + theme: { |
| 128 | + extend: {}, |
| 129 | + }, |
| 130 | + plugins: [], |
| 131 | +} |
| 132 | +``` |
| 133 | + |
| 134 | +### Add the Tailwind directives to your CSS file |
| 135 | + |
| 136 | +Add the `@tailwind` directives for each of Tailwind's layers to your `src/styles/app.css` file. |
| 137 | + |
| 138 | +```css |
| 139 | +/* src/styles/app.css */ |
| 140 | +@tailwind base; |
| 141 | +@tailwind components; |
| 142 | +@tailwind utilities; |
| 143 | +``` |
| 144 | + |
| 145 | +> [!NOTE] |
| 146 | +> 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. |
0 commit comments