Lesson 2 / 1217%

Lesson 2

Installing Tailwind CSS

15 min

Install Tailwind with Vite, the CDN, or a framework like Astro.

Definition

Installing Tailwind CSS means adding Tailwind to a project so that its utility classes become usable in your HTML. In practice, you install the tailwindcss package along with a build plugin (such as @tailwindcss/vite), which scans your code and generates only the CSS you need, or you load the Play CDN directly for a simple prototype.

Installing Tailwind CSS

There are several ways to install Tailwind depending on your project. In this lesson, we cover the two most common methods: installation with a build tool (Vite, the recommended method for a real project) and quick integration through a CDN link to test or prototype rapidly.

Whichever method you choose, the result is the same: you will be able to write classes like p-4 or bg-blue-500 in your HTML and see them applied immediately in the browser.

Before you start: prerequisites

For the build-tool method, you only need three things on your machine:

  • Node.js (version 20 or newer recommended): it provides the runtime environment required by Vite and Tailwind.
  • npm : the package manager that comes with Node.js, used to download Tailwind and its plugins.
  • A code editor : VS Code is an excellent choice, and the "Tailwind CSS IntelliSense" extension helps you with class autocompletion.

You can check your versions with these two commands:

Terminal
node --version
npm --version

Method 1 — Installing with Vite (recommended)

Since version 4, Tailwind can be installed with just a few commands, with no mandatory configuration file. Here are the steps to follow for a Vite-based project:

Terminal
# 1. Create an empty Vite project (if needed)
npm create vite@latest my-project
cd my-project

# 2. Install the required packages
npm install tailwindcss @tailwindcss/vite

If you are starting from scratch, create the Vite project first, then install Tailwind inside the folder that was created. The order matters: the packages must be added inside your project folder, where thepackage.json file is located.

Next, enable the Tailwind plugin in your Vite configuration:

vite.config.js
import { defineConfig } from 'vite'
import tailwindcss from '@tailwindcss/vite'

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

Then import Tailwind in your main CSS file (for examplesrc/style.css):

src/style.css
@import "tailwindcss";

This single line is enough: Tailwind itself injects its base styles (Preflight), its theme variables and all the utility classes you will need.

All that is left is to import this CSS file in your HTML or JavaScript entry point, then start your development server:

Terminal
npm run dev

Vite then displays a local address (usuallyhttp://localhost:5173). As long as the server is running, every change to your classes is reflected instantly in the browser, with no manual reload.

Method 2 — The CDN (for rapid prototyping)

If you simply want to test an idea in a single HTML file, without a build tool, you can use the Play CDN script. This method is handy for learning or building a demo, but it is not recommended in production (heavier file, no advanced theme customization).

index.html
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body>
  <h1 class="text-3xl font-bold text-indigo-600">
    Hello Tailwind!
  </h1>
</body>
</html>

To try it, save this code in an index.html file and open it directly in your browser: no installation, no terminal, no server. This is what makes the CDN ideal for a first contact or for sharing a small example.

Here is why the CDN remains reserved for experiments:

  • Performance : the browser downloads and runs a full script to generate the CSS, which slows the page down.
  • Limited features : no optimization of the final CSS and no simple integration with a framework.
  • External dependency : without an internet connection, your styles no longer load.

Result

Preview of the page styled with Tailwind CSS after installation

What does your project look like after installation?

Once Method 1 is complete, your project contains a few key files. Thepackage.json file lists the installed packages, and you will find Tailwind and its Vite plugin in the dependencies:

package.json (excerpt)
{
  "dependencies": {
    "tailwindcss": "^4.0.0",
    "@tailwindcss/vite": "^4.0.0"
  }
}

The version numbers may differ on your machine: this is not a problem as long as you stay on version 4. You do not need to create atailwind.config.js file to get started; it only becomes useful later, when you want to customize your theme.

Which method should you choose?

The choice simply depends on what you want to do. Here is a quick guide:

  • Learning or testing an idea : the CDN, to get started in a few seconds in a single HTML file.
  • Building a real website or application : Vite (or your framework's integration), for optimized final CSS and a fully customizable theme.
  • Going live : always use the build tool, never the Play CDN.

Installing with a framework (Astro, Next.js, etc.)

Most modern frameworks offer their own official integration. The principle remains the same: install the matching package, enable the plugin, then import Tailwind in the global CSS.

  • Astro : npx astro add tailwind configures everything automatically.
  • Next.js : Tailwind is offered directly when you create the project with create-next-app.
  • Vue / Svelte : follow the same pattern as the Vite setup described above, through the matching plugin.

In every case, remember to check Tailwind's official documentation: it provides an installation guide for each framework and remains the most up-to-date source if a command changes.

Checking that everything works

Once Tailwind is installed, the best way to check that everything is properly wired up is to add a very visible class to an element, such asbg-red-500 or text-4xl, and reload the page. If the style is applied, the installation was successful.

If nothing changes, don't worry: open your browser's developer tools (the F12 key), then look in the "Console" and "Network" tabs to see whether an error appears or your CSS file is not loaded. This almost always points you in the right direction.

What if the styles are not applied?

This is the most common problem for a beginner, and it almost always comes from a simple oversight. Go through these points:

  • The CSS is not imported : check thatsrc/style.css is actually imported in your HTML or JavaScript entry file.
  • The plugin is missing : make sure thattailwindcss() appears in the plugins list ofvite.config.js.
  • The server was not restarted : after changing the configuration, stop the server and run npm run dev again.
  • A typo in the class name : an unknown class is simply ignored, with no error message. Compare it with the documentation.

Your turn

Create a new Vite project, install Tailwind using Method 1, then display an<h1> heading with the classestext-4xl font-extrabold text-blue-600 text-center mt-10. Check in your browser that the heading appears centered, in blue and large.