Installing Tailwind via CDN and npm Step by step Implementation and Top 10 Questions and Answers
 Last Update:6/1/2025 12:00:00 AM     .NET School AI Teacher - SELECT ANY TEXT TO EXPLANATION.    10 mins read      Difficulty-Level: beginner

Certainly! Installing Tailwind CSS can seem daunting at first, but it's actually a straightforward process. We'll walk through installing Tailwind CSS using two different methods: the Content Delivery Network (CDN) and Node Package Manager (npm). Each method has its own use cases, with the CDN method being simpler yet less customizable, and the npm method offering more flexibility and control over your project setup.

Installing Tailwind via CDN:

Using a CDN is a quick and easy way to get started with Tailwind CSS. This method is best suited for small projects or prototypes due to limited customization capabilities. Here's how you do it:

Step 1: Create Your HTML File

First, create an HTML file. Let’s name it index.html and save it in your project folder. You can do this manually or via a code editor like Visual Studio Code.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tailwind CSS via CDN</title>
    <!-- Step 2: Import Tailwind CSS from the CDN -->
    <link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">
</head>
<body>
    <!-- Step 3: Add Tailwind Classes to Your HTML -->
    <h1 class="text-3xl font-bold underline text-center mt-10">
        Hello Tailwind!
    </h1>
</body>
</html>

Step 2: Import Tailwind CSS from the CDN

In the <head> section of your index.html, insert the following link tag to import Tailwind CSS. Note that this is importing version 2.2.19; make sure you check for the latest release on Tailwind CSS official site.

<link href="https://cdn.jsdelivr.net/npm/tailwindcss@2.2.19/dist/tailwind.min.css" rel="stylesheet">

This will pull in the full Tailwind CSS stylesheet from a CDN server. While it's convenient, keep in mind that it includes all possible classes, leading to a larger file size which isn't optimal for production. However, for small projects or learning purposes, it works great!

Step 3: Add Tailwind Classes to Your HTML

Now, let’s add some Tailwind CSS classes to our HTML. For instance, we can center text, make it bold, and underlined all in one go.

<h1 class="text-3xl font-bold underline text-center mt-10">
    Hello Tailwind!
</h1>

When you open this HTML file in a browser, you should see the text styled as specified by Tailwind CSS.

Installing Tailwind via npm:

Using npm gives you far greater control over Tailwind CSS. It minimizes the final CSS file size by only including the utility classes that you've used, making your page significantly faster to load. Moreover, it allows you to customize Tailwind’s default settings and build more advanced projects. Let’s proceed step by step:

Prerequisites

Before starting, ensure that you have Node.js and npm installed on your machine. To check if they're installed correctly, run node -v and npm -v in your terminal or command prompt.

Setting Up a Basic Project Structure

Create your project folder and navigate into it. Then initialize a new npm project with a simple template:

mkdir my-tailwind-project
cd my-tailwind-project
npm init -y

This creates a package.json file with some basic configurations automatically.

Step 4: Install Tailwind CSS and PostCSS

Now you need to install Tailwind CSS along with necessary tools to build and purge unused styles. Run:

npm install -D tailwindcss@latest postcss@latest autoprefixer@latest

We're installing Tailwind CSS, PostCSS, and Autoprefixer as development dependencies (-D). PostCSS is a tool used to transform CSS with JavaScript, and Autoprefixer ensures compatibility with older browsers.

Step 5: Create Configuration Files

Tailwind needs configuration files to know how to process your CSS. Generate these configurations by running:

npx tailwindcss init -p

This command creates two files, tailwind.config.js (configuration options) and postcss.config.js (settings for PostCSS).

Step 6: Configure Tailwind to Purge Unused Styles

Open tailwind.config.js and modify the purge property to include paths to all your template files. This tells Tailwind to only keep the CSS that’s used in those files, effectively reducing the size of your final CSS output.

If you’re working with a single index.html, your config should look like:

module.exports = {
  purge: ['./index.html'],
  darkMode: false, // or 'media' or 'class'
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

For projects with multiple pages or using frameworks like React, Vue, etc., you might want to include all files under src/ or public/ folders.

module.exports = {
  purge: ['./src/**/*.{html,js}', './public/index.html'],
  darkMode: false,
  theme: {
    extend: {},
  },
  variants: {
    extend: {},
  },
  plugins: [],
}

Step 7: Create Your CSS File

Inside your project folder, create a src directory and then inside it, create a styles.css file (you can name it any name you prefer). Import the Tailwind base, directives, and components into this file:

/* src/styles.css */
@tailwind base;
@tailwind components;
@tailwind utilities;

These @tailwind directives instruct Tailwind to generate each of these stylesheets when it’s run.

Step 8: Add Scripts to Your package.json File

Modify your package.json to include scripts that will compile your CSS. Open the file and add the following:

"scripts": {
    "dev": "npx tailwindcss -i ./src/styles.css -o ./dist/output.css --watch",
    "build": "npx tailwindcss -i ./src/styles.css -o ./dist/output.css --minify"
}

The dev script runs Tailwind in watch mode, so every time you change your template files, it'll recompile the CSS. The build script compiles the CSS and minifies it, reducing the file size which is great for production.

Step 9: Import Your Compiled CSS File in Your HTML

Create a dist directory in your root folder where output.css will be placed after running the build or dev scripts. Modify your index.html to use this output.css:

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Tailwind CSS via npm</title>
    <link href="dist/output.css" rel="stylesheet">
</head>
<body>
    <h1 class="text-3xl font-bold underline text-center mt-10">
        Hello Tailwind!
    </h1>
</body>
</html>

Now, you have to compile the CSS. If you want live reloading during development, use the dev script:

npm run dev

For production, just run the build script:

npm run build

This compiles the CSS and writes it to dist/output.css. Refresh your index.html page to see the styles applied.

Final Thoughts

Both installation methods are valid and choosing between them depends on project requirements. For beginners who want to get hands-on quickly, the CDN method is perfect. It requires no additional setup and is easy to understand. However, for serious applications where you need more flexibility and efficient optimization of CSS, setting up Tailwind via npm is highly recommended. It might take some time initially to set up, but the benefits in terms of customization, performance, and maintainability make the effort worthwhile.

By following these steps, you’re now ready to start building with Tailwind CSS using either method. Tailwind CSS has an excellent documentation, which I encourage you to explore further to understand all its features and possibilities. Happy coding!