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:
| Layer | What it is |
|---|---|
content | The text or image itself — width × height. |
padding | Space inside the box, between content and border. |
border | A line around the padding. |
margin | Space outside the box, pushing other boxes away. |
.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:
*, *::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
| Unit | Relative to | Use for |
|---|---|---|
px | Nothing (absolute) | Borders, fine details |
rem | The root font size (usually 16px) | Font sizes, spacing — scales with user settings |
% | The parent's size | Fluid widths |
vw / vh | Viewport width / height | Full-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
.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
- 01Every element is a box: content → padding → border → margin.
- 02Set box-sizing: border-box globally so width includes padding and border.
- 03Vertical margins collapse to the larger of the two — not their sum.
- 04Block elements stack and take full width; inline elements flow within a line.
- 05Use rem for font sizes (respects user zoom); px for fine details; % and vw for fluid sizing.
