Tailwind CSS has revolutionized how developers approach styling web applications. Unlike traditional CSS frameworks that provide pre-built components, Tailwind offers utility-first classes that let you build custom designs without writing custom CSS. In this guide, we'll cover everything you need to know to get started with Tailwind CSS.
Tailwind CSS is a utility-first CSS framework that provides low-level utility classes to build custom designs directly in your markup. Instead of writing custom CSS, you compose styles using predefined classes.
/* Traditional CSS */
.button {
background-color: #3b82f6;
color: white;
padding: 0.5rem 1rem;
border-radius: 0.25rem;
font-weight: 600;
border: none;
cursor: pointer;
}
.button:hover {
background-color: #2563eb;
}
<!-- Tailwind CSS -->
<button class="bg-blue-500 text-white px-4 py-2 rounded font-semibold hover:bg-blue-600">
Click me
</button>
For rapid prototyping, you can use the CDN:
<!DOCTYPE html>
<html>
<head>
<script src="https://cdn.tailwindcss.com"></script>
</head>
<body>
<h1 class="text-3xl font-bold underline text-blue-600">
Hello Tailwind!
</h1>
</body>
</html>
For production projects:
# Install Tailwind CSS
npm install -D tailwindcss
npx tailwindcss init
# Configure your template paths in tailwind.config.js
# Add Tailwind directives to your CSS
/* styles.css */
@tailwind base;
@tailwind components;
@tailwind utilities;
<!-- Display -->
<div class="block">Block element</div>
<div class="inline-block">Inline-block element</div>
<div class="flex">Flexbox container</div>
<div class="grid">Grid container</div>
<div class="hidden">Hidden element</div>
<!-- Positioning -->
<div class="relative">Relative positioning</div>
<div class="absolute top-0 left-0">Absolute positioning</div>
<div class="fixed bottom-4 right-4">Fixed positioning</div>
<div class="sticky top-0">Sticky positioning</div>
<!-- Flex Container -->
<div class="flex justify-center items-center h-screen">
<div class="bg-blue-500 text-white p-4">Centered content</div>
</div>
<!-- Flex Direction -->
<div class="flex flex-col">Vertical layout</div>
<div class="flex flex-row">Horizontal layout</div>
<!-- Flex Wrap -->
<div class="flex flex-wrap">Wrapping items</div>
<div class="flex flex-nowrap">No wrapping</div>
<!-- Grid Container -->
<div class="grid grid-cols-3 gap-4">
<div class="bg-red-200 p-4">Item 1</div>
<div class="bg-green-200 p-4">Item 2</div>
<div class="bg-blue-200 p-4">Item 3</div>
</div>
<!-- Responsive Grid -->
<div class="grid grid-cols-1 md:grid-cols-2 lg:grid-cols-4 gap-6">
<div class="bg-gray-200 p-4">Responsive item</div>
</div>
<!-- Padding -->
<div class="p-4">Padding on all sides</div>
<div class="px-6 py-3">Horizontal and vertical padding</div>
<div class="pt-2 pr-4 pb-6 pl-8">Individual sides</div>
<!-- Margin -->
<div class="m-4">Margin on all sides</div>
<div class="mx-auto">Centered with auto horizontal margin</div>
<div class="mt-8 mb-4">Top and bottom margin</div>
<!-- Width -->
<div class="w-full">Full width</div>
<div class="w-1/2">Half width</div>
<div class="w-64">Fixed width (16rem)</div>
<div class="w-screen">Full screen width</div>
<!-- Height -->
<div class="h-32">Fixed height</div>
<div class="h-screen">Full screen height</div>
<div class="min-h-screen">Minimum screen height</div>
<!-- Font Size -->
<h1 class="text-4xl">Extra large heading</h1>
<h2 class="text-2xl">Large heading</h2>
<p class="text-base">Normal text</p>
<small class="text-sm">Small text</small>
<!-- Font Weight -->
<p class="font-light">Light text</p>
<p class="font-normal">Normal text</p>
<p class="font-semibold">Semi-bold text</p>
<p class="font-bold">Bold text</p>
<!-- Text Color -->
<p class="text-gray-900">Dark text</p>
<p class="text-blue-500">Blue text</p>
<p class="text-red-600">Red text</p>
<!-- Text Alignment -->
<p class="text-left">Left aligned</p>
<p class="text-center">Center aligned</p>
<p class="text-right">Right aligned</p>
<!-- Solid Backgrounds -->
<div class="bg-blue-500">Blue background</div>
<div class="bg-red-400">Red background</div>
<div class="bg-gray-100">Light gray background</div>
<!-- Gradient Backgrounds -->
<div class="bg-gradient-to-r from-blue-500 to-purple-600">
Gradient background
</div>
<div class="bg-gradient-to-br from-pink-400 via-red-500 to-yellow-500">
Multi-color gradient
</div>
<!-- Border Width -->
<div class="border">Default border</div>
<div class="border-2">Thick border</div>
<div class="border-t-4">Top border only</div>
<!-- Border Color -->
<div class="border border-gray-300">Gray border</div>
<div class="border-2 border-blue-500">Blue border</div>
<!-- Border Radius -->
<div class="rounded">Slightly rounded</div>
<div class="rounded-lg">Large rounded corners</div>
<div class="rounded-full">Fully rounded (circle)</div>
Tailwind uses mobile-first responsive design with breakpoint prefixes:
<!-- Responsive Classes -->
<div class="w-full md:w-1/2 lg:w-1/3">
<!-- Full width on mobile, half on tablet, third on desktop -->
</div>
<div class="text-sm md:text-base lg:text-lg">
<!-- Responsive text sizes -->
</div>
<div class="hidden md:block">
<!-- Hidden on mobile, visible on tablet and up -->
</div>
<!-- Responsive Grid -->
<div class="grid grid-cols-1 sm:grid-cols-2 md:grid-cols-3 lg:grid-cols-4">
<div class="bg-blue-200 p-4">Responsive grid item</div>
</div>
sm
: 640px and upmd
: 768px and uplg
: 1024px and upxl
: 1280px and up2xl
: 1536px and up<!-- Hover Effects -->
<button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">
Hover me
</button>
<!-- Focus States -->
<input class="border border-gray-300 focus:border-blue-500 focus:ring-2 focus:ring-blue-200 px-3 py-2 rounded">
<!-- Active States -->
<button class="bg-green-500 active:bg-green-700 text-white px-4 py-2 rounded">
Click me
</button>
<!-- Multiple States -->
<a href="#" class="text-blue-600 hover:text-blue-800 focus:underline active:text-blue-900">
Link with multiple states
</a>
<div class="max-w-sm rounded-lg shadow-lg bg-white overflow-hidden">
<img class="w-full h-48 object-cover" src="image.jpg" alt="Card image">
<div class="p-6">
<h3 class="text-xl font-semibold text-gray-900 mb-2">Card Title</h3>
<p class="text-gray-600 mb-4">Card description goes here...</p>
<button class="bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded">
Read More
</button>
</div>
</div>
<nav class="bg-white shadow-md">
<div class="max-w-7xl mx-auto px-4 sm:px-6 lg:px-8">
<div class="flex justify-between items-center h-16">
<div class="flex items-center">
<span class="text-xl font-bold text-gray-900">Logo</span>
</div>
<div class="hidden md:flex space-x-8">
<a href="#" class="text-gray-700 hover:text-blue-600">Home</a>
<a href="#" class="text-gray-700 hover:text-blue-600">About</a>
<a href="#" class="text-gray-700 hover:text-blue-600">Services</a>
<a href="#" class="text-gray-700 hover:text-blue-600">Contact</a>
</div>
</div>
</div>
</nav>
<form class="max-w-md mx-auto bg-white p-6 rounded-lg shadow-md">
<div class="mb-4">
<label class="block text-gray-700 text-sm font-bold mb-2">
Email Address
</label>
<input type="email"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
placeholder="Enter your email">
</div>
<div class="mb-6">
<label class="block text-gray-700 text-sm font-bold mb-2">
Password
</label>
<input type="password"
class="w-full px-3 py-2 border border-gray-300 rounded-md focus:outline-none focus:ring-2 focus:ring-blue-500 focus:border-transparent"
placeholder="Enter your password">
</div>
<button type="submit"
class="w-full bg-blue-500 hover:bg-blue-600 text-white font-bold py-2 px-4 rounded-md transition-colors">
Sign In
</button>
</form>
<!-- Using arbitrary values -->
<div class="w-[350px] h-[200px] bg-[#1da1f2]">
Custom dimensions and color
</div>
<!-- Custom spacing -->
<div class="mt-[17px] p-[13px]">
Custom spacing values
</div>
<!-- Dark mode classes -->
<div class="bg-white dark:bg-gray-900 text-black dark:text-white">
<h1 class="text-2xl font-bold">Dark mode support</h1>
<p class="text-gray-600 dark:text-gray-300">
This content adapts to dark mode
</p>
</div>
<!-- Good -->
<article class="bg-white rounded-lg shadow-md p-6">
<header class="mb-4">
<h2 class="text-2xl font-bold text-gray-900">Article Title</h2>
</header>
<main class="prose prose-gray">
<p>Article content...</p>
</main>
</article>
<!-- Define common patterns -->
<button class="btn-primary">Primary Button</button>
<button class="btn-secondary">Secondary Button</button>
<style>
.btn-primary {
@apply bg-blue-500 hover:bg-blue-600 text-white px-4 py-2 rounded font-semibold transition-colors;
}
.btn-secondary {
@apply bg-gray-200 hover:bg-gray-300 text-gray-900 px-4 py-2 rounded font-semibold transition-colors;
}
</style>
<!-- Use a consistent spacing scale -->
<div class="space-y-4">
<div class="p-4 bg-white rounded">Item 1</div>
<div class="p-4 bg-white rounded">Item 2</div>
<div class="p-4 bg-white rounded">Item 3</div>
</div>
<!-- Group related classes together -->
<div class="
flex items-center justify-between
w-full max-w-4xl mx-auto
bg-white rounded-lg shadow-md
p-6 mb-8
hover:shadow-lg transition-shadow
">
Content here
</div>
<!-- Horizontal centering -->
<div class="flex justify-center">
<div class="bg-blue-500 text-white p-4">Centered horizontally</div>
</div>
<!-- Vertical and horizontal centering -->
<div class="flex items-center justify-center min-h-screen">
<div class="bg-blue-500 text-white p-8 rounded-lg">
Perfectly centered
</div>
</div>
<img src="image.jpg"
alt="Description"
class="w-full h-auto object-cover rounded-lg">
<!-- Aspect ratio control -->
<div class="aspect-w-16 aspect-h-9">
<img src="image.jpg" alt="Description" class="object-cover">
</div>
Tailwind CSS offers a powerful utility-first approach to styling that can significantly speed up your development process. By composing styles from utility classes, you can:
Key Benefits:
Getting Started Tips:
Once you understand these basics, you'll be able to build beautiful, responsive interfaces quickly and efficiently with Tailwind CSS.
Ready to dive deeper? Check out the official Tailwind CSS documentation for advanced features and customization options.
Designed & Developed by Shivam
2025. All rights reserved.
Visitors --
Delhi, --:--