Skip to main content
Albero Academy

The Box Model & Spacing

Every element is a box: content, padding, border and margin. Master them plus box-sizing, colours, units and typography.

Chapter 2CSS15 min readBeginner friendly
  • #CSS
  • #Box Model
  • #Spacing

The single most important mental model in CSS: every element is a rectangular box. Layout is just arranging boxes and controlling the space inside and around them. Once the box model clicks, mysterious spacing bugs disappear.

The box model

Each box has four layers, from the inside out:

LayerWhat it is
contentThe text or image itself — width × height.
paddingSpace inside the box, between content and border.
borderA line around the padding.
marginSpace outside the box, pushing other boxes away.
spacing shorthands
.card {
padding: 24px; /* all four sides */
padding: 16px 24px; /* top/bottom left/right */
padding: 8px 16px 24px 32px; /* top right bottom left (clockwise) */
margin: 0 auto; /* 0 top/bottom, auto left/right → centres */
border: 1px solid #e5e7eb; /* width style colour */
}

box-sizing: the fix everyone sets

By default, width sets the content width — padding and border are added on top, so a "300px" box is actually wider. Almost every project fixes this globally so width means the whole box:

the one rule to start every stylesheet
*, *::before, *::after {
box-sizing: border-box;
}
/* Now width:300px + padding:20px = 300px total, as you'd expect. */

Block vs inline

The display property controls how a box flows. Two defaults you meet immediately:

  • Block (div, p, h1) — starts on a new line and fills the available width. Respects width/height.
  • Inline (a, span, strong) — flows within a line and ignores width/height & vertical margins.

display: inline-block is the middle ground (flows inline but respects size), and display: none removes the box entirely. The real layout engines — flex and grid — get their own chapter next.

Units: px, rem, %, vw

UnitRelative toUse for
pxNothing (absolute)Borders, fine details
remThe root font size (usually 16px)Font sizes, spacing — scales with user settings
%The parent's sizeFluid widths
vw / vhViewport width / heightFull-screen sections, hero heights

Prefer rem for font sizes so text respects a user's browser zoom and accessibility settings — hard-coding everything in px ignores them.

Colour & typography

colour & text
.hero {
color: #0f172a; /* hex */
background: rgb(37 99 235 / 0.1); /* rgb with alpha */
font-family: Inter, system-ui, sans-serif;
font-size: 1.125rem; /* 18px */
font-weight: 600;
line-height: 1.6; /* unitless = 1.6× the font size */
letter-spacing: -0.01em;
}

Set font-family with a stack — your preferred font, then fallbacks, ending in a generic family like sans-serif so text always renders while a web font loads.

Practice

Build a "programme card": a bordered box with padding, a heading, a paragraph, and a button. Add box-sizing: border-box globally, centre the card with margin: 0 auto and a max-width, and size all text in rem.

Key takeaways

  1. 01Every element is a box: content → padding → border → margin.
  2. 02Set box-sizing: border-box globally so width includes padding and border.
  3. 03Vertical margins collapse to the larger of the two — not their sum.
  4. 04Block elements stack and take full width; inline elements flow within a line.
  5. 05Use rem for font sizes (respects user zoom); px for fine details; % and vw for fluid sizing.