Tailwind CSS Tutorial menu

Tailwind CSS Get Started

There are two common ways to start using Tailwind CSS: a quick CDN script for learning, and an npm install for real projects.

Option 1: The Play CDN (for Learning)

Add one <script> tag to the <head> of any HTML page. The script reads the classes in your page and creates the CSS for them in the browser.

Example

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

A complete page looks like this:

Example

html
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>My Tailwind Page</title>
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
</head>
<body class="p-8">

<h1 class="text-3xl font-bold text-cyan-600">It works!</h1>
<p class="mt-3 text-gray-600">Tailwind is loaded from the CDN.</p>

</body>
</html>
Try it Yourself »

This is what every example in this tutorial uses, so they run in the Try it editor.

The Play CDN is great for learning and quick demos, but it is not meant for live websites. It builds the CSS in each visitor's browser, which is slower. For a real site, use the npm setup below.

Option 2: Install with npm and Vite

For real websites, Tailwind runs on your computer and creates a small CSS file with only the classes you use. You need Node.js installed.

Step 1: Create a project

Example

bash
npm create vite@latest my-shop
cd my-shop
npm install

Step 2: Install Tailwind

Example

bash
npm install tailwindcss @tailwindcss/vite

Step 3: Add the plugin to vite.config.js

Example

javascript
import { defineConfig } from "vite";
import tailwindcss from "@tailwindcss/vite";

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

Step 4: Import Tailwind in your CSS file

Example

css
@import "tailwindcss";

Step 5: Start the dev server

Example

bash
npm run dev

Now you can use Tailwind classes in your HTML, and the page updates every time you save.

Adding Your Own Colours

Tailwind lets you add your own theme values with @theme. With the Play CDN, put it inside a <style type="text/tailwindcss"> tag. Each --color-... variable becomes a new colour class.

Example

html
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://cdn.jsdelivr.net/npm/@tailwindcss/browser@4"></script>
<style type="text/tailwindcss">
@theme {
  --color-brand: #0f766e;
  --color-brand-light: #ccfbf1;
}
</style>
</head>
<body class="p-8">

<div class="bg-brand-light text-brand p-6 rounded-lg font-semibold">
  Ali Electronics uses its own brand colours.
</div>

</body>
</html>
Try it Yourself »

The variable --color-brand created the classes bg-brand, text-brand, border-brand and more.

Which Option to Choose?

Play CDNnpm install
One line, nothing to installNeeds Node.js and a build tool
Good for learning and demosGood for real websites
CSS made in the browser (slower)Small, fast CSS file made in advance

Editor Tip

Install the Tailwind CSS IntelliSense extension in VS Code. It suggests class names while you type and shows the CSS each class adds when you hover over it.

Exercise

Exercise

Which line in a CSS file loads Tailwind CSS version 4 in an npm project?

Learn Tailwind CSS with a teacher

Join the Web Development course at Workownix, online or in Orangi Town, Karachi, and get a certificate.

See the course Ask on WhatsApp