# Layout Patterns **Master the 5 essential layouts** that cover 80% of all interface needs. Learn when to use Grid vs Flex, and build responsive, consistent layouts every time. --- ## Table of Contents 1. [Grid vs Flex Decision Tree](#grid-vs-flex-decision-tree) 2. [The 5 Essential Patterns](#the-5-essential-patterns) 3. [Responsive Strategies](#responsive-strategies) 4. [Common Mistakes](#common-mistakes) 5. [Advanced Patterns](#advanced-patterns) --- ## Grid vs Flex Decision Tree Use this flowchart to choose between Grid and Flex: ``` ┌─────────────────────────────────────┐ │ Need equal-width columns? │ │ (e.g., 3 cards of same width) │ └──────────┬─YES──────────┬─NO────────┘ │ │ ▼ ▼ USE GRID Need 2D layout? (rows + columns) │ ┌────┴────┐ │YES │NO ▼ ▼ USE GRID USE FLEX ``` ### Quick Rules | Scenario | Solution | | --------------------------- | ------------------------------------------------------- | | **Equal-width columns** | Grid (`grid grid-cols-3`) | | **Flexible item sizes** | Flex (`flex gap-4`) | | **2D layout (rows + cols)** | Grid (`grid grid-cols-2 grid-rows-3`) | | **1D layout (row OR col)** | Flex (`flex` or `flex flex-col`) | | **Card grid** | Grid (`grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3`) | | **Navbar items** | Flex (`flex items-center gap-4`) | | **Sidebar + Content** | Flex (`flex gap-6`) | | **Form fields** | Flex column (`flex flex-col gap-4` or `space-y-4`) | --- ## The 5 Essential Patterns These 5 patterns cover 80% of all layout needs. Master these first. --- ### 1. Page Container Pattern **Use case**: Standard page layout with readable content width ```tsx

Page Title

Section Title Page content goes here
``` **Key Features:** - `container` - Responsive container with max-width - `mx-auto` - Center horizontally - `px-4` - Horizontal padding (mobile-friendly) - `py-8` - Vertical padding - `max-w-4xl` - Constrain content width for readability - `space-y-6` - Vertical spacing between children **When to use:** - Blog posts - Documentation pages - Settings pages - Any page with readable content **[See live example](/dev/layouts#page-container)** --- ### 2. Dashboard Grid Pattern **Use case**: Responsive card grid that adapts to screen size ```tsx

Dashboard

{items.map((item) => ( {item.title} {item.description}

{item.value}

))}
``` **Responsive behavior:** - **Mobile** (`< 768px`): 1 column - **Tablet** (`≥ 768px`): 2 columns - **Desktop** (`≥ 1024px`): 3 columns **Key Features:** - `grid` - Use CSS Grid - `grid-cols-1` - Default: 1 column (mobile-first) - `md:grid-cols-2` - 2 columns on tablet - `lg:grid-cols-3` - 3 columns on desktop - `gap-6` - Consistent spacing between items **When to use:** - Dashboards - Product grids - Image galleries - Card collections **[See live example](/dev/layouts#dashboard-grid)** --- ### 3. Form Layout Pattern **Use case**: Centered form with constrained width ```tsx
Login Enter your credentials to continue
``` **Key Features:** - `max-w-md` - Constrain form width (448px max) - `mx-auto` - Center the form - `space-y-4` - Vertical spacing between fields - `w-full` - Full-width button **Form width guidelines:** - **Short forms** (login, signup): `max-w-md` (448px) - **Medium forms** (profile, settings): `max-w-lg` (512px) - **Long forms** (checkout): `max-w-2xl` (672px) **When to use:** - Login/signup forms - Contact forms - Settings forms - Any single-column form **[See live example](/dev/layouts#form-layout)** --- ### 4. Sidebar Layout Pattern **Use case**: Sidebar navigation with main content area ```tsx
{/* Sidebar */} {/* Main Content */}

Page Title

{/* Content */}
``` **Key Features:** - `flex` - Horizontal layout - `w-64` - Fixed sidebar width (256px) - `flex-1` - Main content takes remaining space - `min-h-screen` - Full viewport height - `border-r` - Visual separator **Responsive strategy:** ```tsx // Mobile: Collapsible sidebar
{/* Sidebar - hidden on mobile */} {/* Main content - full width on mobile */}
{/* Content */}
// Add mobile menu button ``` **When to use:** - Admin dashboards - Settings pages - Documentation sites - Apps with persistent navigation **[See live example](/dev/layouts#sidebar-layout)** --- ### 5. Centered Content Pattern **Use case**: Single-column content with optimal reading width ```tsx

Article Title

Published on Nov 2, 2025

Article content with optimal line length for reading...

More content...

``` **Key Features:** - `max-w-2xl` - Optimal reading width (672px) - `mx-auto` - Center content - `prose` - Typography styles (if using @tailwindcss/typography) **Width recommendations:** - **Articles/Blogs**: `max-w-2xl` (672px) - **Documentation**: `max-w-3xl` (768px) - **Landing pages**: `max-w-4xl` (896px) or wider - **Forms**: `max-w-md` (448px) **When to use:** - Blog posts - Articles - Documentation - Long-form content **[See live example](/dev/layouts#centered-content)** --- ## Responsive Strategies ### Mobile-First Approach Always start with mobile layout, then enhance for larger screens: ```tsx // ✅ CORRECT - Mobile first
{/* Items */}
// ❌ WRONG - Desktop first
// Don't do this ``` ### Breakpoints | Breakpoint | Min Width | Typical Use | | ---------- | --------- | --------------------------- | | `sm:` | 640px | Large phones, small tablets | | `md:` | 768px | Tablets | | `lg:` | 1024px | Laptops, desktops | | `xl:` | 1280px | Large desktops | | `2xl:` | 1536px | Extra large screens | ### Responsive Grid Columns ```tsx // 1→2→3→4 progression (common) grid-cols-1 sm:grid-cols-2 lg:grid-cols-3 xl:grid-cols-4 // 1→2→3 progression (most common) grid-cols-1 md:grid-cols-2 lg:grid-cols-3 // 1→2 progression (simple) grid-cols-1 md:grid-cols-2 // 1→3 progression (skip 2) grid-cols-1 lg:grid-cols-3 ``` ### Responsive Text ```tsx // Heading sizes

Responsive Title

// Body text (usually doesn't need responsive sizes)

Body text stays consistent

``` --- ## Common Mistakes ### ❌ Mistake 1: Using Margins Instead of Gap ```tsx // ❌ WRONG - Children have margins
Item 1
Item 2
Item 3
{/* Last one has no margin */}
// ✅ CORRECT - Parent controls spacing
Item 1
Item 2
Item 3
``` ### ❌ Mistake 2: Fixed Widths Instead of Responsive ```tsx // ❌ WRONG - Fixed width, not responsive
Content
// ✅ CORRECT - Responsive width
Content
``` ### ❌ Mistake 3: Not Using Container ```tsx // ❌ WRONG - Content touches edges on large screens
Content spans full width on 4K screens
// ✅ CORRECT - Container constrains width
Content has maximum width
``` ### ❌ Mistake 4: Desktop-First Responsive ```tsx // ❌ WRONG - Desktop first
// ✅ CORRECT - Mobile first
``` ### ❌ Mistake 5: Using Flex for Equal Columns ```tsx // ❌ WRONG - Flex doesn't guarantee equal widths
Col 1
Col 2
Col 3
// ✅ CORRECT - Grid ensures equal widths
Col 1
Col 2
Col 3
``` **[See before/after examples](/dev/layouts)** --- ## Advanced Patterns ### Asymmetric Grid ```tsx // 2/3 - 1/3 split
Main content (2/3 width)
Sidebar (1/3 width)
``` ### Auto-fit Grid (Flexible columns) ```tsx // Columns adjust based on available space
Item 1 Item 2 Item 3 {/* Adds as many columns as fit */}
``` ### Sticky Sidebar ```tsx
{/* Scrollable content */}
``` ### Full-height Layout ```tsx
Header
Flexible content
Footer
``` --- ## Layout Checklist Before implementing a layout, ask: - [ ] **Responsive?** Does it work on mobile, tablet, desktop? - [ ] **Container?** Is content constrained on large screens? - [ ] **Spacing?** Using `gap` or `space-y`, not margins on children? - [ ] **Mobile-first?** Starting with mobile layout? - [ ] **Semantic?** Using appropriate HTML tags (main, aside, nav)? - [ ] **Accessible?** Proper heading hierarchy, skip links? --- ## Quick Reference ### Grid Cheat Sheet ```tsx // Basic grid grid grid-cols-3 gap-6 // Responsive grid grid grid-cols-1 md:grid-cols-2 lg:grid-cols-3 gap-6 // Asymmetric grid grid grid-cols-3 gap-6
...
// Auto-fit grid grid grid-cols-[repeat(auto-fit,minmax(250px,1fr))] gap-6 ``` ### Flex Cheat Sheet ```tsx // Horizontal flex flex gap-4 // Vertical flex flex flex-col gap-4 // Center items flex items-center justify-center // Space between flex items-center justify-between // Wrap items flex flex-wrap gap-4 ``` ### Container Cheat Sheet ```tsx // Standard container container mx-auto px-4 py-8 // Constrained width max-w-4xl mx-auto px-4 // Full width w-full px-4 ``` --- ## Next Steps - **Practice**: Build pages using the 5 essential patterns - **Explore**: [Interactive layout examples](/dev/layouts) - **Deep Dive**: [Spacing Philosophy](./04-spacing-philosophy.md) - **Reference**: [Quick Reference Tables](./99-reference.md) --- **Related Documentation:** - [Spacing Philosophy](./04-spacing-philosophy.md) - When to use margin vs padding vs gap - [Foundations](./01-foundations.md) - Spacing tokens and scale - [Quick Start](./00-quick-start.md) - Essential patterns **Last Updated**: November 2, 2025