--- 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
Description