Tutorials Tailwind CSS Introduction to Tailwind CSS
Ch 1 / 45
Chapter 1 Beginner 8 min read Tailwind CSS

Introduction to Tailwind CSS

Traditional CSS requires you to switch between HTML and stylesheet files, name classes, and fight specificity battles. Tailwind CSS flips this entirely — you style directly in your HTML using tiny, composable utility classes. No more naming things, no more context-switching. In this chapter you'll understand what Tailwind is, why developers love it, and how to get it running in minutes.

Advertisement
Google Ad Placeholder · 728×90 Leaderboard
What you'll learn
What Tailwind CSS is and how utility-first CSS differs from traditional CSS
How to install Tailwind via CDN, NPM, and with a config file
The core concepts — utility classes, variants, responsive prefixes
How Tailwind handles dark mode and state variants like hover & focus
The anatomy of a Tailwind class — prefix, property, value scale
Writing your very first Tailwind UI — a styled card component

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that provides thousands of low-level, single-purpose CSS classes — called utility classes — that you apply directly in your HTML. Instead of writing button { padding: 12px 24px; background: blue; } in a separate stylesheet, you write <button class="px-6 py-3 bg-blue-500"> right in your markup.

Released in 2017 by Adam Wathan, Tailwind has become the most popular CSS framework for modern web development. It ships a design system built into the class names — a consistent spacing scale, colour palette, type scale, and shadow system — so your designs are automatically cohesive.

Utility-First
Style elements directly with class names. No separate CSS files needed for most UI work.
Configurable
Extend the design system with your own colours, fonts, spacing, and breakpoints via tailwind.config.js.
Responsive
Built-in responsive prefixes (sm:, md:, lg:) let you design mobile-first without media query files.
Dark Mode
A single dark: prefix applies dark-mode styles — no duplicate stylesheets required.
Tip Tailwind is not a component library (like Bootstrap). It gives you the building blocks; you compose the components. That's the key mental shift.

Utility-First vs Traditional CSS

The biggest paradigm shift with Tailwind is moving from semantic class names (where you name what a component is) to utility class names (where classes describe what a style does). Both approaches produce the same visual result, but the developer experience is radically different.

Traditional CSS — two-file approach
/* styles.css — you must name, maintain, and switch to this file */
.card {
  background: white;
  border-radius: 12px;
  padding: 24px;
  box-shadow: 0 4px 20px rgba(0,0,0,.08);
  max-width: 380px;
}
.card-title {
  font-size: 1.25rem;
  font-weight: 700;
  color: #1a1a2e;
  margin-bottom: 8px;
}
.card-btn {
  background: #3b82f6;
  color: white;
  padding: 10px 20px;
  border-radius: 8px;
  border: none;
  cursor: pointer;
}

<!-- index.html -->
<div class="card">
  <h2 class="card-title">Hello World</h2>
  <button class="card-btn">Click me</button>
</div>
Tailwind CSS — everything in HTML
<!-- No separate CSS file needed — styles live in the class attribute -->
<div class="bg-white rounded-xl p-6 shadow-lg max-w-sm">
  <h2 class="text-xl font-bold text-gray-900 mb-2">Hello World</h2>
  <button class="bg-blue-500 text-white px-5 py-2.5 rounded-lg
           hover:bg-blue-600 transition-colors cursor-pointer">
    Click me
  </button>
</div>
Aspect Traditional CSS Tailwind CSS
Naming classes Must think of semantic names (.card, .btn-primary) No naming needed — classes are predefined
File management Switch between HTML and CSS files constantly Everything in one place (the HTML)
CSS growth Stylesheet grows unboundedly with the project CSS file stays tiny (only used classes are included)
Design system Must build your own spacing, colour, type scales Built-in consistent design system out of the box
Responsive Write @media queries manually in CSS Add sm: / md: prefix to any class
Dark mode Separate rules with [data-theme] or .dark selectors Add dark: prefix to any class
Learning curve Learn CSS properties (widely known) Learn Tailwind class naming convention

Installing Tailwind CSS

There are three common ways to add Tailwind to a project. For learning and prototyping, the CDN link is instant — no build tool needed. For production projects, the PostCSS / CLI approach gives you the full power of Tailwind including purging unused classes, custom config, and plugins.

1
CDN — Instant, No Build Tool (Learning/Prototyping)
Add the CDN script to your HTML <head>. Tailwind loads the full framework — great for experiments, not production.
2
Tailwind CLI — Local Build (Recommended for Most Projects)
Install via npm, create a config file, and run the CLI watcher. Tailwind scans your HTML and only outputs the CSS you actually use.
3
Framework Integration — Vite / Next.js / Nuxt / Laravel
Most modern frameworks have official Tailwind guides. Vite + Tailwind is the most popular combo for React/Vue/Svelte projects.
Method 1 — CDN (quickest start)
<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>My Tailwind Page</title>
  <!-- Tailwind CDN -->
  <script src="https://cdn.tailwindcss.com"></script>
</head>
<body class="bg-gray-100 min-h-screen flex items-center justify-center">
  <div class="bg-white rounded-2xl p-8 shadow-xl max-w-md w-full">
    <h1 class="text-3xl font-bold text-gray-900 mb-3">
      Hello, Tailwind! 🎉
    </h1>
    <p class="text-gray-500 leading-relaxed mb-6">
      Styled with utility classes — no separate CSS file needed.
    </p>
    <button class="bg-cyan-500 hover:bg-cyan-600 text-white font-semibold
             px-6 py-3 rounded-xl transition-all duration-200 w-full">
      Get Started
    </button>
  </div>
</body>
</html>
Method 2 — Tailwind CLI (production)
# Step 1 — Install Tailwind CSS via npm
npm install -D tailwindcss

# Step 2 — Generate config file
npx tailwindcss init

# Step 3 — Configure template paths in tailwind.config.js
module.exports = {
  content: ["./src/**/*.{html,js}"],
  theme: { extend: {} },
  plugins: [],
}

# Step 4 — Add Tailwind directives to your CSS (input.css)
@tailwind base;
@tailwind components;
@tailwind utilities;

# Step 5 — Run the build watcher
npx tailwindcss -i ./src/input.css -o ./dist/output.css --watch

# Link output.css in your HTML
<link rel="stylesheet" href="dist/output.css">

Core Concept — Utility Classes

Every Tailwind class does one specific CSS thing. They follow a consistent naming pattern: [property]-[value] or [prefix]:[property]-[value] for variants. Once you internalize the pattern, you can guess most class names without looking them up.

Tailwind class categories — a quick reference
Layout
flex, grid, block, hidden, container, max-w-*, w-*, h-* — control how elements are displayed and sized. The foundation of every layout.
Spacing
p-4, px-6, py-2, m-auto, mx-4, mt-8, gap-4 — padding, margin, and gap. Tailwind uses a 4px base unit: p-1 = 4px, p-4 = 16px, p-8 = 32px.
Typography
text-xl, font-bold, text-gray-700, leading-relaxed, tracking-wide, uppercase, truncate — font size, weight, colour, line height, letter spacing, and text transforms.
Colors
bg-blue-500, text-red-600, border-gray-200 — Tailwind's colour palette includes 22 named colours, each with 11 shades (50–950). E.g. bg-cyan-400.
Flexbox
flex-row, flex-col, items-center, justify-between, flex-1, flex-wrap — all flexbox properties mapped to concise class names.
Grid
grid-cols-3, col-span-2, row-span-1, place-items-center — CSS Grid made easy. grid-cols-3 creates a 3-column layout instantly.

Responsive Design with Prefixes

Tailwind is mobile-first. By default, every class applies to all screen sizes. To apply a class only at a specific breakpoint and above, prefix it with the breakpoint name. This replaces the need for custom @media queries entirely.

(none)
All screens
0px and up
sm:
Small
≥ 640px
md:
Medium
≥ 768px
lg:
Large
≥ 1024px
xl:
X-Large
≥ 1280px
2xl:
2X-Large
≥ 1536px
Responsive + Dark Mode + State variants
<!-- Responsive grid: 1 col → 2 col (md) → 3 col (lg) -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6">
  <div class="...">Card 1</div>
  <div class="...">Card 2</div>
  <div class="...">Card 3</div>
</div>

<!-- Dark mode: bg changes based on OS/class preference -->
<div class="bg-white dark:bg-gray-900 text-gray-900 dark:text-white p-6 rounded-xl">
  Adapts to dark mode automatically!
</div>

<!-- State variants: hover, focus, active, disabled -->
<button class="bg-cyan-500 hover:bg-cyan-600 focus:ring-4 focus:ring-cyan-300
          active:scale-95 disabled:opacity-50 disabled:cursor-not-allowed
          text-white font-semibold px-6 py-3 rounded-xl transition-all duration-200">
  Interactive Button
</button>

<!-- Combine responsive + dark + hover -->
<h1 class="text-2xl md:text-4xl lg:text-6xl
         font-black text-gray-900 dark:text-white
         hover:text-cyan-500 transition-colors">
  Tailwind CSS
</h1>

Your First Tailwind Component

Let's build a complete, production-quality profile card with Tailwind CSS. This example demonstrates layout, spacing, typography, colours, hover states, and responsive design — all without writing a single line of custom CSS.

HTML — Profile Card Component
<!DOCTYPE html>
<html lang="en" class="dark">
<head>
  <script src="https://cdn.tailwindcss.com"></script>
  <script>tailwind.config = { darkMode: 'class' }</script>
</head>
<body class="bg-gray-100 dark:bg-gray-950 min-h-screen flex items-center justify-center p-6">
  <div class="bg-white dark:bg-gray-900 rounded-2xl p-8 max-w-sm w-full
             shadow-xl border border-gray-100 dark:border-gray-800
             hover:shadow-2xl hover:-translate-y-1 transition-all duration-300">
    <div class="relative w-20 h-20 mx-auto mb-4">
      <div class="w-20 h-20 rounded-full bg-gradient-to-br from-cyan-400 to-blue-600
                  flex items-center justify-center text-3xl font-bold text-white">
        AW
      </div>
      <span class="absolute bottom-1 right-1 w-4 h-4 rounded-full
              bg-green-400 border-2 border-white dark:border-gray-900"></span>
    </div>
    <div class="text-center mb-6">
      <h2 class="text-xl font-bold text-gray-900 dark:text-white">Adam Wathan</h2>
      <p class="text-sm text-cyan-500 font-medium mt-1">Creator of Tailwind CSS</p>
      <p class="text-sm text-gray-500 dark:text-gray-400 mt-3 leading-relaxed">
        Building utility-first CSS frameworks and making developers lives easier.
      </p>
    </div>
    <div class="grid grid-cols-3 gap-4 mb-6 border-t border-gray-100 dark:border-gray-800 pt-6">
      <div class="text-center"><div class="text-lg font-bold text-gray-900 dark:text-white">82k</div><div class="text-xs text-gray-400 mt-0.5">Stars</div></div>
      <div class="text-center border-x border-gray-100 dark:border-gray-800"><div class="text-lg font-bold text-gray-900 dark:text-white">4.6M</div><div class="text-xs text-gray-400 mt-0.5">Downloads</div></div>
      <div class="text-center"><div class="text-lg font-bold text-gray-900 dark:text-white">v4</div><div class="text-xs text-gray-400 mt-0.5">Version</div></div>
    </div>
    <div class="flex gap-3">
      <button class="flex-1 bg-cyan-500 hover:bg-cyan-600 text-white font-semibold py-2.5 rounded-xl transition-colors">Follow</button>
      <button class="flex-1 border border-gray-200 dark:border-gray-700 text-gray-700 dark:text-gray-300
               font-semibold py-2.5 rounded-xl hover:border-cyan-400 transition-colors">Message</button>
    </div>
  </div>
</body>
</html>

Quick Reference — Essential Classes

Class CSS Equivalent Description
flex display: flex Makes an element a flex container
grid display: grid Makes an element a grid container
hidden display: none Hides the element
w-full width: 100% Full width of parent
h-screen height: 100vh Full viewport height
p-4 padding: 1rem (16px) Padding on all sides
px-6 padding-left/right: 1.5rem Horizontal padding
text-xl font-size: 1.25rem Extra-large text
font-bold font-weight: 700 Bold font weight
rounded-xl border-radius: 0.75rem Extra-large border radius
shadow-lg box-shadow: 0 10px 15px… Large drop shadow
transition transition-property: all… Smooth transitions
duration-300 transition-duration: 300ms 300ms transition speed
opacity-50 opacity: 0.5 50% opacity
cursor-pointer cursor: pointer Hand cursor on hover
overflow-hidden overflow: hidden Clips overflowing content
relative position: relative Relative positioning
absolute position: absolute Absolute positioning
z-50 z-index: 50 Stacking order
top-0 top: 0 Positioned at top edge
Featured Course
Master Tailwind CSS with Real Projects(React Course)
Go from utility-class beginner to building production-grade UIs. Our structured course covers Tailwind v4, Vite integration, custom themes, component patterns, and 5 real projects including a full SaaS landing page and dashboard.
45 Chapters 10+ Hours of Content 5 Real Projects Certificate Included Live Mentoring
🎨
⭐ 4.9 · 1,200+ students
Tailwind CSS Cheat Sheet
Download our free one-page Tailwind CSS cheat sheet covering layout, spacing, typography, colours, flexbox, grid, responsive prefixes, and state variants.
Layout Spacing Typography Colors Responsive Dark Mode
Chapter Summary
  • Tailwind CSS is a utility-first CSS framework — you style elements by combining tiny, single-purpose class names directly in HTML.
  • No more naming classes — Tailwind's predefined classes describe what they do (text-xl, bg-blue-500, p-4) so naming is never a bottleneck.
  • Install via CDN for quick prototyping, or use the Tailwind CLI / PostCSS for production to get tree-shaking (only used classes in the final CSS).
  • Responsive design uses breakpoint prefixes: sm:, md:, lg:, xl:, 2xl: — mobile-first by default.
  • Dark mode is one prefix away: add dark: before any class to apply it in dark mode.
  • State variants like hover:, focus:, active:, and disabled: replace pseudo-class rules entirely.
  • Next chapter dives into the complete utility class system — spacing, sizing, colour, flexbox, grid, and typography in depth.

Quick Check — Questions & Answers

Test your understanding of this chapter. Click a question to reveal the answer.

"Utility-first" means you style elements by composing many single-purpose, low-level CSS classes (called utilities) rather than writing custom CSS in a separate stylesheet. Each class does exactly one thing — text-xl sets font-size, p-4 adds padding, bg-blue-500 sets the background colour. You combine them in your HTML to build any UI without ever leaving your markup file.
Use the md: prefix. Tailwind is mobile-first, so an unprefixed class applies to all screen sizes. md:text-xl means "apply text-xl only at screens ≥ 768px".

Example: <h1 class="text-base md:text-2xl lg:text-4xl">

This renders as text-base on mobile, text-2xl on medium, and text-4xl on large screens — all in one class list, no custom media queries.
CDN: Add one script tag — Tailwind loads entirely in the browser. Zero setup, but it loads the entire Tailwind framework (~3MB), which is not suitable for production.

CLI / PostCSS: Install via npm, configure tailwind.config.js, and run a build process. Tailwind scans your HTML files and outputs only the CSS classes you actually use — this can result in a stylesheet under 10KB. Essential for production, supports custom themes, plugins, and JIT (just-in-time) mode.
Tailwind supports two dark mode strategies:

1. Media strategy (default): Uses the OS/browser prefers-color-scheme media query. If the user's system is in dark mode, dark: classes apply automatically.

2. Class strategy: Set darkMode: 'class' in tailwind.config.js. Dark styles apply when a dark class is present on the <html> element — great for toggle buttons.

Usage: <div class="bg-white dark:bg-gray-900"> — the dark: prefix applies the style in dark mode only.
Not entirely — but it dramatically reduces the need. For the vast majority of UI work (layout, spacing, colours, typography, states, animations), Tailwind utilities cover everything. You may still write custom CSS for:

Complex animations that can't be expressed as utility classes
CSS variables / design tokens for dynamic theming
@layer components in Tailwind for reusable component classes
Third-party integrations that expect specific class names

The @apply directive in Tailwind also lets you compose utility classes inside your CSS when needed.

MCQ Quiz — Test Yourself

Select the best answer for each question. Correct answers are revealed after you click.

Question 1 of 4
Which of the following best describes Tailwind CSS?
Question 2 of 4
What does the class md:flex-col mean in Tailwind CSS?
Question 3 of 4
Which Tailwind class adds padding: 1rem (16px) on all sides?
Question 4 of 4
When using Tailwind's class dark mode strategy, which HTML element needs the dark class added?

Rate Us on Google

4.8 / 5
Enjoying this tutorial?
Your 5-star review helps other learners discover our free tutorials and encourages us to keep creating quality content.
80+ reviews
Write a Google Review
Join WhatsApp Channel