--- name: tailwindcss description: Guide for implementing Tailwind CSS - a utility-first CSS framework for rapid UI development. Use when styling applications with responsive design, dark mode, custom themes, or building design systems with Tailwind's utility classes. license: MIT version: 1.0.0 --- # Tailwind CSS Skill Tailwind CSS is a utility-first CSS framework that enables rapid UI development by providing pre-built utility classes. It generates optimized CSS at build-time by scanning your project files, resulting in zero runtime overhead and minimal production bundles. ## When to Use This Skill Use this skill when: - Building responsive layouts with mobile-first design - Implementing dark mode and theme customization - Creating custom design systems with consistent spacing, colors, and typography - Styling React, Vue, Svelte, or any web framework components - Prototyping interfaces with rapid visual feedback - Building production applications with optimized CSS bundles - Working with state-based styling (hover, focus, disabled, etc.) - Creating complex layouts with Grid and Flexbox utilities ## Core Concepts ### Utility-First Approach Tailwind provides low-level utility classes that you apply directly to HTML elements instead of writing custom CSS: ```html

Title

Title

``` **Benefits:** - No CSS file switching - styles live with markup - No naming conventions needed - Automatic dead code elimination - Consistent design tokens across team - Fast iteration without CSS bloat ### Build-Time Processing Tailwind scans your source files at build-time and generates only the CSS classes you actually use: ```javascript // Tailwind analyzes these files content: [ "./src/**/*.{js,jsx,ts,tsx}", "./app/**/*.{js,jsx,ts,tsx}", "./components/**/*.{js,jsx,ts,tsx}" ] ``` Result: Optimized production bundles with zero runtime overhead. ## Installation & Setup ### Modern Setup with Vite **Step 1: Install dependencies** ```bash npm install -D tailwindcss @tailwindcss/vite # or pnpm add -D tailwindcss @tailwindcss/vite # or yarn add -D tailwindcss @tailwindcss/vite ``` **Step 2: Configure Vite** ```javascript // vite.config.ts import { defineConfig } from 'vite' import tailwindcss from '@tailwindcss/vite' export default defineConfig({ plugins: [tailwindcss()] }) ``` **Step 3: Import in CSS** ```css /* src/index.css */ @import "tailwindcss"; ``` **Step 4: Reference stylesheet in HTML** ```html
Hello Tailwind!
``` ### Framework-Specific Setup **Next.js (App Router):** ```bash npx create-next-app@latest --tailwind ``` **Next.js (Manual):** ```javascript // tailwind.config.js module.exports = { content: [ "./app/**/*.{js,ts,jsx,tsx}", "./components/**/*.{js,ts,jsx,tsx}" ] } ``` **React with Vite:** ```bash npm create vite@latest my-app -- --template react npm install -D tailwindcss @tailwindcss/vite ``` **Vue:** ```bash npm install -D tailwindcss @tailwindcss/vite ``` **Svelte:** ```bash npm install -D tailwindcss @tailwindcss/vite ``` **Astro:** ```bash npx astro add tailwind ``` ### PostCSS Setup (Alternative) ```bash npm install -D tailwindcss postcss autoprefixer npx tailwindcss init -p ``` ```javascript // postcss.config.js export default { plugins: { tailwindcss: {}, autoprefixer: {} } } ``` ## Design System & Tokens ### Default Design System Tailwind includes a comprehensive default design system: - **Colors**: 18 color palettes with 11 shades each (50-950) - **Spacing**: Consistent scale from 0.25rem to 96rem - **Typography**: Font sizes, weights, line heights - **Breakpoints**: Mobile-first responsive system - **Shadows**: Elevation system for depth - **Border radius**: Rounded corners at different scales ### Customizing Theme Use the `@theme` directive in CSS: ```css @import "tailwindcss"; @theme { /* Custom colors */ --color-brand-50: oklch(0.97 0.02 264); --color-brand-500: oklch(0.55 0.22 264); --color-brand-900: oklch(0.25 0.15 264); /* Custom fonts */ --font-display: "Satoshi", "Inter", sans-serif; --font-body: "Inter", system-ui, sans-serif; /* Custom spacing */ --spacing-18: calc(var(--spacing) * 18); --spacing-navbar: 4.5rem; /* Custom breakpoints */ --breakpoint-3xl: 120rem; --breakpoint-4xl: 160rem; /* Custom shadows */ --shadow-glow: 0 0 20px rgba(139, 92, 246, 0.3); } ``` **Usage:** ```html
Custom themed element
``` ### Color System **Using default colors:** ```html
Background

Text

Border
``` **Color scale:** - 50: Lightest - 100-400: Light variations - 500: Base color - 600-800: Dark variations - 950: Darkest **Color opacity modifiers:** ```html
75% opacity
30% opacity
87% opacity
``` ## Utility Classes ### Layout **Display:** ```html
Block
Inline Block
Flex
Inline Flex
Grid
``` **Flexbox:** ```html
Item 1
Item 2
Vertical stack
``` **Grid:** ```html
Column 1
Column 2
Column 3
Flexible
Fixed 500px
More flexible
``` **Positioning:** ```html
Positioned
Fixed
Sticky header
``` ### Spacing **Padding & Margin:** ```html
Padding all sides
Padding X and Y
Padding top/bottom
Margin all sides
Center horizontally
Negative margin
``` **Gap (Flexbox/Grid):** ```html
Flex with gap
Grid with X/Y gap
``` ### Typography **Font Size:** ```html

Extra small

Small

Base (16px)

Large

Extra large

2XL

Heading

``` **Font Weight:** ```html

Light (300)

Normal (400)

Medium (500)

Semibold (600)

Bold (700)

``` **Text Alignment:** ```html

Left aligned

Center aligned

Right aligned

Justified

``` **Line Height:** ```html

Tight line height

Normal line height

Relaxed line height

``` **Combining font utilities:** ```html

Font size 4xl with tight line-height

``` ### Colors & Backgrounds **Background colors:** ```html
White background
Gray background
Gradient background
``` **Text colors:** ```html

Dark text

Blue text

Link ``` ### Borders ```html
Default border
2px border
Top and bottom borders
Rounded corners
Large rounded
Fully rounded
Combined
``` ### Shadows ```html
Small shadow
Medium shadow
Large shadow
Extra large shadow
No shadow
``` ## Responsive Design ### Mobile-First Breakpoints Tailwind uses a mobile-first approach. Base styles apply to all screen sizes, then use breakpoint prefixes to override at larger sizes: **Breakpoints:** - `sm:` - 640px and up - `md:` - 768px and up - `lg:` - 1024px and up - `xl:` - 1280px and up - `2xl:` - 1536px and up **Example:** ```html
Item 1
Item 2
Item 3
Item 4
Mobile only content

Responsive heading

``` ### Custom Breakpoints ```css @theme { --breakpoint-3xl: 120rem; --breakpoint-tablet: 48rem; } ``` ```html
Custom breakpoints
``` ### Max-width Queries ```html
Hidden on mobile
Only visible on tablets
``` ### Container Queries Style elements based on parent container width: ```html
Responds to parent width
``` ## Interactive States ### Hover States ```html Hover link
Scale on hover
``` ### Focus States ```html ``` ### Active States ```html ``` ### Disabled States ```html ``` ### Form States ```html ``` ### Group Hover (Parent State) ```html

Text changes when parent is hovered

``` ### Peer State (Sibling State) ```html ``` ## Dark Mode ### Setup Dark Mode **Media query approach (automatic):** ```html
Auto switches based on system preference
``` **Class-based approach (manual toggle):** ```javascript // Add .dark class to element document.documentElement.classList.toggle('dark') ``` ### Dark Mode Utilities ```html
Background

Text

Border

Dark mode card

Content adapts to theme

``` ### Dark Mode Toggle Implementation ```javascript // Store preference function toggleDarkMode() { const isDark = document.documentElement.classList.toggle('dark') localStorage.setItem('theme', isDark ? 'dark' : 'light') } // Initialize on load if (localStorage.theme === 'dark' || (!('theme' in localStorage) && window.matchMedia('(prefers-color-scheme: dark)').matches)) { document.documentElement.classList.add('dark') } ``` ## Arbitrary Values Use square brackets for one-off custom values: **Pixel values:** ```html
Custom position

Custom font size

Custom width
``` **Colors:** ```html
Custom hex color
RGB color
OKLCH color
``` **CSS variables:** ```html
CSS variable
Type hint
``` **Complex values:** ```html
Custom grid
Custom content
Custom property
``` ## Transitions & Animations ### Transitions ```html
Scale with transition
Multiple transitions
Fast
Normal
Slow
``` ### Transforms ```html
Scale
Rotate 45deg
Slight rotation
Move
Skew
Multiple transforms
``` ### Animations ```html
Spinning
Pinging
Pulsing
Bouncing
``` ### Custom Animations ```css @theme { --animate-slide-in: slide-in 0.5s ease-out; } @keyframes slide-in { from { transform: translateX(-100%); } to { transform: translateX(0); } } ``` ```html
Custom animation
``` ## Advanced Patterns ### Custom Utilities Create reusable utility classes: ```css @utility content-auto { content-visibility: auto; } @utility tab-* { tab-size: var(--tab-size-*); } ``` ```html
Custom utility
Custom tab size
``` ### Custom Variants ```css @custom-variant theme-midnight (&:where([data-theme="midnight"] *)); @custom-variant aria-checked (&[aria-checked="true"]); ``` ```html
Applies when data-theme="midnight"
``` ### Layer Organization ```css @layer base { h1 { @apply text-4xl font-bold; } body { @apply bg-white text-gray-900; } } @layer components { .btn { @apply px-4 py-2 rounded bg-blue-500 text-white hover:bg-blue-700; } .card { @apply bg-white rounded-lg shadow-md p-6; } } ``` ### Apply Directive Extract repeated utilities into CSS classes: ```css .btn-primary { @apply bg-blue-500 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded; } .input-field { @apply border border-gray-300 rounded-md px-3 py-2 focus:outline-none focus:ring-2 focus:ring-blue-500; } ``` ## Component Examples ### Button Component ```html ``` ### Card Component ```html
Card image

Card Title

Card description text goes here

``` ### Form Component ```html
``` ### Navigation Component ```html ``` ### Grid Layout ```html
Item 1
Item 2
Item 3
Item 4
``` ## Best Practices ### 1. Use Consistent Spacing Scale ```html
``` ### 2. Leverage Design Tokens ```html ) } ``` ### 5. Use @apply for Repeated Patterns ```css /* Only use @apply for truly repeated patterns */ @layer components { .card { @apply bg-white rounded-lg shadow-md p-6 hover:shadow-lg transition-shadow; } } ``` ### 6. Accessibility First ```html Accessible link ``` ### 7. Performance Optimization - Use PurgeCSS in production (automatic with modern setup) - Avoid dynamic class names: `class={`text-${color}-500`}` won't work - Use safelist for dynamic classes if needed ### 8. Dark Mode Consistency ```html
Complete dark mode support
``` ## Common Patterns ### Centering Content ```html
Content
Content
Content
Centered content
``` ### Full-Width Container with Max Width ```html
Content with consistent max width
``` ### Aspect Ratio Boxes ```html
Square
16:9 video
4:3 ratio
``` ### Truncate Text ```html

Long text that will be truncated with ellipsis...

Long text that will be truncated after 3 lines with ellipsis...

``` ### Smooth Scrolling ```html Smooth scroll to section ``` ## Troubleshooting ### Classes Not Working 1. **Check content configuration:** ```javascript // tailwind.config.js content: [ "./src/**/*.{js,jsx,ts,tsx}", // Add all file paths where you use Tailwind ] ``` 2. **Dynamic classes won't work:** ```javascript // ❌ Won't work const color = 'blue'
// ✅ Works
``` 3. **Specificity issues:** ```css /* Use !important sparingly */
Overrides other styles
``` ### Build Issues ```bash # Clear cache and rebuild rm -rf .next node_modules/.cache npm run dev ``` ### IntelliSense Not Working Install official extension: - VS Code: "Tailwind CSS IntelliSense" - Configure in `.vscode/settings.json`: ```json { "tailwindCSS.experimental.classRegex": [ ["cva\\(([^)]*)\\)", "[\"'`]([^\"'`]*).*?[\"'`]"] ] } ``` ## Resources - Official Documentation: https://tailwindcss.com/docs - Tailwind UI Components: https://tailwindui.com - Headless UI (unstyled components): https://headlessui.com - Tailwind Play (online playground): https://play.tailwindcss.com - Color Palette Generator: https://uicolors.app - Community Components: https://tailwindcomponents.com ## Framework Integration Examples ### React ```jsx export function Card({ title, description }) { return (

{title}

{description}

) } ``` ### Vue ```vue ``` ### Svelte ```svelte

{title}

{description}

``` ## Implementation Checklist When implementing Tailwind CSS: - [ ] Install `tailwindcss` and framework-specific plugin - [ ] Configure build tool (Vite/PostCSS/CLI) - [ ] Set up content paths in configuration - [ ] Import Tailwind in CSS file - [ ] Configure custom theme tokens (if needed) - [ ] Set up dark mode strategy - [ ] Install VS Code IntelliSense extension - [ ] Create reusable component patterns - [ ] Implement responsive breakpoints - [ ] Add accessibility focus states - [ ] Test dark mode across all components - [ ] Optimize for production build - [ ] Document custom utilities and components