Skip to content

Tailwind CSS - Introduction

Tailwind CSS is a utility-first CSS framework designed for rapid UI development. Unlike component-based frameworks like Bootstrap, Tailwind provides low-level utility classes that let you build completely custom designs without ever leaving your HTML.

The JIT Engine: The Core of Modern Tailwind

Section titled “The JIT Engine: The Core of Modern Tailwind”

Modern Tailwind CSS is powered by a Just-in-Time (JIT) compiler. This is a game-changer. Instead of shipping a large, predefined CSS file, the JIT engine scans your HTML, JavaScript, and other template files for class names and generates only the CSS you actually use. This results in incredibly small production bundles, regardless of how many utilities you use during development.

While you can use a CDN for quick demos, the true power of Tailwind is unlocked through a proper build setup using a package manager like npm.

1. Installation

# Navigate to your project directory
npm install -D tailwindcss
npx tailwindcss init

This creates a tailwind.config.js file.

2. Configure Content Paths

In tailwind.config.js, tell Tailwind where to look for your classes.

/** @type {import('tailwindcss').Config} */
module.exports = {
content: ["./src/**/*.{html,js}"],
theme: {
extend: {},
},
plugins: [],
}

3. Create your Main CSS File

Create a CSS file (e.g., ./src/input.css) and add the Tailwind directives.

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

4. Start the Build Process

Run the Tailwind CLI to compile your CSS. The --watch flag will automatically rebuild as you make changes.

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

Finally, link ./dist/output.css in your HTML file and start building!

  • Rapid Development: Build complex interfaces quickly by composing utilities directly in your markup.
  • Highly Customizable: Tweak every color, spacing unit, font, and breakpoint in the tailwind.config.js file.
  • Tiny Production Builds: The JIT engine ensures your final CSS is only a few kilobytes.
  • No Naming Conventions: Say goodbye to agonizing over class names like .user-profile-card__header--active.
  • Great for Components: Works perfectly with modern frameworks like React, Vue, and Svelte, allowing you to create encapsulated, styled components.

“My HTML looks messy with all these classes!”

This is a common first impression. The modern solution is to embrace a component-based workflow. When you extract repeated UI into a template partial or a JavaScript framework component, the long string of classes is defined in only one place. For non-component-based projects, you can use the @apply directive in your CSS to create custom, reusable component classes.

/* In your CSS file */
.btn-primary {
@apply py-2 px-4 bg-blue-500 text-white font-semibold rounded-lg shadow-md hover:bg-blue-700;
}
<!-- In your HTML -->
<button class="btn-primary">Click me</button>

“It has a steep learning curve.”

The learning curve is primarily about memorizing the utility names. However, they are highly intuitive (p-4 for padding, flex for flexbox, text-lg for large text). With modern editor extensions like the official Tailwind CSS IntelliSense, you get autocompletion and class previews, making the process much easier.