Skip to content
Tutorials Center
My learning

Topics

Lesson 2 of 37 ยท Getting Started

Installing Tailwind CSS

Set up Tailwind CSS v4 with the Vite plugin, the standalone CLI, PostCSS or the browser build, and learn which method fits which project.

There are four common ways to use Tailwind CSS v4. Which one you pick depends on your project: a modern build tool, a plain static site, an existing PostCSS pipeline, or just a quick experiment. All four produce the same utilities; they differ in how the CSS gets generated.

Every example in this track runs in your browser with the fourth method, the browser build, so you can try things without installing anything. For a real site you should use one of the first three, which generate a small static CSS file ahead of time. The install commands below are read-only snippets, because the site cannot run a terminal for you.

You need Node.js (version 20 or newer) for the first three methods. Check with node --version.

If your project uses Vite (Laravel, SvelteKit, Astro, Vue, React and many others do), this is the fastest path. Install Tailwind and the plugin:

Terminal
npm install tailwindcss @tailwindcss/vite

Add the plugin to vite.config.ts:

TypeScript
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'

export default defineConfig({
  plugins: [tailwindcss()],
})

Create a CSS file, for example src/style.css, and import Tailwind:

CSS
@import "tailwindcss";

Import that CSS from your entry point (or link it from your HTML), run npm run dev, and start using classes. That single @import is the whole setup: no config file, no content list.

Method 2: the Tailwind CLI

For a plain HTML site with no bundler, use the standalone command line tool:

Terminal
npm install tailwindcss @tailwindcss/cli

Write the input file src/input.css:

CSS
@import "tailwindcss";

Then build, and keep rebuilding as you edit:

Terminal
npx @tailwindcss/cli -i ./src/input.css -o ./dist/output.css --watch

Link dist/output.css from your HTML:

HTML
<link href="/dist/output.css" rel="stylesheet">

For production, add --minify and drop --watch. The CLI scans the folder it runs in for class names.

Method 3: PostCSS

If your tooling already uses PostCSS (Next.js, Webpack setups, older frameworks), use the PostCSS plugin:

Terminal
npm install tailwindcss @tailwindcss/postcss postcss
JavaScript
// postcss.config.mjs
export default {
  plugins: {
    "@tailwindcss/postcss": {},
  },
}

Notice there is no autoprefixer and no postcss-import: v4 handles vendor prefixes and @import itself. Then add @import "tailwindcss"; to your main CSS file, exactly like the other methods.

flowchart LR
  A["Your HTML, JS,<br/>templates"] --> T["Tailwind engine<br/>scans class names"]
  C["input.css<br/>@import tailwindcss"] --> T
  T --> O["output.css<br/>only used utilities"]

All three build methods work the same way: scan your files, read your CSS, write the used utilities.

Method 4: the browser build

The browser build is a JavaScript file that scans the page and generates CSS right in the visitor's browser. It needs no tooling at all, which makes it ideal for learning, demos and CodePens:

HTML
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>

Our examples pin an exact version (@tailwindcss/[email protected]) so they keep working. Try it:

Tailwind from a script tag

Do not ship the browser build to real visitors. It generates CSS on every page load, which is slower and flashes unstyled content. Use it for learning and prototypes, and use the Vite, CLI or PostCSS method in production.

The browser build also reads CSS written inside <style type="text/tailwindcss">. This is where you can use @theme and @apply, the same directives you would put in input.css. You will use this a lot from chapter 6 on:

@theme in the browser build

Declaring --color-brand made bg-brand, text-brand, border-brand and every other color utility available, including opacity modifiers like hover:bg-brand/80.

Which method should I choose?

Situation Use
Vite, Laravel, Astro, SvelteKit, Vue, React with Vite Vite plugin
Static HTML, no bundler Tailwind CLI
Next.js or an existing PostCSS pipeline PostCSS plugin
Learning, a demo, a bug-report reproduction Browser build

Laravel 12 and 13 starter kits already ship with the Vite plugin configured, so a new Laravel project needs no extra steps beyond npm install and npm run dev.

Editor support

Install the official Tailwind CSS IntelliSense extension for VS Code (also available for JetBrains IDEs and Neovim). It gives you class autocompletion, shows the CSS behind a utility on hover, and warns about typos. Pair it with the Prettier plugin for Tailwind (prettier-plugin-tailwindcss), which sorts class lists into a consistent order so diffs stay readable.

Common mistakes

  • Forgetting the CSS import. If nothing is styled, check that your entry CSS contains @import "tailwindcss"; and that the page actually loads it.
  • Using v3 syntax with v4. The old @tailwind base; @tailwind components; @tailwind utilities; lines, tailwind.config.js and the tailwindcss PostCSS plugin name all belong to v3. In v4 use @import "tailwindcss"; and @tailwindcss/postcss.
  • Building class names dynamically. class="bg-${color}-500" can never work, because Tailwind reads your files as plain text and never runs your code. You will see why in How Tailwind Works.

Check your understanding

Quick check ยท 4 questions

of 4 answered You scored / 4 ยท perfect!

  1. 1. Which line activates Tailwind CSS v4 in your stylesheet?

    v4 replaced the three @tailwind directives with a single CSS import.
  2. 2. Which install method is meant for learning and prototypes only, not production?

    The browser build generates CSS in the visitor's browser on every load, so production sites should use a build step.
  3. 3. Which package do you need to use Tailwind v4 in a PostCSS pipeline?

    In v4 the PostCSS plugin lives in its own package, @tailwindcss/postcss, and handles prefixing and imports itself.
  4. 4. Where do you write @theme when using the browser build?

    The browser build reads Tailwind CSS from style elements with type text/tailwindcss.

Summary

  • For real projects, use the Vite plugin, the CLI, or the PostCSS plugin; each needs only @import "tailwindcss"; in your CSS.
  • The browser build (@tailwindcss/browser) needs no tooling and is perfect for learning, but is not for production.
  • Custom CSS such as @theme and @apply goes in your CSS file, or in <style type="text/tailwindcss"> with the browser build.
  • Add IntelliSense and the Prettier plugin to your editor.

Next: Your First Tailwind Page