--- name: tailwind-css-patterns description: Comprehensive Tailwind CSS utility-first styling patterns including responsive design, layout utilities, flexbox, grid, spacing, typography, colors, and modern CSS best practices. Use when styling React/Vue/Svelte components, building responsive layouts, implementing design systems, or optimizing CSS workflow. language: html,tsx,jsx,vue,svelte framework: tailwindcss license: MIT --- # Tailwind CSS Development Patterns Expert guide for building modern, responsive user interfaces with Tailwind CSS utility-first framework. Covers v4.1+ features including CSS-first configuration, custom utilities, and enhanced developer experience. ## When to Use - Styling React/HTML components with utility classes - Building responsive layouts with breakpoints - Implementing flexbox and grid layouts - Managing spacing, colors, and typography - Creating custom design systems - Optimizing for mobile-first design - Building dark mode interfaces ## Core Concepts ### Utility-First Approach Apply styles directly in markup using utility classes: ```html ``` ### Responsive Design Mobile-first breakpoints with prefixes: ```html
``` Breakpoint prefixes: - `sm:` - 640px and above - `md:` - 768px and above - `lg:` - 1024px and above - `xl:` - 1280px and above - `2xl:` - 1536px and above ## Layout Utilities ### Flexbox Layouts Basic flex container: ```html
Left
Center
Right
``` Responsive flex direction: ```html
Item 1
Item 2
Item 3
``` Common flex patterns: ```html
Centered Content
Left Right
Item 1
Item 2
``` ### Grid Layouts Basic grid: ```html
Column 1
Column 2
Column 3
``` Responsive grid: ```html
Item 1
Item 2
Item 3
Item 4
``` Auto-fit columns: ```html
``` ### Container & Max Width Centered container with max width: ```html
``` Responsive max width: ```html
``` ## Spacing ### Padding & Margin Uniform spacing: ```html
Padding all sides
Margin all sides
``` Individual sides: ```html
``` Axis-based spacing: ```html
``` Responsive spacing: ```html
``` Space between children: ```html
Item 1
Item 2
Item 3
``` ## Typography ### Font Size & Weight ```html

Large Heading

Subheading

Body text

Small text ``` Responsive typography: ```html

Responsive Heading

``` ### Line Height & Letter Spacing ```html

Text with relaxed line height and wide letter spacing

``` ### Text Alignment ```html

Left aligned on mobile, centered on tablet+

``` ## Colors ### Background Colors ```html
Blue background
Light gray background
Gradient background
``` ### Text Colors ```html

Dark text

Blue text

Error text

``` ### Opacity ```html
Semi-transparent blue
``` ## Interactive States ### Hover States ```html Hover link ``` ### Focus States ```html ``` ### Active & Disabled States ```html ``` ### Group Hover ```html

Hover the parent

``` ## Component Patterns ### Card Component ```html
Card image

Card Title

Card description text goes here.

``` ### Responsive User Card ```html
Profile
Product Engineer

John Doe

Building amazing products with modern technology.

``` ### Navigation Bar ```html ``` ### Form Elements ```html
``` ### Modal/Dialog ```html

Modal Title

Modal content goes here.

``` ## Responsive Design Patterns ### Mobile-First Responsive Layout ```html

Welcome to Our Site

Build amazing things with Tailwind CSS

``` ### Responsive Grid Gallery ```html
``` ## Dark Mode ### Basic Dark Mode Support ```html

Title

Description

``` Enable dark mode in tailwind.config.js: ```javascript module.exports = { darkMode: 'class', // or 'media' // ... } ``` ## Animations & Transitions ### Basic Transitions ```html ``` ### Transform Effects ```html
Scale on hover
``` ### Built-in Animations ```html
Spinning
Pulsing
Bouncing
``` ## Performance Optimization ### Bundle Size Optimization Configure content sources for optimal purging: ```javascript // tailwind.config.js export default { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx,vue,svelte}", "./node_modules/@mycompany/ui-lib/**/*.{js,ts,jsx,tsx}", ], // Enable JIT for faster builds jit: true, } ``` ### CSS Optimization Techniques ```html
Heavy content that's initially offscreen
Video thumbnail
Complex layout that doesn't affect outside elements
``` ### Development Performance ```css /* Enable CSS-first configuration in v4.1 */ @import "tailwindcss"; @theme { /* Define once, use everywhere */ --color-brand: #3b82f6; --font-mono: "Fira Code", monospace; } /* Critical CSS for above-the-fold content */ @layer critical { .hero-title { @apply text-4xl md:text-6xl font-bold; } } ``` ## Accessibility Guidelines ### Focus Management ```html Skip to main content ``` ### Screen Reader Support ```html Documentation

Learn how to use our API and integration guides

``` ### Color Contrast ```html
High contrast text (WCAG AAA)
Good contrast on colored backgrounds
Adjusts for high contrast mode
``` ### Motion Preferences ```html
Doesn't animate when user prefers reduced motion
Only animates when motion is preferred
``` ## Best Practices 1. **Mobile-First**: Start with mobile styles, add responsive prefixes for larger screens 2. **Consistent Spacing**: Use Tailwind's spacing scale (4, 8, 12, 16, etc.) 3. **Color Palette**: Stick to Tailwind's color system for consistency 4. **Component Extraction**: Extract repeated patterns into components 5. **Utility Composition**: Prefer utility classes over @apply for better maintainability 6. **Semantic HTML**: Use proper HTML elements with Tailwind classes 7. **Performance**: Configure content paths correctly for optimal CSS purging 8. **Accessibility**: Include focus styles, ARIA labels, and respect user preferences 9. **CSS-First Config**: Use @theme directive for v4.1+ instead of JavaScript config 10. **Custom Utilities**: Create reusable utilities with @utility for complex patterns ## Configuration ### CSS-First Configuration (v4.1+) Use the `@theme` directive for CSS-based configuration: ```css /* src/styles.css */ @import "tailwindcss"; @theme { /* Custom colors */ --color-brand-50: #f0f9ff; --color-brand-500: #3b82f6; --color-brand-900: #1e3a8a; /* Custom fonts */ --font-display: "Inter", system-ui, sans-serif; --font-mono: "Fira Code", monospace; /* Custom spacing */ --spacing-128: 32rem; /* Custom animations */ --animate-fade-in: fadeIn 0.5s ease-in-out; /* Custom breakpoints */ --breakpoint-3xl: 1920px; } /* Define custom animations */ @keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } } /* Custom utilities */ @utility content-auto { content-visibility: auto; } ``` ### JavaScript Configuration (Legacy) ```javascript /** @type {import('tailwindcss').Config} */ export default { content: [ "./index.html", "./src/**/*.{js,ts,jsx,tsx,vue,svelte}", ], theme: { extend: { colors: { primary: { 50: '#f0f9ff', 500: '#3b82f6', 900: '#1e3a8a', }, }, fontFamily: { sans: ['Inter', 'system-ui', 'sans-serif'], }, spacing: { '128': '32rem', }, }, }, plugins: [], } ``` ### Vite Integration (v4.1+) ```javascript // vite.config.ts import { defineConfig } from 'vite' import tailwindcss from '@tailwindcss/vite' export default defineConfig({ plugins: [ tailwindcss(), ], }) ``` ## Advanced v4.1 Features ### Native CSS Custom Properties ```html
Using CSS custom properties directly
``` ### Enhanced Arbitrary Values ```html
Responsive grid without custom CSS
Bounce with custom easing
``` ### Container Queries ```html
Text size based on container, not viewport
``` ## Common Patterns with React/JSX ```tsx import { useState } from 'react'; function Button({ variant = 'primary', size = 'md', children }: { variant?: 'primary' | 'secondary'; size?: 'sm' | 'md' | 'lg'; children: React.ReactNode; }) { const baseClasses = 'font-semibold rounded transition'; const variantClasses = { primary: 'bg-blue-600 text-white hover:bg-blue-700', secondary: 'bg-gray-200 text-gray-800 hover:bg-gray-300', }; const sizeClasses = { sm: 'px-3 py-1 text-sm', md: 'px-4 py-2 text-base', lg: 'px-6 py-3 text-lg', }; return ( ); } ``` ## References - Tailwind CSS Docs: https://tailwindcss.com/docs - Tailwind UI: https://tailwindui.com - Tailwind Play: https://play.tailwindcss.com - Headless UI: https://headlessui.com