CSS Preprocessors & Tools

Tailwind CSS Basics

A utility-first CSS framework for rapidly building modern user interfaces with pre-designed utility classes.

What is Tailwind CSS?

Tailwind is a utility-first CSS framework: instead of writing custom classes, you compose small single-purpose classes like flex, p-4, text-lg and bg-blue-500 directly in your HTML. It generates only the CSS you actually use, and responsive or state variants are prefixes such as md: and hover:.

<button class="rounded-lg bg-blue-600 px-4 py-2 font-medium text-white
               hover:bg-blue-700 focus-visible:outline-2 md:px-6">
  Save
</button>

Complete Tailwind Example

Code Editor

<p>Tailwind classes (left) and the CSS each one maps to (right):</p>
<table>
  <tr><td><code>flex</code></td><td>display: flex</td></tr>
  <tr><td><code>p-4</code></td><td>padding: 1rem</td></tr>
  <tr><td><code>text-lg</code></td><td>font-size: 1.125rem</td></tr>
  <tr><td><code>bg-blue-600</code></td><td>background: #2563eb</td></tr>
  <tr><td><code>md:flex-row</code></td><td>@media (min-width: 768px) { flex-direction: row }</td></tr>
  <tr><td><code>hover:bg-blue-700</code></td><td>:hover { background: #1d4ed8 }</td></tr>
</table>
<button class="px-4 py-2 rounded-lg bg-blue-600 text-white">Button built from utilities</button>

Preview

Core Concepts

Utility-First Approach

Build designs using small, single-purpose utility classes.

<div class="bg-blue-500 text-white p-4 rounded">

Responsive Design

Mobile-first responsive design with breakpoint prefixes.

<div class="w-full md:w-1/2 lg:w-1/3">

State Variants

Style different states with variant prefixes.

<button class="bg-blue-500 hover:bg-blue-600">

Component Extraction

Extract repeated patterns into reusable components.

@apply bg-blue-500 text-white p-4 rounded;

Getting Started

Installation Options

1. CDN (Quick Start)

<script src="https://cdn.tailwindcss.com"></script>

2. NPM Installation

npm install -D tailwindcss
npx tailwindcss init

3. PostCSS Plugin

npm install -D tailwindcss postcss autoprefixer

Tailwind CSS — step by step

1 / 4
1

Install and configure Tailwind

Add Tailwind via npm and configure the content array in tailwind.config.js to point to your template files. This enables purging of unused utilities.

1 of 4
💡Tailwind Tree-Shaking
Tailwind scans your source files for class names and removes every utility you don't use before production. The resulting CSS file is tiny — often under 10 KB — regardless of how many utilities Tailwind provides.

🎯 Quick Check

What Tailwind directive lets you reuse a set of utility classes inside a CSS file?

Tailwind CSS: summary

Key points

  • You still need to understand CSS: Tailwind classes map almost one-to-one to CSS properties.
  • Prefixes apply variants: sm:/md:/lg: for breakpoints, hover:, focus:, dark: and many more.
  • Repeated patterns belong in components (React, Vue…) rather than in long @apply rules.
  • Tailwind v4 is configured in CSS with @theme instead of a JavaScript config file.

Frequently asked questions

›Should I learn CSS before Tailwind?

Yes. Tailwind is a faster way to write CSS, not a replacement for knowing it. Layout, the box model and specificity all still matter.

›Is Tailwind better than Bootstrap?

They solve different problems. Bootstrap gives you ready-made components with a recognisable look. Tailwind gives you low-level utilities to build any design, so sites don't all look the same.