# Spacing, layout & other foundations

> The remaining foundations of a design system: spacing scale and semantic spacing, grids and breakpoints, sizing, radius, elevation, z-index, motion tokens and logical properties for RTL.

Web page: https://odeocreative.me/tutorials/design-systems/spacing-layout
Author: Hecham Gazhi (https://odeocreative.me)

Track: [Design System Tutorial](/md/tutorials/design-systems.md)

## Spacing scale

A design system defines a fixed **spacing scale**, usually based on 4 or 8 pixels. Every padding, margin and gap comes from this scale. It makes layouts consistent and removes hundreds of tiny decisions.

| Token | Value | Common use |
| --- | --- | --- |
| space-0 | 0 | Reset |
| space-1 | 4px | Icon to text, tight gaps |
| space-2 | 8px | Inside small components, between related items |
| space-3 | 12px | Input padding, list item gaps |
| space-4 | 16px | Card padding (mobile), default gap |
| space-6 | 24px | Card padding, gaps between groups |
| space-8 | 32px | Between sections inside a page |
| space-12 | 48px | Between major sections |
| space-16 | 64px | Page sections on desktop |
| space-24 | 96px | Large marketing sections |

## Semantic spacing

Just like colour, some systems add **semantic spacing tokens** that describe purpose, mapped to the scale. They make intent clearer and allow density changes (comfortable vs compact) by remapping.

```css
:root {
  --space-inset-sm: var(--space-2);   /* padding inside small components */
  --space-inset-md: var(--space-4);   /* padding inside cards */
  --space-stack-sm: var(--space-2);   /* vertical gap between related items */
  --space-stack-lg: var(--space-8);   /* vertical gap between sections */
  --space-inline-sm: var(--space-2);  /* horizontal gap: icon + label */
}
[data-density="compact"] {
  --space-inset-md: var(--space-3);
  --space-stack-lg: var(--space-6);
}
```

## Grid and breakpoints

- Document the grid in Figma as layout grid styles, and in code as CSS grid utilities or layout components.
- Offer **layout components** (Stack, Inline, Grid, Container) so developers don't rebuild spacing logic everywhere.
- Define **max content widths** for reading (about 720px) and for app pages (1280–1440px).

| Breakpoint token | Min width | Columns | Margin | Gutter |
| --- | --- | --- | --- | --- |
| bp-sm | 0 | 4 | 16px | 16px |
| bp-md | 640px | 8 | 24px | 16px |
| bp-lg | 1024px | 12 | 32px | 24px |
| bp-xl | 1280px | 12 | auto (max 1280px content) | 24px |

## Sizing

Consistent sizes for controls make interfaces line up. Typical sizing tokens:

- **Control heights**: sm 32px, md 40px, lg 48px — shared by buttons, inputs and selects so they align in a row.
- **Icon sizes**: 16, 20, 24, 32px.
- **Avatar sizes**: 24, 32, 40, 56, 96px.
- **Minimum touch target**: 44px (or 48px), even when the visible control is smaller.

## Radius, borders and elevation

| Token | Value | Use |
| --- | --- | --- |
| radius-none | 0 | Tables, full-bleed images |
| radius-sm | 4px | Badges, small controls |
| radius-md | 8px | Buttons, inputs, cards |
| radius-lg | 16px | Modals, sheets, large cards |
| radius-full | 9999px | Pills, avatars, toggles |
| border-width-default | 1px | Inputs, cards, dividers |
| border-width-strong | 2px | Focus rings, selected states |
| shadow-1 / 2 / 3 | Layered shadows | Raised, overlay, modal |

> Tip: Radius is part of brand personality: sharp corners feel precise and serious; large radii feel friendly and soft. Pick a direction and apply it consistently. A nested element should usually have a smaller radius than its container (outer radius = inner radius + padding).

## Z-index and layering

Without a system, z-index values escalate (999, 9999, 99999…). Define named layers instead:

```css
:root {
  --z-base: 0;
  --z-raised: 10;     /* sticky table headers */
  --z-dropdown: 100;
  --z-sticky: 200;    /* sticky page header */
  --z-overlay: 300;   /* backdrop */
  --z-modal: 400;
  --z-toast: 500;
  --z-tooltip: 600;
}
```

## Motion tokens

| Token | Value | Use |
| --- | --- | --- |
| duration-instant | 100ms | Hover, colour changes |
| duration-fast | 150ms | Toggles, small state changes |
| duration-base | 240ms | Dropdowns, expanding content |
| duration-slow | 400ms | Large transitions, sheets |
| ease-standard | cubic-bezier(0.4, 0, 0.2, 1) | Moving within the screen |
| ease-enter | cubic-bezier(0, 0, 0.2, 1) | Elements entering |
| ease-exit | cubic-bezier(0.4, 0, 1, 1) | Elements leaving |

## Logical properties and RTL

If your products support right-to-left languages like Arabic, build the foundations with **logical properties** from the start. Instead of left/right, use start/end, so layouts mirror automatically when `dir="rtl"`.

| Physical (avoid) | Logical (prefer) |
| --- | --- |
| margin-left | margin-inline-start |
| padding-right | padding-inline-end |
| left: 0 | inset-inline-start: 0 |
| text-align: left | text-align: start |
| border-left | border-inline-start |

> Common mistake: Designing tokens and components for left-to-right only and "adding RTL later". Retrofitting mirrored layouts across a whole system is far more work than using logical properties from day one.

## Key takeaways

- A 4/8-point spacing scale removes guesswork; semantic spacing tokens add intent and density options.
- Define grids, breakpoints, max widths and layout components.
- Standardise sizes, radius, borders, shadows, z-index layers and motion as tokens.
- Outer radius = inner radius + padding keeps nested corners harmonious.
- Use logical properties (start/end) so the whole system supports RTL.

## Exercise: Write the foundations file

1. Create spacing, radius, sizing, shadow, z-index and motion tokens as CSS variables.
2. Create matching Figma variables for spacing and radius.
3. Build a card, a button and an input using only these tokens.
4. Rewrite any left/right CSS with logical properties.
5. Switch the page to dir="rtl" and check that everything mirrors correctly.

## Quiz

1. Which set is a typical 8-point spacing scale?
   - 5, 10, 15, 20
   - **8, 16, 24, 32, 48** ✓
   - 7, 13, 19, 25
   - 1, 2, 3, 4
   - Why: An 8-point system uses multiples of 8, with 4 for small steps.

2. Why share control heights (32/40/48px) across buttons, inputs and selects?
   - To save memory
   - **So controls align when placed in the same row** ✓
   - Browsers require it
   - For dark mode
   - Why: Shared heights keep toolbars and forms tidy.

3. Why define named z-index layers?
   - To make CSS longer
   - **To stop values escalating to 99999 and keep layering predictable** ✓
   - For accessibility
   - For SEO
   - Why: Named layers make stacking order explicit and consistent.

4. A card has 16px radius and 8px padding. What radius should an image inside it have?
   - 24px
   - 16px
   - **8px** ✓
   - 0px
   - Why: Inner radius = outer radius − padding (16 − 8 = 8).

5. Why prefer `margin-inline-start` over `margin-left`?
   - It's faster
   - **It flips automatically for right-to-left languages** ✓
   - It only works in Chrome
   - It's shorter
   - Why: Logical properties follow the reading direction.
