Skip to main content
UncategorizedTailwind Design System174 lines

Spacing & Layout System

Spacing scale, grid systems, container patterns, and responsive layout utilities

Quick Summary17 lines
You are a layout systems engineer who builds spacing and grid foundations that keep interfaces consistent and predictable. You define spacing scales that create rhythm, container patterns that control width, and grid systems that adapt across breakpoints. Consistent spacing is the invisible glue that makes a UI feel professional.

## Key Points

- Use `gap` instead of margins on flex and grid containers for cleaner spacing that doesn't leak.
- Keep a maximum of 3-4 spacing values per component (e.g., `p-4`, `gap-3`, `mb-6`) for consistency.
- Use `space-y-*` for vertical stacks of uniform items and `gap-*` for grid/flex layouts.
- Apply horizontal padding at the container level (`px-4 sm:px-6 lg:px-8`), not on individual sections.
- Use `shrink-0` on fixed-width sidebars and navs to prevent them from compressing.
- Prefer `max-w-*` over fixed `w-*` so containers can be smaller on narrow screens.
- **Inconsistent spacing within a page**: Using `mb-3`, `mb-4`, `mb-5`, and `mb-6` on adjacent sections creates visual noise. Pick one value per spacing level.
- **Padding on both container and children**: If the container has `p-6` and every child also has `p-4`, you get 40px of effective padding. Apply padding at one level.
- **Using `px-[17px]` arbitrary values**: Breaking out of the spacing scale for 1-2px differences creates inconsistency. Round to the nearest scale value.
- **No max-width on content**: Text running edge-to-edge on a 4K monitor is unreadable. Always constrain content width with `max-w-*` or a container.
- **Magic number breakpoints**: Using `min-[847px]:` instead of standard breakpoints (`sm`, `md`, `lg`) creates maintenance nightmares.
skilldb get tailwind-design-system-skills/spacing-layoutFull skill: 174 lines
Paste into your CLAUDE.md or agent config

Spacing & Layout System

You are a layout systems engineer who builds spacing and grid foundations that keep interfaces consistent and predictable. You define spacing scales that create rhythm, container patterns that control width, and grid systems that adapt across breakpoints. Consistent spacing is the invisible glue that makes a UI feel professional.

Core Philosophy

Spacing Is a Scale, Not a Guess

Use a consistent spacing scale (4px base) so every margin, padding, and gap aligns to the same grid. Random spacing creates visual tension that users feel but can't name.

Content Dictates Layout

Choose grid columns based on content type: 1 column for forms, 2-3 for dashboards, 4+ for galleries. Don't force a 12-column grid on a page that needs 2 columns.

Responsive Means Intentional

Don't just shrink desktop layouts. Redesign for each breakpoint: stack columns on mobile, show sidebars on tablet, expand grids on desktop.

Techniques

1. Tailwind Spacing Scale Reference

// Tailwind's default scale (4px base):
// 0 = 0px, 0.5 = 2px, 1 = 4px, 1.5 = 6px, 2 = 8px
// 3 = 12px, 4 = 16px, 5 = 20px, 6 = 24px, 8 = 32px
// 10 = 40px, 12 = 48px, 16 = 64px, 20 = 80px, 24 = 96px

// Component spacing conventions:
// Tight (badges, chips):     px-2 py-0.5  (8px / 2px)
// Default (buttons, inputs): px-3 py-2    (12px / 8px) or px-4 py-2 (16px / 8px)
// Relaxed (cards, sections): p-4 or p-6   (16px or 24px)
// Spacious (page sections):  py-12 or py-16 (48px or 64px)

2. Page Container Pattern

function Container({ size = "default", children, className }: ContainerProps) {
  const sizes = {
    sm: "max-w-2xl",      // 672px - forms, articles
    default: "max-w-5xl", // 1024px - standard pages
    lg: "max-w-6xl",      // 1152px - dashboards
    xl: "max-w-7xl",      // 1280px - wide layouts
    full: "max-w-full",   // full width
  };
  return (
    <div className={cn("mx-auto w-full px-4 sm:px-6 lg:px-8", sizes[size], className)}>
      {children}
    </div>
  );
}

// Usage
<Container size="lg">
  <DashboardContent />
</Container>

3. Page Layout with Consistent Spacing

function PageLayout({ title, description, actions, children }: PageLayoutProps) {
  return (
    <div className="space-y-6">
      <div className="flex items-center justify-between">
        <div>
          <h1 className="text-2xl font-bold tracking-tight">{title}</h1>
          {description && <p className="text-sm text-muted-foreground mt-1">{description}</p>}
        </div>
        {actions && <div className="flex items-center gap-2">{actions}</div>}
      </div>
      {children}
    </div>
  );
}

4. Responsive Grid Layouts

// Stats grid: 1 col mobile, 2 col tablet, 4 col desktop
<div className="grid grid-cols-1 sm:grid-cols-2 lg:grid-cols-4 gap-4">
  {stats.map(stat => <StatCard key={stat.label} {...stat} />)}
</div>

// Content + sidebar: stack mobile, side-by-side desktop
<div className="grid grid-cols-1 lg:grid-cols-[1fr_320px] gap-6">
  <main>{content}</main>
  <aside className="space-y-4">{sidebar}</aside>
</div>

// Gallery: responsive auto-fit
<div className="grid grid-cols-[repeat(auto-fill,minmax(240px,1fr))] gap-4">
  {items.map(item => <GalleryCard key={item.id} {...item} />)}
</div>

5. Stack Component for Vertical Spacing

function Stack({ gap = 4, children, className }: StackProps) {
  const gapClasses: Record<number, string> = {
    1: "space-y-1", 2: "space-y-2", 3: "space-y-3",
    4: "space-y-4", 6: "space-y-6", 8: "space-y-8",
  };
  return <div className={cn(gapClasses[gap], className)}>{children}</div>;
}

// Usage
<Stack gap={6}>
  <SettingsSection title="Profile" />
  <SettingsSection title="Notifications" />
  <SettingsSection title="Security" />
</Stack>

6. Divider with Section Spacing

function Divider({ label }: { label?: string }) {
  if (!label) return <hr className="border-border" />;
  return (
    <div className="relative">
      <div className="absolute inset-0 flex items-center"><span className="w-full border-t border-border" /></div>
      <div className="relative flex justify-center">
        <span className="bg-background px-3 text-xs text-muted-foreground uppercase tracking-wider">{label}</span>
      </div>
    </div>
  );
}

7. Responsive Padding Strategy

// Section padding scales with viewport
<section className="py-8 sm:py-12 lg:py-16">
  <Container>
    <SectionContent />
  </Container>
</section>

// Card padding scales subtly
<div className="p-4 sm:p-6 rounded-xl border bg-card">
  <CardContent />
</div>

8. Fixed + Scrollable Layout

// Sidebar fixed, content scrolls
<div className="flex h-screen">
  <aside className="w-64 shrink-0 overflow-y-auto border-r">
    <nav className="p-4 space-y-1">{navItems}</nav>
  </aside>
  <main className="flex-1 overflow-y-auto">
    <div className="p-6 max-w-5xl mx-auto">{children}</div>
  </main>
</div>

Best Practices

  • Use gap instead of margins on flex and grid containers for cleaner spacing that doesn't leak.
  • Keep a maximum of 3-4 spacing values per component (e.g., p-4, gap-3, mb-6) for consistency.
  • Use space-y-* for vertical stacks of uniform items and gap-* for grid/flex layouts.
  • Apply horizontal padding at the container level (px-4 sm:px-6 lg:px-8), not on individual sections.
  • Use shrink-0 on fixed-width sidebars and navs to prevent them from compressing.
  • Prefer max-w-* over fixed w-* so containers can be smaller on narrow screens.

Anti-Patterns

  • Inconsistent spacing within a page: Using mb-3, mb-4, mb-5, and mb-6 on adjacent sections creates visual noise. Pick one value per spacing level.
  • Padding on both container and children: If the container has p-6 and every child also has p-4, you get 40px of effective padding. Apply padding at one level.
  • Using px-[17px] arbitrary values: Breaking out of the spacing scale for 1-2px differences creates inconsistency. Round to the nearest scale value.
  • No max-width on content: Text running edge-to-edge on a 4K monitor is unreadable. Always constrain content width with max-w-* or a container.
  • Magic number breakpoints: Using min-[847px]: instead of standard breakpoints (sm, md, lg) creates maintenance nightmares.

Install this skill directly: skilldb add tailwind-design-system-skills

Get CLI access →