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. - Styling React/HTML components with utility classes - Building responsive layouts with breakpoints
<button> Click me </button>
<div> <!-- Full width on mobile, half on tablet, third on desktop --> </div>
sm: - 640px and abovemd: - 768px and abovelg: - 1024px and abovexl: - 1280px and above2xl: - 1536px and above<div> <div>Left</div> <div>Center</div> <div>Right</div> </div> `Responsive flex direction:` <div> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div> `Common flex patterns:` <!-- Center content --> <div> <div>Centered Content</div> </div> <!-- Space between items --> <div> <span>Left</span> <span>Right</span> </div> <!-- Vertical stack with gap --> <div> <div>Item 1</div> <div>Item 2</div> </div>
<div> <div>Column 1</div> <div>Column 2</div> <div>Column 3</div> </div> `Responsive grid:` <div> <!-- 1 column mobile, 2 tablet, 4 desktop --> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> <div>Item 4</div> </div> `Auto-fit columns:` <div> <!-- Automatically fit columns based on container width --> </div>
<div> <!-- Centered content with padding --> </div> `Responsive max width:` <div> <!-- Max 448px width, centered --> </div>
<div>Padding all sides</div> <div>Margin all sides</div> `Individual sides:` <div> <!-- Top 1rem, Right 2rem, Bottom 1rem, Left 2rem --> </div> `Axis-based spacing:` <div> <!-- Horizontal padding 1rem, Vertical padding 2rem --> </div> `Responsive spacing:` <div> <!-- Increases padding at larger breakpoints --> </div> `Space between children:` <div> <div>Item 1</div> <div>Item 2</div> <div>Item 3</div> </div>
<h1>Large Heading</h1> <h2>Subheading</h2> <p>Body text</p> <small>Small text</small> `Responsive typography:` <h1> Responsive Heading </h1> `### Line Height & Letter Spacing` <p> Text with relaxed line height and wide letter spacing </p> `### Text Alignment` <p> Left aligned on mobile, centered on tablet+ </p>
<div>Blue background</div> <div>Light gray background</div> <div> Gradient background </div> `### Text Colors` <p>Dark text</p> <p>Blue text</p> <p>Error text</p> `### Opacity` <div> Semi-transparent blue </div>
<button> Hover me </button> <a> Hover link </a> `### Focus States` <input> `### Active & Disabled States` <button> Button </button> `### Group Hover` <div> <img src="image.jpg" /> <p>Hover the parent</p> </div>
<div> <img src="image.jpg" alt="Card image" /> <div> <h3>Card Title</h3> <p>Card description text goes here.</p> <button> Action </button> </div> </div> `### Responsive User Card` <div> <img src="profile.jpg" alt="Profile" /> <div> <div> Product Engineer </div> <h2> John Doe </h2> <p> Building amazing products with modern technology. </p> <button> Contact </button> </div> </div> `### Navigation Bar` <nav> <div> <div> <div> <a href="#">Logo</a> </div> <div> <a href="#">Home</a> <a href="#">About</a> <a href="#">Services</a> <a href="#">Contact</a> </div> <button> <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M4 6h16M4 12h16M4 18h16"></path> </svg> </button> </div> </div> </nav> `### Form Elements` <form> <div> <label> Email </label> <input type="email" placeholder="you@example.com" /> </div> <div> <label> Password </label> <input type="password" /> </div> <div> <input type="checkbox" /> <label>Remember me</label> </div> <button type="submit" > Sign In </button> </form> `### Modal/Dialog` <div> <div> <div> <h3>Modal Title</h3> <button> <svg fill="none" stroke="currentColor" viewBox="0 0 24 24"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12"></path> </svg> </button> </div> <p> Modal content goes here. </p> <div> <button> Cancel </button> <button> Confirm </button> </div> </div> </div>
<div> <!-- Hero Section --> <div> <div> <h1> Welcome to Our Site </h1> <p> Build amazing things with Tailwind CSS </p> <button> Get Started </button> </div> <div> <img src="hero.jpg" /> </div> </div> </div> `### Responsive Grid Gallery` <div> <div> <img src="image1.jpg" /> </div> <div> <img src="image2.jpg" /> </div> <!-- More items... --> </div>
<div> <h1>Title</h1> <p>Description</p> </div> `Enable dark mode in tailwind.config.js:` module.exports = { darkMode: 'class', // or 'media' // ... }
<button> Smooth transition </button> `### Transform Effects` <div> Scale on hover </div> <img /> `### Built-in Animations` <div>Spinning</div> <div>Pulsing</div> <div>Bouncing</div>
// 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` <!-- Use content-visibility for offscreen content --> <div> <div>Heavy content that's initially offscreen</div> </div> <!-- Optimize images with aspect-ratio --> <img src="video.jpg" alt="Video thumbnail" /> <!-- Use contain for paint optimization --> <div> Complex layout that doesn't affect outside elements </div> `### Development Performance` /* 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; } }
<!-- Custom focus styles that meet WCAG AA --> <button> Accessible Button </button> <!-- Skip links for keyboard navigation --> <a href="#main-content"> Skip to main content </a> `### Screen Reader Support` <!-- Semantic buttons with ARIA labels --> <button aria-label="Close dialog"> <svg fill="none" stroke="currentColor"> <path stroke-linecap="round" stroke-linejoin="round" stroke-width="2" d="M6 18L18 6M6 6l12 12" /> </svg> </button> <!-- Descriptive links --> <a href="/docs" aria-describedby="docs-description"> Documentation </a> <p id="docs-description"> Learn how to use our API and integration guides </p> `### Color Contrast` <!-- Ensure sufficient contrast ratios --> <div> High contrast text (WCAG AAA) </div> <div> Good contrast on colored backgrounds </div> <!-- Use contrast utilities for testing --> <div> Adjusts for high contrast mode </div> `### Motion Preferences` <!-- Respect prefers-reduced-motion --> <div> Doesn't animate when user prefers reduced motion </div> <!-- Conditional animations --> <div> Only animates when motion is preferred </div>
@theme directive for CSS-based configuration:/* 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)` /** @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+)` // vite.config.ts import { defineConfig } from 'vite' import tailwindcss from '@tailwindcss/vite' export default defineConfig({ plugins: [ tailwindcss(), ], })
<div> Using CSS custom properties directly </div> `### Enhanced Arbitrary Values` <!-- Complex grid with custom tracks --> <div> Responsive grid without custom CSS </div> <!-- Custom animation timing --> <div> Bounce with custom easing </div> `### Container Queries` <!-- Component that responds to its container size --> <div> <div> Text size based on container, not viewport </div> </div> `## Common Patterns with React/JSX` 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 ( <button className={`${baseClasses} ${variantClasses[variant]} ${sizeClasses[size]}`} > {children} </button> ); }