Tailwind CSS v4 Complete Guide

Walid EzzatWalid Ezzat
8 min read

Credit & Source: This comprehensive guide is based on the excellent YouTube tutorial "Tailwind CSS v4 Full Course 2025 | Master Tailwind in One Hour"
by [JavaScript Mastery].
I've expanded and organized the content into this detailed reference article for easier learning and future reference.


Introduction to Tailwind CSS

Tailwind CSS isn't just another styling framework—it's a utility-first ideology that fundamentally transforms how we approach web styling. Unlike traditional frameworks like Bootstrap or Material UI that provide pre-built components, Tailwind gives you complete control through small, reusable utility classes.

Why Choose Tailwind CSS?

The Kitchen vs. Fast Food Analogy:

  • Traditional UI frameworks = Fast food menus (they work, but everything looks the same)

  • Tailwind CSS = A fully stocked kitchen (gives you ingredients, not prepackaged meals)

Real-World Adoption

Major companies using Tailwind CSS in production:

  • OpenAI

  • Shopify

  • GitHub

  • Microsoft

  • NASA

  • The Verge

  • Loom

This isn't just a trend, it's a proven solution for enterprise-level applications.


How Tailwind CSS Works

Traditional CSS Approach

/* Traditional CSS */
.container {
  display: flex;
  justify-content: center;
  align-items: center;
}

.container-heading {
  font-size: 24px;
  color: blue;
}
<div class="container">
  <h1 class="container-heading">Hello World</h1>
</div>

Tailwind's Utility-First Approach

<div class="flex justify-center items-center">
  <h1 class="text-2xl text-blue-500">Hello World</h1>
</div>

Key Differences from Inline Styles

Tailwind is NOT inline styles because:

  1. Reusability: Utility classes can be reused across elements

  2. Pseudo-classes: Access to hover:, focus:, active: states

  3. Media queries: Responsive design with sm:, md:, lg: prefixes

  4. Maintainability: Consistent design system


Understanding the Fundamentals

Basic Utility Classes

Text Styling

<!-- Text alignment and sizing -->
<h1 class="text-center text-lg text-blue-400">Centered Large Blue Text</h1>

<!-- Font properties -->
<p class="font-mono font-extrabold">Monospace Extra Bold</p>

Spacing System

Tailwind uses a consistent spacing scale based on rem units:

<!-- Margin examples -->
<div class="mt-2">Margin top (0.5rem)</div>
<div class="mx-4">Margin left and right (1rem)</div>
<div class="my-8">Margin top and bottom (2rem)</div>

<!-- Padding examples -->
<div class="p-4">Padding all sides (1rem)</div>
<div class="px-6">Padding left and right (1.5rem)</div>
<div class="py-2">Padding top and bottom (0.5rem)</div>

Colors and Backgrounds

<!-- Background colors with shade variants -->
<div class="bg-violet-200 border-2 border-violet-600">
  <p class="text-cyan-400">Colored text with colored container</p>
</div>

Dimensions

<!-- Width and height -->
<div class="w-full h-10">Full width, fixed height</div>
<div class="w-16 h-16 rounded-full">16x16 circle</div>

Understanding Margin vs Padding

Visual Example:

<div class="bg-blue-200 p-6 m-4 border-2 border-blue-500">
  <!-- 
  - m-4: External spacing (pushes element away from others)
  - p-6: Internal spacing (space inside the element)
  - Content appears here
  -->
  <p>Content with internal padding</p>
</div>

The Just-In-Time (JIT) Compiler

One of Tailwind's superpowers is its JIT compiler, which generates styles on demand and supports arbitrary values.

Arbitrary Values

<!-- Custom font sizes -->
<h1 class="text-[13px]">Custom 13px text</h1>
<h2 class="text-[20px]">Custom 20px text</h2>

<!-- Custom colors -->
<div class="bg-[#ff6b6b] text-[#ffffff]">Custom hex colors</div>

<!-- Custom spacing -->
<div class="p-[16px] m-[24px]">Custom pixel values</div>

Benefits of JIT

  • Performance: Only generates CSS for classes you use

  • Faster builds: No pre-compilation of thousands of unused classes

  • Arbitrary values: Use any custom value you need

  • Works seamlessly: No extra setup required


Layouts & Flexbox

CSS Display Properties

Flexbox Layouts

<!-- Basic flex container -->
<div class="flex justify-center items-center space-x-6">
  <div class="w-16 h-16 bg-blue-500 rounded-full"></div>
  <div class="w-16 h-16 bg-orange-500 rounded-full"></div>
  <div class="w-16 h-16 bg-green-500 rounded-full"></div>
</div>

<!-- Flex direction column -->
<div class="flex flex-col justify-center items-center space-y-6">
  <!-- Vertical layout -->
</div>

Flexbox Properties Reference

  • justify-center: Center items on the main axis. (if flex row then the X axis, if flex col then the Y axis)

  • justify-between: Space items evenly with space between

  • justify-around: Space items evenly with space around

  • items-center: Center items on the axis, Perpendicular to the main axis.

  • items-start: Align items to start

  • items-end: Align items to end

CSS Grid Layouts

<!-- 3-column grid -->
<div class="grid grid-cols-3 gap-2 mt-2 mx-2">
  <div class="w-16 h-16 bg-blue-500 rounded-full"></div>
  <div class="w-16 h-16 bg-orange-500 rounded-full"></div>
  <div class="w-16 h-16 bg-green-500 rounded-full"></div>
</div>

<!-- 5-column grid -->
<div class="grid grid-cols-5 gap-4">
  <!-- Grid items automatically fill available space -->
</div>

Positioning

<!-- Relative positioning -->
<div class="relative">
  <div class="absolute top-0 right-0">Positioned absolutely</div>
</div>

<!-- Fixed positioning -->
<div class="fixed top-0 w-full bg-red-500">Fixed header</div>

<!-- Sticky positioning -->
<div class="sticky top-0 bg-white">Sticky navigation</div>

Media Queries & Responsive Design

Tailwind uses a mobile-first approach with predefined breakpoints:

Breakpoint System

  • sm: 640px and up

  • md: 768px and up

  • lg: 1024px and up

  • xl: 1280px and up

  • 2xl: 1536px and up

Mobile-First Examples

<!-- Mobile first: starts with base styles, then adds larger screen styles -->
<div class="bg-blue-500 sm:bg-amber-600 md:bg-green-700 lg:bg-purple-800">
  <!-- 
  - Default (mobile): blue background
  - Small screens+: amber background  
  - Medium screens+: green background
  - Large screens+: purple background
  -->
</div>

<!-- Responsive text sizing -->
<h1 class="text-lg sm:text-xl md:text-2xl lg:text-3xl">
  Responsive heading
</h1>

<!-- Show/hide elements -->
<div class="hidden md:block">
  <!-- Hidden on mobile, visible on medium screens and up -->
</div>

Max-Width Queries

<!-- Use max- prefix for upper limits -->
<div class="bg-red-500 max-md:bg-blue-500">
  <!-- Blue on screens smaller than 768px, red on larger screens -->
</div>

Important Mobile-First Principles

  1. Don't use sm: For mobile - use unprefixed utilities

  2. Start with mobile design, then layer on larger screen changes


Dark Mode Implementation

System-Based Dark Mode

<!-- Automatically adapts to user's system preference -->
<div class="bg-white text-black dark:bg-black dark:text-white">
  <h1 class="text-blue-600 dark:text-blue-400">Adaptive heading</h1>
  <p class="text-gray-800 dark:text-gray-200">Adaptive paragraph</p>
</div>

Custom Styles & Reusability

Arbitrary Values for One-Off Customizations

<!-- Custom colors -->
<div class="bg-[#ff6b6b] text-[#ffffff]">Custom brand colors</div>

<!-- Custom spacing -->
<div class="p-[16px] m-[24px]">Exact pixel spacing</div>

<!-- Custom font sizes -->
<h1 class="text-[36px]">Custom font size</h1>

Tailwind v4 CSS Configuration

Adding Custom Colors

css

@theme {
  --color-chestnut: #cd853f;
  --color-brand-primary: #3b82f6;
  --color-brand-secondary: #10b981;
}
<!-- Use your custom colors -->
<div class="bg-chestnut text-brand-primary">
  Custom branded element
</div>

Adding Custom Fonts

@theme {
  --font-display: 'Playfair Display', serif;
  --font-body: 'Inter', sans-serif;
}

Component Layer for Reusability

Creating Component Classes

@layer components {
  .card {
    @apply bg-white rounded-lg shadow-lg p-6 border border-gray-200;
  }

  .btn-primary {
    @apply bg-blue-500 hover:bg-blue-600 text-white font-semibold py-2 px-4 rounded transition-colors;
  }

  .btn-secondary {
    @apply bg-gray-200 hover:bg-gray-300 text-gray-800 font-semibold py-2 px-4 rounded transition-colors;
  }
}

Using Component Classes

<div class="card">
  <h3 class="text-xl font-bold mb-4">Card Title</h3>
  <p class="text-gray-600 mb-4">Card content goes here.</p>
  <div class="flex space-x-3">
    <button class="btn-primary">Primary Action</button>
    <button class="btn-secondary">Secondary Action</button>
  </div>
</div>

Base Layer for Global Styles

@layer base {
  h1 {
    @apply text-3xl font-bold text-gray-900 mb-4;
  }

  h2 {
    @apply text-2xl font-semibold text-gray-800 mb-3;
  }

  p {
    @apply text-gray-600 leading-relaxed mb-4;
  }

  button {
    @apply font-medium transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2;
  }
}

Utilities Layer for Custom Utilities

@layer utilities {
  .flex-center {
    @apply flex justify-center items-center;
  }

  .text-balance {
    text-wrap: balance;
  }

  .scrollbar-hide {
    -ms-overflow-style: none;
    scrollbar-width: none;
  }

  .scrollbar-hide::-webkit-scrollbar {
    display: none;
  }
}

Best Practices

1. Component Organization

// React component example
const Button = ({ variant = 'primary', size = 'md', children, ...props }) => {
  const baseClasses = 'font-semibold rounded transition-colors focus:outline-none focus:ring-2 focus:ring-offset-2';

  const variants = {
    primary: 'bg-blue-500 hover:bg-blue-600 text-white focus:ring-blue-500',
    secondary: 'bg-gray-200 hover:bg-gray-300 text-gray-800 focus:ring-gray-500',
    danger: 'bg-red-500 hover:bg-red-600 text-white focus:ring-red-500'
  };

  const sizes = {
    sm: 'px-3 py-1.5 text-sm',
    md: 'px-4 py-2',
    lg: 'px-6 py-3 text-lg'
  };

  const classes = `${baseClasses} ${variants[variant]} ${sizes[size]}`;

  return (
    <button className={classes} {...props}>
      {children}
    </button>
  );
};

2. Consistent Spacing Scale

<!-- Use consistent spacing throughout your app -->
<div class="space-y-6">
  <section class="mb-8">
    <h2 class="mb-4">Section Title</h2>
    <div class="grid gap-6">
      <!-- Content with consistent 6-unit spacing -->
    </div>
  </section>
</div>

3. Color System Organization

css

@theme {
  /* Brand colors */
  --color-brand-50: #eff6ff;
  --color-brand-100: #dbeafe;
  --color-brand-500: #3b82f6;
  --color-brand-600: #2563eb;
  --color-brand-900: #1e3a8a;

  /* Semantic colors */
  --color-success: #16a34a;
  --color-warning: #ca8a04;
  --color-error: #dc2626;

  /* Gray scale */
  --color-gray-50: #f9fafb;
  --color-gray-100: #f3f4f6;
  --color-gray-500: #6b7280;
  --color-gray-900: #111827;
}

4. Responsive Design Patterns

html

<!-- Card grid that adapts to screen size -->
<div class="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 gap-6">
  <!-- Cards automatically flow to new rows -->
</div>

<!-- Sidebar layout -->
<div class="lg:grid lg:grid-cols-12 lg:gap-x-5">
  <aside class="lg:col-span-3">
    <!-- Sidebar content -->
  </aside>
  <main class="lg:col-span-9">
    <!-- Main content -->
  </main>
</div>

5. Performance Optimization

  • Use the JIT compiler to keep the CSS bundle small

  • Purge unused styles in production

  • Use arbitrary values sparingly - prefer configured theme values

  • Group related utilities logically in your HTML

6. Maintainability Tips

  • Create component classes for repeated patterns

  • Use semantic color names in your theme

  • Document custom utilities and components

  • Establish naming conventions for your team

  • Use TypeScript for better autocomplete with component variants


Conclusion

Tailwind CSS v4 represents a mature, powerful approach to styling modern web applications. By embracing the utility-first methodology, you gain:

  • Faster development through reusable utility classes

  • Better maintainability with a consistent design system

  • Superior customization without fighting against framework defaults

  • Excellent performance with automatic CSS optimization

  • Great developer experience with excellent tooling and documentation


Hope that helps!

0
Subscribe to my newsletter

Read articles from Walid Ezzat directly inside your inbox. Subscribe to the newsletter, and don't miss out.

Written by

Walid Ezzat
Walid Ezzat

I love communicating with the computer ; .Making this blog to document my studying #notes, #summaries, and professional journey in the tech field. Buy me a PIZZA! :D https://www.buymeacoffee.com/walidezzat