Skip to main content

Box Model & Spacing Cheatsheet

The Box Model

┌──────────────── margin ────────────────┐
│ ┌─────────────border─────────────┐ │
│ │ ┌────────padding────────┐ │ │
│ │ │ │ │ │
│ │ │ CONTENT │ │ │
│ │ │ │ │ │
│ │ └───────────────────────┘ │ │
│ └────────────────────────────────┘ │
└────────────────────────────────────────┘

content-box (default, bad): width = content only
border-box (use this): width = content + padding + border

Always add to your reset:

*, *::before, *::after { box-sizing: border-box; }

Margin & Padding Shorthand

Clockwise: Top → Right → Bottom → Left (TRouBLe)

1 value: all sides equal
2 values: top/bottom | left/right
3 values: top | left/right | bottom
4 values: top | right | bottom | left

Examples:
margin: 1rem; → all 4 sides
margin: 1rem 2rem; → vertical | horizontal
margin: 1rem 2rem 1.5rem; → top | sides | bottom
margin: 1rem 2rem 1.5rem 0; → top | right | bottom | left

Logical Properties (Modern — Preferred)

PhysicalLogicalUse When
margin-top + bottommargin-blockAlways prefer this
margin-left + rightmargin-inlineAlways prefer this
padding-top + bottompadding-blockAlways prefer this
padding-left + rightpadding-inlineAlways prefer this
widthinline-sizeFor RTL-aware projects
heightblock-sizeFor RTL-aware projects
top/right/bottom/left: 0inset: 0Always prefer this

Sizing Keywords

ValueBehavior
autoBrowser calculates (fills available or fits content)
100%100% of parent's size
fit-contentShrinks to content, max = available space
min-contentShrinks to smallest non-overflowing size
max-contentExpands to fit all content (no wrapping)
100vw100% viewport width
100dvhDynamic viewport height (accounts for mobile browser bar)
clamp(min, ideal, max)Fluid between floor and ceiling

Width / Height Patterns

/* Responsive container */
.o-container {
width: min(100% - 2rem, 1200px); /* Max 1200px, full-width with padding */
margin-inline: auto;
}

/* Fluid width with minimum */
width: max(280px, 50%);

/* Lock aspect ratio */
aspect-ratio: 16 / 9; /* Width × (9/16) = height */
aspect-ratio: 1; /* Square */
aspect-ratio: 3 / 4; /* Portrait */

/* Full viewport section */
min-height: 100dvh; /* At minimum, full device viewport */

/* Content-driven grid column */
grid-template-columns: min-content 1fr; /* Label: tight | Content: flexible */

Border Shorthand

border: <width> <style> <color>;
border: 1px solid var(--color-border);
border: none;
border: 0; /* Same as none */

/* Individual sides */
border-top: 4px solid var(--color-primary); /* Accent top */
border-inline-start: 4px solid var(--color-primary); /* RTL-safe left border */

/* Border radius */
border-radius: 8px; /* All corners */
border-radius: 1rem 1rem 0 0; /* Top corners only */
border-radius: 50%; /* Circle */
border-radius: 9999px; /* Pill */

/* Outline (no layout effect — for focus rings) */
outline: 2px solid var(--color-primary);
outline-offset: 3px;

Overflow

ValueBehavior
visibleContent shows outside (default)
hiddenClips at edge — also creates BFC (stops margin collapsing)
autoScrollbar only when needed ✅
scrollAlways shows scrollbar
clipClips like hidden but NO BFC — no scroll context
/* Scroll specific axis */
overflow-x: auto; /* Horizontal scroll if needed */
overflow-y: hidden; /* Clip vertical overflow */

/* Text truncation */
.truncate {
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
max-width: 100%;
}

/* Multi-line clamp */
.c-text-clamp {
display: -webkit-box;
-webkit-line-clamp: 3;
-webkit-box-orient: vertical;
overflow: hidden;
}

Margin Collapsing Quick Rules

Collapses (vertical margins only):
✅ Adjacent block siblings → larger wins
✅ Parent + first/last child → larger wins (if no separator)
✅ Empty element's own margins → collapse to one

Doesn't collapse:
❌ Inside flex or grid containers
❌ When parent has padding or border
❌ overflow != visible
❌ Horizontal (left/right) margins — NEVER
❌ display: flow-root elements

Fix: display: flow-root on parent (cleanest — no side effects)

The display Property

ValueCreates
blockBlock-level box, full width, new line
inlineInline box, wraps in text
inline-blockInline + block sizing
flexFlex container
inline-flexInline flex container
gridGrid container
inline-gridInline grid container
flow-rootBlock + new BFC (stops margin collapsing)
noneRemoves from layout and accessibility tree
contentsElement itself is invisible, children participate in parent layout
table / table-row / table-cellTable-like behavior