# Tailwind CSS Best Practices - Agent Guide This skill provides comprehensive Tailwind CSS patterns and best practices for AI coding agents. ## Skill Overview **Name:** tailwind-best-practices **Version:** 1.0.0 **Framework:** Tailwind CSS v3.4+ / v4.0+ **Rule Count:** 29 rules across 8 categories **License:** MIT ## When to Use This Skill Activate this skill when: - Writing or refactoring Tailwind CSS classes - Implementing responsive designs with breakpoints - Adding dark mode support to applications - Creating reusable component patterns - Configuring Tailwind theme customization (v3 config or v4 @theme) - Migrating from Tailwind v3 to v4 - Building forms, buttons, cards, tables, navigation - Optimizing Tailwind for production - Questions about Tailwind utilities and patterns ## Rule Categories ### 1. Responsive Design (CRITICAL - 6 rules) Mobile-first responsive patterns are fundamental to every Tailwind project. **Key Concepts:** - Mobile-first: Base styles apply to all screens, add breakpoints upward - Breakpoint order: `sm:` (640px) → `md:` (768px) → `lg:` (1024px) → `xl:` (1280px) → `2xl:` (1536px) - Container queries for component-scoped responsiveness - Fluid typography with `clamp()` for smooth scaling - Responsive grid systems with `grid-cols-{n}` **Common Patterns:** ```html
Content
``` **Rules:** `resp-mobile-first`, `responsive-breakpoint-order`, `responsive-container-queries`, `responsive-fluid-typography`, `responsive-aspect-ratio`, `responsive-grid-system` ### 2. Dark Mode (CRITICAL - 6 rules) Modern applications require seamless light/dark theme support. **Key Concepts:** - Class strategy (`darkMode: 'class'`) for manual toggle - Media strategy (`darkMode: 'media'`) for system preference - Semantic color naming for maintainable themes - Custom color palettes with full scales (50-950) - Smooth transitions between themes **Common Patterns:** ```html
Content adapts to theme
``` **Rules:** `dark-setup`, `dark-class-strategy`, `dark-media-strategy`, `dark-color-scheme`, `dark-custom-colors`, `dark-transitions` ### 3. Component Patterns (HIGH - 7 rules) Reusable component patterns for consistent UI. **Key Concepts:** - Use `clsx` + `tailwind-merge` (cn utility) for conditional classes - Component variants with proper type safety - Consistent button, card, form, table, modal patterns - Proper accessibility attributes - Responsive component behavior **Common Patterns:** ```tsx // Button with variants using cn utility import { cn } from '@/lib/utils' function Button({ variant = 'primary', size = 'md', className, children }) { return ( ) } ``` **Rules:** `comp-clsx-cn`, `component-btn-variants`, `component-card-patterns`, `component-form-elements`, `component-modals`, `component-navigation`, `component-tables` ### 4. Custom Configuration — v3 (HIGH - 6 rules) Extending Tailwind's v3 theme via `tailwind.config.js`. For v4, use `@theme {}` instead. **Key Concepts:** - Always `extend` theme, don't override (preserves defaults) - Use full color scales (50-950) for flexibility - Font families with proper fallbacks - Custom spacing for layout consistency - Plugins for extended functionality - Presets for shared configuration **Common Patterns:** ```js // tailwind.config.js - Proper theme extension const defaultTheme = require('tailwindcss/defaultTheme') module.exports = { darkMode: 'class', theme: { extend: { colors: { brand: { 50: '#eff6ff', 500: '#3b82f6', 950: '#172554', }, }, fontFamily: { sans: ['Inter var', ...defaultTheme.fontFamily.sans], }, spacing: { '18': '4.5rem', 'header': '4rem', }, }, }, plugins: [ require('@tailwindcss/forms'), require('@tailwindcss/typography'), ], } ``` **Rules:** `config-extend-theme`, `config-custom-colors`, `config-custom-fonts`, `config-custom-spacing`, `config-plugins`, `config-presets` ### 5. V4 & Migration (HIGH - 4 rules) Tailwind CSS v4 setup, configuration, and migration from v3. **Key Concepts:** - CSS-first architecture: `@import "tailwindcss"` replaces `@tailwind` directives - `@theme {}` replaces `tailwind.config.js` for design tokens - `@utility` and `@custom-variant` replace JS plugin API - `@variant` for nesting variants in custom CSS - `@reference` for importing without emitting CSS - `@source` / `@source not` for scan control - New features: `starting:`, `forced-colors:`, `color-mix()`, `transition-discrete` - Container queries and aspect-ratio built into core (no plugins) **Common Patterns:** ```css @import "tailwindcss"; @theme { --color-brand-500: #3b82f6; --font-sans: "Inter", sans-serif; } @utility scrollbar-hide { scrollbar-width: none; } @custom-variant hocus (&:hover, &:focus); ``` **Rules:** `v4-installation`, `v4-theme-configuration`, `v4-custom-utilities`, `v4-migration` ### 6. Spacing & Typography (MEDIUM - 0 rules) Consistent spacing and typography systems. **Key Concepts:** - Use Tailwind's spacing scale (0-96) - Custom spacing only when needed - Typography scale with line heights - Responsive text sizing - Vertical rhythm **Future Rules:** To be added based on common patterns ### 7. Animation (MEDIUM - 0 rules) Smooth transitions and animations. **Key Concepts:** - Transition utilities for state changes - Custom keyframe animations - Respect `prefers-reduced-motion` - Performance considerations **Future Rules:** To be added based on common patterns ### 8. Performance (LOW - 0 rules) Build and runtime optimization. **Key Concepts:** - Content configuration for tree-shaking - JIT mode benefits - Arbitrary value usage - Bundle size optimization **Future Rules:** To be added based on common patterns ## Tailwind CSS v4 Quick Reference When working with Tailwind v4, be aware of these key differences from v3: ### Setup ```css /* Single import replaces @tailwind directives */ @import "tailwindcss"; ``` ### Configuration ```css /* @theme replaces tailwind.config.js */ @theme { --color-brand-500: #3b82f6; --font-sans: "Inter", sans-serif; --breakpoint-3xl: 1920px; } ``` ### Custom Code ```css /* @utility replaces addUtilities() and @layer components */ @utility scrollbar-hide { scrollbar-width: none; } /* @custom-variant replaces addVariant() */ @custom-variant hocus (&:hover, &:focus); /* @variant for nesting in custom CSS */ .card { background: white; @variant dark { background: #1e293b; } } /* @plugin replaces require() in config */ @plugin "@tailwindcss/forms"; /* @reference for @apply without emitting CSS */ @reference "tailwindcss"; ``` ### New Variants and Features ```html
Range query
``` ### Plugins No Longer Needed - `@tailwindcss/aspect-ratio` — native `aspect-*` utilities - `@tailwindcss/container-queries` — native `@container` with range variants ## Agent Workflow ### 1. Analyze Requirements - Is this responsive? Use mobile-first approach - Does it need dark mode? Apply dark: variants - Is it a reusable component? Use cn utility - Custom colors/fonts? Extend theme properly ### 2. Write Classes - Start with base styles (display, sizing) - Add responsive breakpoints (mobile-first) - Include dark mode variants - Add interactive states (hover, focus, active) - Apply transitions for smooth UX ### 3. Component Pattern ```tsx // Standard component structure ``` ### 4. Verify Patterns - ✅ Mobile-first breakpoint order - ✅ Dark mode coverage - ✅ Accessibility (ARIA, semantic HTML) - ✅ Interactive states - ✅ No conflicting utilities - ✅ Proper transitions ## Essential Utilities Reference ### Layout ```html
``` ### Spacing ```html
``` ### Colors ```html

``` ### Typography ```html

``` ### Interactive States ```html