Design and frontend · Beginner · 18 min
How to Set Up Tailwind CSS 4 with Vite
By Padding · Updated 15 Nov 2026
Tailwind CSS 4.3 · Vite 8 · Node.js 26
You will add Tailwind CSS 4.3 to a new Vite 8 project with its official Vite plugin, style a card with utility classes, add your own brand colour with @theme, use the dark and responsive variants, and check in the production build that only the classes you use are generated.
On this page
Tailwind CSS is a styling toolkit made of small single-purpose classes such as text-3xl, font-bold and bg-slate-900. You compose them in your markup instead of writing a stylesheet by hand. It generates only the CSS for the classes it finds in your files, so the stylesheet you ship stays small.
Version 4 changed the setup compared with older tutorials you may find. There is no tailwind.config.js and no postcss.config.js to start with, and no @tailwind directives. You install a Vite plugin and add one line to your CSS. This tutorial follows the current installation guide for Vite, and checks each claim about the output by reading what the build produces.
Tailwind 4 is built for modern browsers: Chrome 111, Safari 16.4 and Firefox 128 or later. If you must support older ones, stay on Tailwind 3. Tailwind's documentation also says it is not designed to be used with Sass, Less or Stylus. It handles imports, variables and nesting itself.
Step 1 · Create a Vite project and install Tailwind
create-vite scaffolds a project. The vanilla template is plain JavaScript with no framework, and --no-interactive skips the questions, so the command is repeatable. Then install the dependencies, and add the two Tailwind packages: tailwindcss itself and @tailwindcss/vite, the plugin that connects it to Vite:
cd ~
npm create vite@9 tw-demo -- --template vanilla --no-interactive
cd tw-demo
npm install
npm install tailwindcss @tailwindcss/viteThe double dash passes the options on to create-vite and not to npm. The last command saves both packages in package.json.
Check it worked
npm ls tailwindcss @tailwindcss/vite viteThe tree lists tailwindcss@4.3. and @tailwindcss/vite@4.3. with the same release number, and vite@8. as a dependency of the project.
Step 2 · Register the plugin and import Tailwind
The vanilla template has no Vite configuration file. Create vite.config.js with the Tailwind plugin:
cat > vite.config.js <<'JS'
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'
export default defineConfig({
plugins: [tailwindcss()],
})
JSThe next two commands replace the starter's own stylesheet and script, which draw a counter you do not need. Overwriting them is intended. In src/style.css the one line @import "tailwindcss"; loads Tailwind's base styles, theme and utilities. src/main.js imports that stylesheet, which is how Vite includes CSS in a page:
cat > src/style.css <<'CSS'
@import "tailwindcss";
CSS
cat > src/main.js <<'JS'
import './style.css'
JSIf you are following an older guide that tells you to run npx tailwindcss init or to write @tailwind base;, that is Tailwind 3. Version 4 does not need either.
Step 3 · Style a card with utility classes
Replace index.html with a page that uses utility classes only, with no CSS of your own. Each class does one thing: rounded-xl rounds corners, p-6 adds padding of 1.5rem, text-slate-600 sets a text colour, and hover:bg-sky-700 applies a colour on hover:
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tailwind demo</title>
</head>
<body class="min-h-screen bg-slate-100 p-6 text-slate-900">
<main class="mx-auto max-w-md">
<article class="rounded-xl bg-white p-6 shadow-md">
<h1 class="text-3xl font-bold tracking-tight">Plan comparison</h1>
<p class="mt-2 text-slate-600">Pick the plan that fits how you work.</p>
<button class="mt-6 rounded-lg bg-sky-600 px-4 py-2 font-semibold text-white hover:bg-sky-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-sky-600">
Choose a plan
</button>
</article>
</main>
<script type="module" src="/src/main.js"></script>
</body>
</html>
HTMLStart the development server. It rebuilds the CSS when you save a file:
npm run devCheck it worked
Open http://localhost:5173/ in a browser. You see a white card with rounded corners and a shadow on a light grey page, a bold heading, grey text and a blue button that darkens when you point at it. From another terminal you can also ask the server for the stylesheet and check that it contains one of your classes:
curl -s -H 'Accept: text/css' http://127.0.0.1:5173/src/style.css | grep '\.text-3xl' > /dev/null && echo "text-3xl is generated"The output is text-3xl is generated.
Troubleshooting
If the page is unstyled, check three things in order: src/main.js imports ./style.css, index.html loads /src/main.js, and vite.config.js calls tailwindcss() inside plugins. If the build log shows Cannot find module '@tailwindcss/vite', run npm install again from the project folder. Editing a class and seeing nothing change usually means the dev server was started from a different folder.
Step 4 · Add your own colours with @theme
Tailwind's design tokens, such as colours, fonts and spacing, are CSS variables declared in a @theme block. Add your own and Tailwind makes matching utility classes. Variables named --color-* become colour utilities such as bg-brand-600 and text-brand-600:
cat >> src/style.css <<'CSS'
@theme {
--color-brand-600: #5b3df5;
--color-brand-700: #4a2fd1;
}
CSSUse the new colour in the button, replacing the sky-blue classes. Write the page again with the three changed class names:
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tailwind demo</title>
</head>
<body class="min-h-screen bg-slate-100 p-6 text-slate-900">
<main class="mx-auto max-w-md">
<article class="rounded-xl bg-white p-6 shadow-md">
<h1 class="text-3xl font-bold tracking-tight">Plan comparison</h1>
<p class="mt-2 text-slate-600">Pick the plan that fits how you work.</p>
<button class="mt-6 rounded-lg bg-brand-600 px-4 py-2 font-semibold text-white hover:bg-brand-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-600">
Choose a plan
</button>
</article>
</main>
<script type="module" src="/src/main.js"></script>
</body>
</html>
HTML
grep -c 'brand-' index.htmlCheck it worked
The grep prints 1, because the whole button tag is on one line. Reload the browser: the button is now violet. Because the tokens are real CSS variables, they are also available in your own CSS as var(--color-brand-600).
Step 5 · Use variants for dark mode, screen size and state
A variant is a prefix that applies a class only under a condition. hover: and focus-visible: you have used. Two more matter for nearly every site. md: applies from the medium breakpoint, 48rem wide, up. dark: applies when the visitor's system is set to a dark appearance, because Tailwind 4 uses the prefers-color-scheme media query by default. Write the page again with a dark background for dark systems and a larger heading from the medium breakpoint up:
cat > index.html <<'HTML'
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<title>Tailwind demo</title>
</head>
<body class="min-h-screen bg-slate-100 p-6 text-slate-900 dark:bg-slate-900 dark:text-slate-100">
<main class="mx-auto max-w-md">
<article class="rounded-xl bg-white p-6 shadow-md">
<h1 class="text-3xl font-bold tracking-tight md:text-5xl">Plan comparison</h1>
<p class="mt-2 text-slate-600">Pick the plan that fits how you work.</p>
<button class="mt-6 rounded-lg bg-brand-600 px-4 py-2 font-semibold text-white hover:bg-brand-700 focus-visible:outline-2 focus-visible:outline-offset-2 focus-visible:outline-brand-600">
Choose a plan
</button>
</article>
</main>
<script type="module" src="/src/main.js"></script>
</body>
</html>
HTML
grep -c 'dark:bg-slate-900' index.htmlVariants do not need a configuration: they change only the @media or selector around the generated rule. To switch dark mode with a button instead of the system setting, declare your own variant in the CSS with @custom-variant dark (&:where(.dark, .dark *));, then put a dark class on the <html> element. See the Tailwind dark mode documentation for the full pattern.
Check it worked
The grep prints 1. Make the browser window wider than 768 pixels and the heading grows to a larger size. Switch your system to a dark appearance and the page turns dark, and the card still looks right because it carries its own white background. Add dark:bg-slate-800 to the <article> if you want the card to follow. In Chrome's DevTools you can test dark mode without changing the system, as the dark mode tutorial in this section explains.
Step 6 · Build for production and see what Tailwind generated
Run the production build and look at the CSS it wrote:
npm run build
ls -l dist/assetsThe build writes hashed files under dist/assets, one .css and one .js. The stylesheet is a few kilobytes, because Tailwind generated rules only for classes it found. It scans your project's source files for strings that look like class names, with no list to maintain. Check that the classes from this tutorial are in it, and one you never used is not:
CSS="$(ls dist/assets/*.css)"
grep -q 'text-3xl' "$CSS" && echo "text-3xl: present"
grep -q 'bg-brand-600' "$CSS" && echo "bg-brand-600: present"
grep -q 'prefers-color-scheme:dark' "$CSS" && echo "dark variant: present"
grep -q 'width>=48rem' "$CSS" && echo "md variant: present"
grep -q 'text-9xl' "$CSS" || echo "text-9xl: absent"Check it worked
The five lines of output are text-3xl: present, bg-brand-600: present, dark variant: present, md variant: present and text-9xl: absent. The output CSS is minified, so the media queries are written without spaces: the md: variant is @media (width>=48rem), and the dark variant is @media (prefers-color-scheme:dark).
Step 7 · Avoid building class names from pieces
Tailwind finds classes by scanning your files as plain text. It does not run your code. A class name you assemble at runtime is invisible to it. See this with a class you have never written in full:
cat > src/main.js <<'JS'
import './style.css'
const colour = 'red'
document.body.dataset.badge = `bg-${colour}-500`
JS
npm run build > /dev/null
grep -q 'bg-red-500' dist/assets/*.css || echo "bg-red-500: not generated"The output is bg-red-500: not generated. The scanner saw bg- and -500 and no complete class. Write the whole name somewhere in a source file, even in a comment, and it is generated:
cat > src/main.js <<'JS'
import './style.css'
// Classes used below: bg-red-500 bg-green-500
const badges = { error: 'bg-red-500', ok: 'bg-green-500' }
document.body.dataset.badge = badges.error
JS
npm run build > /dev/null
grep -q 'bg-red-500' dist/assets/*.css && echo "bg-red-500: generated"The output is bg-red-500: generated. Map values to complete class names, as in the badges object, instead of joining strings. If your classes live in a folder Tailwind does not scan, such as a package in node_modules, add it to the CSS with @source "../node_modules/some-ui-package";.
Step 8 · Stop the server and clean up
Stop the development server with Ctrl+C in its terminal. Then delete the project:
cd ~
rm -rf ~/tw-demoNext steps
- Build a full interface with a framework: build a React 19 app with Vite, then add the same two packages and the same CSS line.
- Learn the CSS underneath: build page layouts with CSS Grid and add dark mode with CSS variables.
- The Tailwind documentation at tailwindcss.com/docs covers the theme variables, custom variants,
@applyand the upgrade guide from version 3.
Related tutorials
- How to Build a React 19 App with ViteYou will scaffold a React 19 and TypeScript app with Vite 8, run the dev server, replace the starter page with a small checklist built from components and state, then create a production build and serve it locally.
- How to Install Node.js 26 LTS on Ubuntu 26.04You will install Node.js 26 LTS with npm from the NodeSource apt repository on Ubuntu 26.04 so that apt keeps it patched, run a first script, set up global npm packages without sudo, and learn the checksum-verified tarball route as an alternative.
Need this done for you?
Padding does this work for growing companies, from the first diagnosis to a system that keeps working.
Get new tutorials