--- name: styling-with-tailwind description: Creates UIs using Tailwind CSS utility classes and shadcn/ui patterns. Covers CSS variables with OKLCH colors, component variants with CVA, responsive design, dark mode, and Tailwind v4 features. Use when building interfaces with Tailwind, styling shadcn/ui components, implementing themes, or working with utility-first CSS. --- # Styling with Tailwind CSS Build accessible UIs using Tailwind utility classes and shadcn/ui component patterns. ## Core Patterns ### CSS Variables for Theming shadcn/ui uses semantic CSS variables mapped to Tailwind utilities: ```css /* globals.css - Light mode */ :root { --background: oklch(1 0 0); --foreground: oklch(0.145 0 0); --primary: oklch(0.205 0 0); --primary-foreground: oklch(0.985 0 0); --muted: oklch(0.97 0 0); --muted-foreground: oklch(0.556 0 0); --border: oklch(0.922 0 0); --radius: 0.5rem; } /* Dark mode */ .dark { --background: oklch(0.145 0 0); --foreground: oklch(0.985 0 0); --primary: oklch(0.922 0 0); --primary-foreground: oklch(0.205 0 0); } /* Tailwind v4: Map variables */ @theme inline { --color-background: var(--background); --color-foreground: var(--foreground); --color-primary: var(--primary); } ``` **Usage in components:** ```tsx // Background colors omit the "-background" suffix
``` ### Component Variants with CVA Use `class-variance-authority` for component variants: ```tsx import { cva } from "class-variance-authority" const buttonVariants = cva( "inline-flex items-center justify-center gap-2 rounded-md text-sm font-medium transition-colors focus-visible:outline-none focus-visible:ring-1 disabled:pointer-events-none disabled:opacity-50", { variants: { variant: { default: "bg-primary text-primary-foreground shadow hover:bg-primary/90", destructive: "bg-destructive text-destructive-foreground hover:bg-destructive/90", outline: "border border-input bg-background hover:bg-accent", ghost: "hover:bg-accent hover:text-accent-foreground", link: "text-primary underline-offset-4 hover:underline", }, size: { default: "h-9 px-4 py-2", sm: "h-8 px-3 text-xs", lg: "h-10 px-8", icon: "size-9", }, }, defaultVariants: { variant: "default", size: "default", }, } ) // Usage ``` ### Responsive Design Mobile-first breakpoints: ```tsx // Stack on mobile, grid on tablet+
// Hide on mobile
// Different layouts per breakpoint
// Responsive text sizes

``` ### Dark Mode ```tsx // Use dark: prefix for dark mode styles
// Theme toggle component "use client" import { Moon, Sun } from "lucide-react" import { useTheme } from "next-themes" export function ThemeToggle() { const { theme, setTheme } = useTheme() return ( ) } ``` ## Common Component Patterns ### Card ```tsx

Title

Description

Content
Footer
``` ### Form Field ```tsx

Helper text

``` ### Badge ```tsx const badgeVariants = cva( "inline-flex items-center rounded-md border px-2.5 py-0.5 text-xs font-semibold transition-colors", { variants: { variant: { default: "border-transparent bg-primary text-primary-foreground shadow", secondary: "border-transparent bg-secondary text-secondary-foreground", destructive: "border-transparent bg-destructive text-destructive-foreground", outline: "text-foreground", }, }, } ) ``` ### Alert ```tsx
Alert Title
Description
``` ### Loading Skeleton ```tsx
``` ## Layout Patterns ### Centered Layout ```tsx
{/* Content */}
``` ### Sidebar Layout ```tsx
Content
``` ### Dashboard Grid ```tsx
Wide card Regular Regular Full width
``` ### Container with Max Width ```tsx
{/* Centered content */}
``` ## Accessibility Patterns ### Focus Visible ```tsx ``` **Overflow Wrap:** ```tsx // Break long words

verylongwordthatneedstowrap

URLs and long strings

``` ## OKLCH Colors Use OKLCH for better color perception: ```css /* Format: oklch(lightness chroma hue) */ --primary: oklch(0.205 0 0); --destructive: oklch(0.577 0.245 27.325); /* Benefits: perceptually uniform, consistent lightness across hues */ ``` ## Base Color Palettes shadcn/ui provides multiple base colors: ```css /* Neutral (default) - pure grayscale */ --primary: oklch(0.205 0 0); /* Zinc - cooler, blue-gray */ --primary: oklch(0.21 0.006 285.885); /* Slate - balanced blue-gray */ --primary: oklch(0.208 0.042 265.755); /* Stone - warmer, brown-gray */ --primary: oklch(0.216 0.006 56.043); ``` ## Best Practices ### Prefer Semantic Colors ```tsx // Good - uses theme
// Avoid - hardcoded
``` ### Group Related Utilities ```tsx
``` ### Avoid Arbitrary Values ```tsx // Prefer design tokens
// Avoid when unnecessary
``` ## Installation ```bash # Initialize shadcn/ui pnpm dlx shadcn@latest init # Add components pnpm dlx shadcn@latest add button card form # Add all components pnpm dlx shadcn@latest add --all ``` ## Troubleshooting **Colors not updating:** 1. Check CSS variable in globals.css 2. Verify @theme inline includes variable 3. Clear build cache **Dark mode not working:** 1. Verify ThemeProvider wraps app 2. Check suppressHydrationWarning on html tag 3. Ensure dark: variants defined **Tailwind v4 migration:** 1. Run `@tailwindcss/upgrade@next` codemod 2. Update CSS variables with hsl() wrappers 3. Change @theme to @theme inline 4. Install tw-animate-css ## Component Patterns For detailed component patterns see [components.md](components.md): - **Composition**: asChild pattern for wrapping elements - **Typography**: Heading scales, prose styles, inline code - **Forms**: React Hook Form with Zod validation - **Icons**: Lucide icons integration and sizing - **Inputs**: OTP, file, grouped inputs - **Dialogs**: Modal patterns and composition - **Data Tables**: TanStack table integration - **Toasts**: Sonner notifications - **CLI**: Complete command reference ## Resources See [theming.md](theming.md) for complete color system reference and examples. ## Summary Key concepts: - Use semantic CSS variables for theming - Apply CVA for component variants - Follow mobile-first responsive patterns - Implement dark mode with next-themes - Use OKLCH for modern color handling - Prefer Tailwind v4 features (size-*, @theme) - Always ensure accessibility with focus-visible, sr-only This skill focuses on shadcn/ui patterns with Tailwind CSS. For component-specific examples, refer to the official shadcn/ui documentation.