--- name: tailwind-best-practices description: Tailwind CSS patterns and conventions, v4 first with v3.4 legacy and migration. Use when writing responsive designs, implementing dark mode, creating reusable component styles, configuring Tailwind, or migrating from v3 to v4. Triggers on tasks involving Tailwind classes, responsive design, dark mode, CSS styling, or "migrate to Tailwind v4". license: MIT metadata: author: agent-skills version: "2.0.0" tailwindVersion: "4.0+ (tested 4.3); 3.4 legacy" --- # Tailwind CSS Best Practices Comprehensive patterns for building consistent, maintainable interfaces with Tailwind CSS v4, with v3.4 kept as legacy and a full v3 → v4 migration path. Contains 30 rules covering responsive design, dark mode, component patterns, configuration, v4 setup and migration, and the utilities added in v4.1–4.3. ## Metadata - **Version:** 2.0.0 - **Framework:** Tailwind CSS v4.0+ (tested on 4.3); v3.4 legacy - **Rule Count:** 30 rules across 5 categories - **Last Updated:** November 2026 - **License:** MIT - **Documentation:** [tailwindcss.com/docs](https://tailwindcss.com/docs) ## Step 1: Detect Tailwind Version **Always check the version before giving any advice.** v3 and v4 are fundamentally different. Check `package.json` for the installed version: ```json { "tailwindcss": "^4.x" } // → v4 rules apply (default) { "tailwindcss": "^3.x" } // → v3 legacy rules apply ``` For minor-version features (4.1 text shadows, 4.2 logical utilities, 4.3 scrollbars), check the exact version with `npm ls tailwindcss`. Also check for these signals: | Signal | Version | |--------|---------| | `@import "tailwindcss"` in CSS | v4 | | `@tailwindcss/vite`, `@tailwindcss/postcss`, `@tailwindcss/cli` or `@tailwindcss/webpack` in dependencies | v4 | | `@theme {}` block in CSS | v4 | | `@tailwind base;` directives in CSS | v3 | | `tailwind.config.js` exists and no `@config` in CSS | v3 | | `tailwind.config.js` exists and CSS has `@config "..."` | v4 loading a legacy config (mid-migration) | A `tailwind.config.js` on its own does not prove v3: v4 ignores it unless the CSS loads it with `@config`. **If v4** (default): Apply `v4-` rules plus `resp-`/`responsive-`, `dark-`, `comp-`/`component-` rules. Use `@theme {}`, not `tailwind.config.js`. **If v3**: Apply `resp-`/`responsive-`, `dark-`, `comp-`/`component-`, `config-` rules and the v3 (legacy) sections. Mention that v4 is available. **If migrating v3 → v4**: Follow `v4-migration` directly. ## When to Apply Reference these guidelines when: - Writing responsive layouts - Implementing dark mode - Creating reusable component styles - Configuring Tailwind (v4 `@theme`, or v3 config) - Migrating a project from v3 to v4 - Setting up a new project with v4 ## Rule Categories by Priority | Priority | Category | Impact | Prefix | Rules | Version | |----------|----------|--------|--------|-------|---------| | 1 | Responsive Design | CRITICAL | `resp-`, `responsive-` | 6 | v4 / v3 | | 2 | Dark Mode | CRITICAL | `dark-` | 6 | v4 / v3 | | 3 | Component Patterns | HIGH | `comp-`, `component-` | 7 | v4 / v3 | | 4 | Custom Configuration | HIGH | `config-` | 6 | v3 legacy (v4 equivalents included) | | 5 | V4 & Migration | HIGH | `v4-` | 5 | v4 | ## Quick Reference ### 1. Responsive Design (CRITICAL) - `resp-mobile-first` - Mobile-first approach - `responsive-breakpoint-order` - Order breakpoints smallest to largest - `responsive-container-queries` - Container queries (`@container`, `@min-*`/`@max-*`, `@container-size`) - `responsive-fluid-typography` - Fluid type with `clamp()` and `--text-*` theme values - `responsive-aspect-ratio` - Aspect ratio utilities - `responsive-grid-system` - Responsive grid layouts ### 2. Dark Mode (CRITICAL) - `dark-setup` - Configure dark mode (v4 default is `prefers-color-scheme`) - `dark-class-strategy` - Manual toggle with `@custom-variant dark` / `darkMode: 'class'` - `dark-media-strategy` - Follow the system preference - `dark-color-scheme` - Semantic color tokens for both themes - `dark-custom-colors` - Custom palettes and pairing - `dark-transitions` - Smooth theme transitions ### 3. Component Patterns (HIGH) - `comp-clsx-cn` - Conditional classes with clsx + tailwind-merge - `component-btn-variants` - Button variants - `component-card-patterns` - Card patterns - `component-form-elements` - Form elements - `component-modals` - Modals and dialogs - `component-navigation` - Navigation - `component-tables` - Tables ### 4. Custom Configuration — v3 legacy (HIGH) - `config-extend-theme` - Extend vs override theme - `config-custom-colors` - Custom color palette - `config-custom-fonts` - Custom fonts - `config-custom-spacing` - Custom spacing - `config-plugins` - Using plugins (`@plugin` in v4) - `config-presets` - Sharing configuration (a theme CSS file in v4) ### 5. V4 & Migration (HIGH) - `v4-installation` - Install v4 with Vite, PostCSS, CLI or webpack; `@source`, `source()`, `@reference` - `v4-theme-configuration` - `@theme {}` in CSS; `@theme inline`; sharing a theme; `@config` as a bridge - `v4-custom-utilities` - `@utility`, `@custom-variant`, `@variant`, `@plugin`, `@layer components` - `v4-migration` - Step-by-step v3 → v4: renames, removals, changed defaults, syntax changes - `v4-new-utilities` - Built-ins from v4.1–4.3: text shadows, masks, pointer variants, logical properties, scrollbars ## Essential Patterns ### Mobile-First Responsive Design ```tsx // ✅ Mobile-first: start with mobile, add larger breakpoints

Content

// ❌ Don't think desktop-first
// Confusing ``` ### Dark Mode Implementation **v4 — CSS only, no config file:** ```css @import "tailwindcss"; /* Default: dark: follows prefers-color-scheme (the OS setting). For a manual toggle with a .dark class, override the variant: */ @custom-variant dark (&:where(.dark, .dark *)); ``` **v3 (legacy) — `tailwind.config.js`:** ```js module.exports = { darkMode: 'class' } ``` **Component — identical in both versions:** ```tsx

Title

Description

// Needs the @custom-variant line above (v4) or darkMode: 'class' (v3) function toggleDarkMode() { document.documentElement.classList.toggle('dark') } ``` ### Conditional Classes with clsx/cn ```tsx import { clsx, type ClassValue } from 'clsx' import { twMerge } from 'tailwind-merge' // cn utility - merges Tailwind classes intelligently export function cn(...inputs: ClassValue[]) { return twMerge(clsx(inputs)) } // Usage interface ButtonProps { variant?: 'primary' | 'secondary' | 'danger' size?: 'sm' | 'md' | 'lg' className?: string children: React.ReactNode } function Button({ variant = 'primary', size = 'md', className, children }: ButtonProps) { return ( ) } ``` ### Theme Configuration — v4 vs v3 **v4 — `app.css` only, no JS config:** ```css @import "tailwindcss"; @plugin "@tailwindcss/forms"; @theme { --color-primary-500: #0ea5e9; --color-primary-600: #0284c7; --font-sans: Inter, sans-serif; --spacing-header: 4rem; /* numeric values like p-18 need no config */ --breakpoint-3xl: 1920px; } ``` **v3 (legacy) — `tailwind.config.js`:** ```js /** @type {import('tailwindcss').Config} */ module.exports = { content: ['./resources/**/*.{blade.php,js,ts,jsx,tsx}'], darkMode: 'class', theme: { extend: { colors: { primary: { 500: '#0ea5e9', 600: '#0284c7' } }, fontFamily: { sans: ['Inter', 'sans-serif'] }, spacing: { '18': '4.5rem' }, }, }, plugins: [require('@tailwindcss/forms')], } ``` > See `v4-theme-configuration` and `v4-migration` rules for full details. ### Responsive Grid Layout ```tsx // Product grid - responsive columns
{products.map(product => ( ))}
// Dashboard layout - sidebar + main
{children}
``` ### Form Styling ```tsx
``` v4 notes: `shadow-xs` is v3's `shadow-sm`; buttons default to `cursor: default`, so add `cursor-pointer` where you want it; `user-invalid:` (v4.1+) shows errors only after the user interacts. ### Animations with Reduced Motion ```tsx // Respect user's motion preferences
Card content
// Custom animation
Content
``` ```css /* v4 — app.css */ @theme { --animate-fade-in: fade-in 0.3s ease-out; @keyframes fade-in { from { opacity: 0; } to { opacity: 1; } } } ``` ```js // v3 (legacy) — tailwind.config.js module.exports = { theme: { extend: { keyframes: { 'fade-in': { '0%': { opacity: '0' }, '100%': { opacity: '1' }, }, }, animation: { 'fade-in': 'fade-in 0.3s ease-out', }, }, }, } ``` ## How to Use Always run Step 1 (version detection) first, then read the relevant rule files: **v4 projects (default):** ``` rules/v4-installation.md rules/v4-theme-configuration.md rules/v4-custom-utilities.md rules/v4-new-utilities.md rules/dark-setup.md rules/comp-clsx-cn.md rules/resp-mobile-first.md ``` **v3 projects (legacy):** ``` rules/config-extend-theme.md rules/dark-setup.md rules/comp-clsx-cn.md rules/resp-mobile-first.md ``` **Migrating v3 → v4:** ``` rules/v4-migration.md ``` ## References - [Tailwind CSS Documentation](https://tailwindcss.com/docs) - Official documentation - [Upgrade Guide](https://tailwindcss.com/docs/upgrade-guide) - v3 → v4 changes - [Responsive Design Guide](https://tailwindcss.com/docs/responsive-design) - Mobile-first patterns - [Dark Mode Guide](https://tailwindcss.com/docs/dark-mode) - Theme implementation - [Theme Variables](https://tailwindcss.com/docs/theme) - `@theme` customization - [Detecting Classes in Source Files](https://tailwindcss.com/docs/detecting-classes-in-source-files) - `@source` and scanning - [Functions & Directives](https://tailwindcss.com/docs/functions-and-directives) - `@utility`, `@variant`, `@reference`, `@config`, `@plugin` - [Tailwind Plus](https://tailwindcss.com/plus) - Official component library (formerly Tailwind UI) - [Headless UI](https://headlessui.com) - Accessible components - [Heroicons](https://heroicons.com) - Icon library ## Ecosystem Tools - **Tailwind CSS IntelliSense** - VS Code autocomplete and linting - **Prettier Plugin** - Automatic class sorting - **tailwind-merge** - Conflict-free class merging - **clsx** - Conditional class utility - **CVA** - Component variant system ## License MIT License - See repository for full license text. This skill is part of the Agent Skills collection, providing AI-powered development assistance with industry best practices.