Skip to content
Live

Advanced Tailwind CSS Patterns

Go beyond utility classes. Learn Tailwind's plugin system, custom design tokens, component extraction, and the patterns that scale to large codebases.

By Soren Lindqvist9 min read

Beyond the Basics

Most developers use Tailwind for its utility classes. The developers who get the most out of it understand its design system capabilities.

Custom Design Tokens

// tailwind.config.ts
export default {
  theme: {
    extend: {
      colors: {
        brand: {
          50: "#fff3ee",
          DEFAULT: "#f54300",
          900: "#3a1005",
        },
      },
      fontFamily: {
        serif: ["var(--font-playfair)", "Georgia", "serif"],
      },
      spacing: {
        "18": "4.5rem",
        "88": "22rem",
      },
    },
  },
};

The @layer Directive

@layer base {
  /* Reset and base styles */
  h1, h2, h3 { @apply font-serif; }
}

@layer components {
  /* Reusable component classes */
  .btn-primary {
    @apply px-6 py-3 bg-brand text-white font-medium rounded-xl
           hover:bg-brand-700 transition-colors shadow-lg shadow-brand/25;
  }
  
  .card {
    @apply bg-white dark:bg-gray-900 rounded-2xl border border-gray-100
           dark:border-gray-800 shadow-sm;
  }
}

@layer utilities {
  /* Custom utilities */
  .gradient-text {
    @apply bg-clip-text text-transparent;
    background-image: linear-gradient(135deg, #f54300, #ff6a25);
  }
}

Arbitrary Values

<!-- When you need a value not in the scale -->
<div class="w-[342px] top-[117px] bg-[#f54300]">

<!-- Arbitrary CSS properties -->
<div class="[mask-image:linear-gradient(to_bottom,black,transparent)]">

The clsx + tailwind-merge Pattern

import { clsx } from "clsx";
import { twMerge } from "tailwind-merge";

function cn(...inputs: ClassValue[]) {
  return twMerge(clsx(inputs));
}

// Now conflicting classes resolve correctly
cn("px-4 py-2", "px-6") // → "py-2 px-6" (not "px-4 py-2 px-6")

Responsive Design Patterns

<!-- Mobile-first, always -->
<div class="
  grid grid-cols-1        <!-- mobile: 1 column -->
  sm:grid-cols-2          <!-- tablet: 2 columns -->
  lg:grid-cols-3          <!-- desktop: 3 columns -->
  gap-4 sm:gap-6 lg:gap-8 <!-- gap scales too -->
">

Tailwind's real power is in its constraints. A finite design system produces more consistent UIs than unlimited CSS freedom.

Share

Written by

Soren Lindqvist

Engineer. Ships the TypeScript, Docker, and database pieces, usually after breaking them in production first.

css

CSS Grid: The Complete Guide

Master CSS Grid from first principles. Learn grid-template-areas, auto-placement, subgrid, and real-world layout patterns every developer needs.

Elena Marchetti10 min read
programming

How to Contribute to Open Source

Your first open source contribution doesn't have to be a major feature. A practical guide to finding projects, making meaningful contributions, and building your reputation.

Amara Osei7 min read
git

Git Workflows for Modern Teams

From trunk-based development to GitFlow — choose the right branching strategy for your team size, release cadence, and deployment model.

Soren Lindqvist8 min read