Using Tailwind CSS with CLI

Introduction πŸš€

The Tailwind CSS CLI (Command Line Interface) method is one of the easiest and most flexible ways to install Tailwind in real projects. It lets you compile your CSS efficiently, remove unused styles, and customize everything usingtailwind.config.js.

>>β€œTailwind CLI gives you full power without complex build tools.” ⚑

Why Use Tailwind CLI? 🌟

  • ⚑ Fast installation
  • 🧹 Purges unused CSS automatically
  • 🎨 Full customization support
  • πŸ“¦ No need for webpack, Vite, or bundlers
  • πŸ›  Works in any project β€” HTML, PHP, Node, or frameworks

Step-by-Step Setup πŸ“Œ

Step 1: Create a Project Folder πŸ“

Create project & open terminal

mkdir tailwind-cli-project
cd tailwind-cli-project

Step 2: Initialize npm πŸ“¦

Initialize npm

npm init -y

Step 3: Install Tailwind CSS 🧩

Install Tailwind via npm

npm install -D tailwindcss

Step 4: Create Tailwind Config File βš™οΈ

Generate tailwind.config.js

npx tailwindcss init

This creates a default configuration file:

tailwind.config.js

module.exports = {
  content: ["./**/*.{html,js}"],
  theme: { extend: {} },
  plugins: [],
};

Note

πŸ’‘ Add paths to every file where you’ll use Tailwind classes.

Step 5: Create Main CSS File 🎨

src/style.css

@tailwind base;
@tailwind components;
@tailwind utilities;

Step 6: Compile Tailwind Using CLI πŸ—οΈ

Use Tailwind CLI to generate your final CSS file. You can run it once or in watch mode.

Build CSS (One-time)

npx tailwindcss -i ./src/style.css -o ./dist/output.css

Build + Watch (auto updates)

npx tailwindcss -i ./src/style.css -o ./dist/output.css --watch

Using Tailwind in HTML πŸ“„

index.html

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>Tailwind CLI Project</title>

  <link rel="stylesheet" href="./dist/output.css" />
</head>
<body class="bg-gray-100 flex items-center justify-center h-screen">

  <div class="p-6 bg-white rounded-xl shadow-xl">
    <h1 class="text-3xl font-bold text-blue-600">Hello Tailwind CLI! πŸŽ‰</h1>
  </div>

</body>
</html>

Folder Structure πŸ—‚οΈ

Structure

project/
β”œβ”€β”€ src/
β”‚   └── style.css
β”œβ”€β”€ dist/
β”‚   └── output.css
β”œβ”€β”€ index.html
β”œβ”€β”€ tailwind.config.js
└── package.json

Advantages of CLI ⚑

  • 🧹 Removes unused CSS β€” optimized build
  • πŸ›  Easy to integrate with any project
  • πŸ“¦ Zero bundler configuration required
  • πŸš€ Faster builds than traditional setup

Production Build πŸ“¦

To minify CSS for production, run:

Production build command

npx tailwindcss -i ./src/style.css -o ./dist/output.css --minify

Note

πŸ”₯ Your CSS becomes super small thanks to purge + minify.

Additional Resources πŸ“š

>>β€œCLI keeps Tailwind simple, powerful, and framework-agnostic.” πŸ§