Spacing System
Consistent spacing creates visual rhythm, improves layout balance, and makes interfaces feel intentional. The UI Lab spacing system provides a modular scale derived from a base unit, ensuring predictable, harmonious layouts across all components and pages.
Spacing Scale
The spacing scale uses a base unit of 4px, with multipliers creating a proportional progression:
| Token | Pixels | REM | Use Case |
|---|---|---|---|
| **space-0.5** | 2px | 0.125rem | Minimal gaps, hairlines |
| **space-1** | 4px | 0.25rem | Tight spacing, icon spacing |
| **space-2** | 8px | 0.5rem | Button padding, small gaps |
| **space-3** | 12px | 0.75rem | Component padding, small margins |
| **space-4** | 16px | 1rem | Standard spacing, primary unit |
| **space-6** | 24px | 1.5rem | Section spacing, component margins |
| **space-8** | 32px | 2rem | Major section spacing |
| **space-12** | 48px | 3rem | Large section spacing |
| **space-16** | 64px | 4rem | Hero sections, major divisions |
| **space-20** | 80px | 5rem | Page-level spacing |
| **space-24** | 96px | 6rem | Full-width sections |
Why This Scale?
Mathematical Foundation
The spacing scale follows a 1.5x ratio between steps, creating proportional relationships:
- 4px → 6px → 8px → 12px → 16px → 24px → 32px → 48px...
This ratio:
- Creates visual harmony
- Feels natural to the eye
- Maintains mathematical relationships
- Reduces decision fatigue
Base Unit (4px)
Using 4px as the base unit:
- Aligns with most modern design systems
- Works well on all screen sizes
- Divides evenly into common sizes
- Supports sub-pixel rendering
Spacing Categories
Padding (Internal Spacing)
Space inside components between content and edges:
Component Padding
| Component | Padding |
|---|---|
| **Buttons** | space-2 (8px) horizontal, space-1.5 (6px) vertical |
| **Input Fields** | space-2 (8px) horizontal, space-1.5 (6px) vertical |
| **Cards** | space-4 (16px) |
| **Badges** | space-1 (4px) horizontal, space-0.5 (2px) vertical |
| **Modals** | space-6 (24px) |
| **Panels** | space-6 (24px) |
Margin (External Spacing)
Space outside components creating relationships between elements:
Element Margins
- Between buttons: space-2 (8px) horizontal
- Between input and label: space-1 (4px)
- Between heading and paragraph: space-2 (8px)
- Between form sections: space-6 (24px)
- Between page sections: space-12 (48px) to space-16 (64px)
Gap (Grid/Flex Spacing)
Space between items in grids and flex containers:
Grid Gaps
- Compact layout: space-2 (8px)
- Standard layout: space-4 (16px)
- Loose layout: space-6 (24px)
- Section grid: space-8 (32px)
Layout Patterns
Horizontal Spacing (Inline)
Spacing between elements on the same line:
Common patterns:
- Form elements: space-2 (8px)
- Navigation items: space-4 (16px)
- Card controls: space-2 (8px)
Vertical Spacing (Block)
Space between stacked elements:
Common patterns:
- Between heading and content: space-2 to space-3
- Between content sections: space-6
- Between major sections: space-12 to space-16
Nested Spacing
Space hierarchy within components:
Component Spacing Guidelines
Buttons
Form Elements
Cards
Lists
Navigation
Responsive Spacing
Spacing adapts for different screen sizes while maintaining the scale:
Desktop (1024px+)
- Use spacing scale as documented
- Generous margins on containers (space-6 to space-8)
- Full implementation of spacing hierarchy
Tablet (768px - 1023px)
- Reduce horizontal padding on containers
- Maintain vertical spacing hierarchy
- Adjust margins for narrower viewports
Mobile (< 768px)
- Reduce large spacing (space-16+) slightly
- Tighter horizontal padding (space-3 minimum)
- Maintain standard spacing for touch targets
- Increase vertical spacing for readability
Touch Target Sizing
Spacing considerations for touch interfaces:
Minimum Touch Target
- 44px × 44px recommended
- 40px × 40px minimum acceptable
- Includes padding around interactive element
Gap Between Touch Targets
- Minimum: 8px (space-2)
- Recommended: 12px (space-3)
- Prevents accidental taps
Example: Button with Spacing
CSS Variable Usage
All spacing values are available as CSS variables:
Practical Examples
Tailwind CSS Classes
Spacing is fully integrated with Tailwind:
Spacing Principles
1. Use the Scale
Always choose from the spacing scale. Don't use arbitrary values.
2. Create Visual Rhythm
Use consistent spacing to guide the eye through the interface.
3. Respect Containment
Space within containers uses smaller units; space between containers uses larger units.
4. Consider Content
Adjust spacing based on content density:
- Dense data: Tighter spacing (space-1 to space-2)
- Standard content: Standard spacing (space-4)
- Editorial content: Generous spacing (space-6 to space-8)
5. Test Readability
Spacing should improve readability and hierarchy, not hinder it.
Common Mistakes
Using Arbitrary Values
Inconsistent Spacing
Over-Spacing Content
Spacing in Different Contexts
Marketing/Landing Pages
- Generous spacing: space-6 to space-12
- Large gaps between sections
- Breathing room around content
Data-Heavy Interfaces
- Tighter spacing: space-2 to space-3
- Compact layouts
- Dense information display
Mobile Apps
- Conservative spacing: space-3 to space-4
- Touch-friendly (minimum 44px targets)
- Limited vertical space
Editorial Content
- Relaxed spacing: space-4 to space-6
- Generous line height
- Large paragraph gaps
Accessibility Considerations
Touch Target Spacing
- Ensure 44px minimum touch targets
- Maintain 8px+ gaps between targets
- Affects users on mobile and touch devices
Visual Spacing for Clarity
- Use spacing to group related content
- Separate different sections clearly
- Aid users with cognitive disabilities
Motor Control
- Adequate spacing between clickable elements
- Reduces accidental interactions
- Especially important for aging users
Integration with Other Design Tokens
Spacing works with other design tokens:
With Colors
Use spacing to separate content with different color backgrounds
With Typography
Spacing around text maintains hierarchy
With Shadows/Elevation
Spacing creates visual separation along with depth