TechLead
Lesson 8 of 9
5 min read
Tailwind CSS

Component Patterns

Learn common UI component patterns and best practices for building reusable components with Tailwind CSS.

Component Patterns in Tailwind

While Tailwind is utility-first, you can still create reusable component patterns. Here are common UI components built entirely with Tailwind utilities.

Buttons

<!-- Primary Button -->
<button class="px-6 py-3 bg-blue-600 hover:bg-blue-700 active:bg-blue-800 text-white font-semibold rounded-lg shadow-md hover:shadow-lg transition-all duration-200">
  Primary Button
</button>

<!-- Secondary Button -->
<button class="px-6 py-3 bg-gray-200 hover:bg-gray-300 text-gray-800 font-semibold rounded-lg transition-colors">
  Secondary Button
</button>

<!-- Outline Button -->
<button class="px-6 py-3 border-2 border-blue-600 text-blue-600 hover:bg-blue-600 hover:text-white font-semibold rounded-lg transition-colors">
  Outline Button
</button>

<!-- Ghost Button -->
<button class="px-6 py-3 text-blue-600 hover:bg-blue-50 font-semibold rounded-lg transition-colors">
  Ghost Button
</button>

<!-- Icon Button -->
<button class="p-3 bg-gray-100 hover:bg-gray-200 rounded-full transition-colors">
  <svg class="w-5 h-5" fill="none" stroke="currentColor" viewBox="0 0 24 24">
    <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16" />
  </svg>
</button>

Cards

<!-- Basic Card -->
<div class="bg-white rounded-xl shadow-lg p-6 max-w-sm">
  <h3 class="text-xl font-bold text-gray-900 mb-2">Card Title</h3>
  <p class="text-gray-600">Card description goes here with some content.</p>
</div>

<!-- Card with Image -->
<div class="bg-white rounded-xl shadow-lg overflow-hidden max-w-sm">
  <img class="w-full h-48 object-cover" src="image.jpg" alt="Card image">
  <div class="p-6">
    <span class="text-sm text-blue-600 font-medium">Category</span>
    <h3 class="text-xl font-bold text-gray-900 mt-1 mb-2">Card Title</h3>
    <p class="text-gray-600 mb-4">Card description with more details.</p>
    <button class="text-blue-600 font-semibold hover:text-blue-700">
      Read more →
    </button>
  </div>
</div>

<!-- Horizontal Card -->
<div class="bg-white rounded-xl shadow-lg overflow-hidden flex max-w-2xl">
  <img class="w-48 object-cover" src="image.jpg" alt="Card image">
  <div class="p-6 flex flex-col justify-between">
    <div>
      <h3 class="text-xl font-bold text-gray-900 mb-2">Card Title</h3>
      <p class="text-gray-600">Horizontal card layout description.</p>
    </div>
    <button class="text-blue-600 font-semibold hover:text-blue-700 self-start">
      Learn more →
    </button>
  </div>
</div>

Form Inputs

<!-- Text Input -->
<div>
  <label class="block text-sm font-medium text-gray-700 mb-1">Email</label>
  <input
    type="email"
    class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none transition-all"
    placeholder="you@example.com"
  >
</div>

<!-- Input with Icon -->
<div class="relative">
  <div class="absolute inset-y-0 left-0 pl-3 flex items-center pointer-events-none">
    <svg class="h-5 w-5 text-gray-400" fill="none" stroke="currentColor" viewBox="0 0 24 24">
      <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M21 21l-6-6m2-5a7 7 0 11-14 0 7 7 0 0114 0z" />
    </svg>
  </div>
  <input
    type="text"
    class="w-full pl-10 pr-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none"
    placeholder="Search..."
  >
</div>

<!-- Select -->
<select class="w-full px-4 py-2 border border-gray-300 rounded-lg focus:ring-2 focus:ring-blue-500 focus:border-blue-500 outline-none bg-white">
  <option>Option 1</option>
  <option>Option 2</option>
  <option>Option 3</option>
</select>

<!-- Checkbox -->
<label class="flex items-center gap-2 cursor-pointer">
  <input type="checkbox" class="w-4 h-4 text-blue-600 rounded border-gray-300 focus:ring-blue-500">
  <span class="text-gray-700">Remember me</span>
</label>

Alerts

<!-- Success Alert -->
<div class="bg-green-50 border-l-4 border-green-500 p-4 rounded-r-lg">
  <div class="flex items-center">
    <svg class="w-5 h-5 text-green-500 mr-3" fill="currentColor" viewBox="0 0 20 20">
      <path fill-rule="evenodd" d="M10 18a8 8 0 100-16 8 8 0 000 16zm3.707-9.293a1 1 0 00-1.414-1.414L9 10.586 7.707 9.293a1 1 0 00-1.414 1.414l2 2a1 1 0 001.414 0l4-4z" clip-rule="evenodd" />
    </svg>
    <p class="text-green-700">Successfully saved!</p>
  </div>
</div>

<!-- Error Alert -->
<div class="bg-red-50 border-l-4 border-red-500 p-4 rounded-r-lg">
  <div class="flex items-center">
    <svg class="w-5 h-5 text-red-500 mr-3" fill="currentColor" viewBox="0 0 20 20">
      <path fill-rule="evenodd" d="M18 10a8 8 0 11-16 0 8 8 0 0116 0zm-7 4a1 1 0 11-2 0 1 1 0 012 0zm-1-9a1 1 0 00-1 1v4a1 1 0 102 0V6a1 1 0 00-1-1z" clip-rule="evenodd" />
    </svg>
    <p class="text-red-700">An error occurred!</p>
  </div>
</div>

<!-- Warning Alert -->
<div class="bg-yellow-50 border-l-4 border-yellow-500 p-4 rounded-r-lg">
  <p class="text-yellow-700">Warning: Please review before continuing.</p>
</div>

<!-- Info Alert -->
<div class="bg-blue-50 border-l-4 border-blue-500 p-4 rounded-r-lg">
  <p class="text-blue-700">Info: New features available!</p>
</div>

Badges

<span class="px-2 py-1 text-xs font-semibold bg-blue-100 text-blue-800 rounded-full">New</span>
<span class="px-2 py-1 text-xs font-semibold bg-green-100 text-green-800 rounded-full">Active</span>
<span class="px-2 py-1 text-xs font-semibold bg-red-100 text-red-800 rounded-full">Closed</span>
<span class="px-2 py-1 text-xs font-semibold bg-gray-100 text-gray-800 rounded-full">Draft</span>

Avatar

<!-- Image Avatar -->
<img class="w-10 h-10 rounded-full object-cover" src="avatar.jpg" alt="Avatar">

<!-- Initials Avatar -->
<div class="w-10 h-10 rounded-full bg-blue-500 flex items-center justify-center">
  <span class="text-white font-semibold">JD</span>
</div>

<!-- Avatar with Status -->
<div class="relative">
  <img class="w-10 h-10 rounded-full object-cover" src="avatar.jpg" alt="Avatar">
  <span class="absolute bottom-0 right-0 w-3 h-3 bg-green-500 border-2 border-white rounded-full"></span>
</div>

<!-- Avatar Group -->
<div class="flex -space-x-2">
  <img class="w-10 h-10 rounded-full border-2 border-white" src="avatar1.jpg" alt="">
  <img class="w-10 h-10 rounded-full border-2 border-white" src="avatar2.jpg" alt="">
  <img class="w-10 h-10 rounded-full border-2 border-white" src="avatar3.jpg" alt="">
  <div class="w-10 h-10 rounded-full border-2 border-white bg-gray-200 flex items-center justify-center text-sm font-medium text-gray-600">
    +5
  </div>
</div>

Modal

<!-- Modal Overlay -->
<div class="fixed inset-0 bg-black/50 flex items-center justify-center z-50">
  <!-- Modal Content -->
  <div class="bg-white rounded-xl shadow-2xl max-w-md w-full mx-4 overflow-hidden">
    <!-- Header -->
    <div class="px-6 py-4 border-b flex items-center justify-between">
      <h3 class="text-lg font-semibold text-gray-900">Modal Title</h3>
      <button class="p-1 hover:bg-gray-100 rounded">
        <svg class="w-5 h-5 text-gray-500" fill="none" stroke="currentColor" viewBox="0 0 24 24">
          <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" />
        </svg>
      </button>
    </div>
    <!-- Body -->
    <div class="px-6 py-4">
      <p class="text-gray-600">Modal content goes here...</p>
    </div>
    <!-- Footer -->
    <div class="px-6 py-4 bg-gray-50 flex justify-end gap-3">
      <button class="px-4 py-2 text-gray-700 hover:bg-gray-100 rounded-lg">Cancel</button>
      <button class="px-4 py-2 bg-blue-600 hover:bg-blue-700 text-white rounded-lg">Confirm</button>
    </div>
  </div>
</div>

Continue Learning