Everything Is a Box
Whether it's a paragraph, an image, or a <div>, the browser treats every element as a rectangular box. That box is made of four layers, nested inside each other, from the inside out: content, padding, border, and margin.
- ✓Content is where text and child elements actually live.
- ✓Padding is space inside the border, between the content and the edge of the box.
- ✓Margin is space outside the border, separating the box from its neighbors.
Content, Padding, Border, Margin
Picture the box as rings around a core, largest to smallest going outward: margin, then border, then padding, then content at the center.
The actual text, image, or nested elements. Sized by width and height.
Space between the content and the border. Shares the box's background color.
A visible (or invisible) line that wraps the padding and content together.
Space outside the border. Transparent — it separates this box from its neighbors.
Padding & Margin Shorthand
Both padding and margin accept one, two, three, or four values — each pattern controls a different combination of sides.
/* One value: all four sides */ .box { padding: 20px; } /* Two values: vertical | horizontal */ .box { padding: 10px 20px; } /* Four values: top | right | bottom | left (clockwise) */ .box { margin: 10px 15px 20px 15px; } /* Individual sides */ .box { margin-top: 24px; margin-bottom: 0; }
margin: 0 auto; is the classic trick for horizontally centering a block element that has a fixed width — the browser splits the remaining space evenly on both sides.
The Property That Changes Everything
By default, width and height only set the content area — padding and border get added on top, making the box bigger than the number you wrote. box-sizing: border-box changes that: width and height now include padding and border, which makes sizing far more predictable.
/* content-box (default): width is content only */ .box-a { box-sizing: content-box; width: 200px; padding: 20px; /* renders at 240px wide (200 + 20 + 20) */ } /* border-box: width includes padding + border */ .box-b { box-sizing: border-box; width: 200px; padding: 20px; /* renders at exactly 200px wide */ } /* Common reset applied to every element */ * { box-sizing: border-box; }
Most developers add the universal * { box-sizing: border-box; } reset at the top of every stylesheet — it removes a whole category of "why is this wider than I set it" bugs.
A Common Surprise
When two vertical margins meet — like the bottom margin of one paragraph and the top margin of the next — they don't add together. The browser collapses them into a single margin equal to the larger of the two.
Breaking Down What You Just Learned
Four layersContent, padding, border, and margin nest inside each other from the outside in.
Shorthand patternsOne, two, or four values in padding/margin control different combinations of sides.
box-sizing: border-boxMakes width and height include padding and border for predictable sizing.
Auto marginsmargin: 0 auto centers a fixed-width block element horizontally.
Margin collapseAdjacent vertical margins merge into the larger single value, not their sum.
Try It Yourself
Build a 200px-wide box with 20px padding and a 5px border, once with box-sizing: content-box and once with border-box. Measure the rendered width of each in your browser's dev tools and compare.
Build a Centered, Spaced-Out Layout
Applying the box model to a real page section
Create day18.html linked to a stylesheet and build a small "profile card" layout.
- Add * { box-sizing: border-box; } at the top of your stylesheet.
- Build a card div with a fixed width, padding, and a solid border.
- Center the card on the page using margin: 0 auto.
- Add three paragraphs inside the card and give them consistent margin-bottom spacing.
- Open dev tools, inspect the card, and confirm the rendered width matches what you set.
Recap
Content, padding, border, and margin make up every element's box.
border-box includes padding and border inside the declared width.
Padding and margin accept one, two, or four values for different side combos.
Adjacent vertical margins merge into the larger value, not their sum.
Key Takeaways
- Every HTML element renders as a box made of content, padding, border, and margin.
- box-sizing: border-box makes an element's declared width include its padding and border.
- padding and margin shorthand accept one, two, or four values to target different sides.
- margin: 0 auto is the standard way to horizontally center a fixed-width block.
- Adjacent vertical margins collapse into the larger single value rather than adding together.