--- name: tailwindcss displayName: Tailwind CSS description: Tailwind CSS v4 utility-first styling patterns including responsive design, dark mode, and custom configuration. Use when styling with Tailwind, adding utility classes, configuring Tailwind, setting up dark mode, or customizing the theme. version: 1.0.0 --- # Tailwind CSS v4 Development Guidelines Best practices for using Tailwind CSS v4 utility classes effectively. **Note**: Tailwind CSS v4 (released January 2025) uses a CSS-first configuration approach. If you need v3 compatibility, tailwind.config.js is still supported. ## Core Principles 1. **Utility-First**: Use utility classes instead of custom CSS 2. **Mobile-First**: Design for mobile, then scale up with responsive modifiers 3. **Component Extraction**: Extract repeated patterns into components 4. **Consistent Spacing**: Use Tailwind's spacing scale 5. **Custom Configuration**: Extend the default theme for brand consistency ## Basic Utilities ### Layout ```tsx // Flexbox
Content
Sidebar
// Grid
1
2
3
// Positioning
Badge
``` ### Spacing ```tsx // Padding and Margin
{/* padding: 1rem, margin: 0.5rem */}
{/* padding-x: 1.5rem, padding-y: 1rem */}
{/* margin-top: 2rem, margin-bottom: 1rem */} // Space between children
{/* margin-bottom on all but last child */}
Item 1
Item 2
``` ### Typography ```tsx

Heading

Paragraph text with comfortable line height.

Label ``` ### Colors ```tsx // Text colors

Text

// Background colors
Button
// Border colors
Box
``` ## Responsive Design ### Breakpoints ```tsx // Mobile-first responsive classes
{/* Full width on mobile, half on medium screens, third on large */}

{/* Responsive text sizes */}

{/* Responsive grid */}
``` ### Container ```tsx
{/* Centered container with horizontal padding */}
{/* Responsive container padding */}
``` ## Component Patterns ### Button ```tsx // Variants ``` ### Card ```tsx

Card Title

Card content goes here.

``` ### Form Input ```tsx

We'll never share your email.

``` ## State Variants ### Hover, Focus, Active ```tsx Link ``` ### Group Hover ```tsx

Hover the container

``` ### Disabled ```tsx ``` ## Dark Mode ```css /* Tailwind v4: Configure in app/globals.css */ @import "tailwindcss"; @media (prefers-color-scheme: dark) { /* Or use class-based: .dark */ } ``` ```tsx // Usage (same as v3)

Title

Description

``` ## Custom Styles ### Arbitrary Values ```tsx
{/* Custom top value */}
{/* Custom color */}
{/* Custom grid template */} ``` ### @apply Directive ```css /* components/button.css */ .btn-primary { @apply px-4 py-2 bg-blue-600 text-white font-medium rounded-md; @apply hover:bg-blue-700 focus:outline-none focus:ring-2 focus:ring-blue-500; @apply disabled:opacity-50 disabled:cursor-not-allowed; } ``` ## Configuration ### Tailwind v4: CSS-First Configuration ```css /* app/globals.css */ @import "tailwindcss"; @theme { /* Custom colors */ --color-brand-50: #eff6ff; --color-brand-100: #dbeafe; --color-brand-900: #1e3a8a; /* Custom spacing */ --spacing-128: 32rem; /* Custom fonts */ --font-family-sans: 'Inter', sans-serif; /* Custom breakpoints */ --breakpoint-3xl: 1920px; } ``` ### Tailwind v3 Config (Still Supported) ```javascript // tailwind.config.js (optional in v4) module.exports = { content: [ './app/**/*.{js,ts,jsx,tsx,mdx}', './components/**/*.{js,ts,jsx,tsx,mdx}', ], theme: { extend: { colors: { brand: { 50: '#eff6ff', 100: '#dbeafe', 900: '#1e3a8a', } }, spacing: { '128': '32rem', }, fontFamily: { sans: ['Inter', 'sans-serif'], }, }, }, plugins: [ require('@tailwindcss/forms'), require('@tailwindcss/typography'), ], } ``` ## Plugins ### Official Plugins ```bash npm install @tailwindcss/forms npm install @tailwindcss/typography npm install @tailwindcss/aspect-ratio npm install @tailwindcss/container-queries ``` ```tsx // @tailwindcss/forms // @tailwindcss/typography

Article Title

Content...

``` ## Performance ### Automatic Content Detection Tailwind v4 automatically detects and scans all template files - no `content` configuration needed. ### Build Performance Tailwind v4 delivers 3.5x faster full builds (~100ms) compared to v3 using modern CSS features like `@property` and `color-mix()`. **Browser Requirements**: Safari 16.4+, Chrome 111+, Firefox 128+ ## Common Patterns ### Centered Content ```tsx
Centered content
``` ### Sticky Header ```tsx
``` ### Grid Layout ```tsx
{posts.map(post => ( ))}
``` ### Truncate Text ```tsx

This text will be truncated with ellipsis if too long

This text will show max 3 lines with ellipsis

``` ## Best Practices 1. **Use Consistent Spacing**: Stick to Tailwind's spacing scale 2. **Responsive by Default**: Always consider mobile-first design 3. **Extract Components**: Avoid repeating long class lists 4. **Use Theme Colors**: Define custom colors in config, not arbitrary values 5. **Leverage @apply Sparingly**: Prefer utility classes in JSX 6. **Enable Dark Mode**: Plan for dark mode from the start 7. **Use Plugins**: Leverage official plugins for common needs 8. **Optimize Production**: Ensure purge is configured correctly ## Additional Resources For detailed information, see: - [Utility Patterns](resources/utility-patterns.md) - [Component Library](resources/component-library.md) - [Configuration Guide](resources/configuration.md)