By WebNest Studio Engineering Team
CSS Tutorial
The CSS Box Model
Every element rendered on a web page is treated by the browser as a rectangular box, and the CSS box model describes exactly how that box's size is calculated from its content outward. Mastering the box model is essential — it explains why elements sometimes overflow their containers, why margins seem to "collapse," and why two elements with the same declared width can render at different actual sizes.
The box model has four layers, from the inside out: content, padding, border, and margin. Each layer adds space around the one before it, and the box-sizing property controls whether padding and border are included in or added to a declared width.
Why it matters
Every element on a page is a rectangular box made of content, padding, border and margin. Layout bugs such as an element that is wider than you set, or two columns that will not fit side by side, come from not accounting for those layers. The box-sizing rule in this lesson is at the top of almost every professional stylesheet.
The Four Layers
Understanding each layer individually makes the full box easy to reason about.
- Content — the actual text, image, or other content, sized by width and height.
- Padding — transparent space between the content and the border; it is part of the element's background.
- Border — a visible (or invisible) line that wraps the padding and content, set with border-width, border-style, and border-color.
- Margin — transparent space outside the border, separating the element from its neighbors; margins can collapse vertically between adjacent block elements.
content-box vs. border-box
By default, box-sizing: content-box means width and height apply only to the content area — padding and border are added on top, making the element's total rendered size larger than the declared width. box-sizing: border-box changes this so that width and height include padding and border, meaning the content area shrinks to fit while the total box stays exactly the declared size. Most modern stylesheets set * { box-sizing: border-box; } globally because it makes sizing far more predictable, especially in layouts that mix padding, borders, and percentage widths.
Margin Collapsing
Vertical margins between two adjacent block-level elements collapse into a single margin equal to the larger of the two, rather than adding together. Horizontal margins never collapse. This behavior surprises many beginners: two stacked paragraphs each with margin: 20px 0 end up with only 20px between them, not 40px.
Examples
content-box vs. border-box changing the rendered size of identical width declarations
.card-content-box {
width: 300px;
padding: 20px;
border: 5px solid #333;
box-sizing: content-box; /* total rendered width = 300 + 40 + 10 = 350px */
}
.card-border-box {
width: 300px;
padding: 20px;
border: 5px solid #333;
box-sizing: border-box; /* total rendered width stays exactly 300px */
}
The content-box card renders 350px wide overall, while the border-box card renders exactly 300px wide, with its content area shrinking to accommodate the padding and border.
Margin collapsing between stacked paragraphs
<!--
<p>First paragraph.</p>
<p>Second paragraph.</p>
-->
p {
margin: 20px 0;
}
The visible gap between the two paragraphs is only 20px, not 40px, because the adjacent vertical margins collapse into the larger of the two values.
Common Mistakes
- Setting width: 300px on an element with padding and a border under the default content-box model, then being confused when it renders wider than 300px.
- Adding vertical margin to both a parent and its first child and expecting the space to stack, without accounting for margin collapsing.
- Forgetting to reset box-sizing globally, causing a mix of content-box and border-box elements to size inconsistently across a layout.
- Confusing padding (inside the border, part of the background) with margin (outside the border, always transparent).
Exercise
Give the button a width of 200px, padding of 20px and a 5px solid border, and make sure its total rendered width is still exactly 200px. Then put 30px of space above it.
Expected result: a button that measures exactly 200px wide in DevTools, sitting 30px below the paragraph.
/* The page contains: <main> with an <h1>, a <p> and a <button> */
button {
/* TODO: width, padding and border */
/* TODO: make the width include the padding and border */
/* TODO: space above the button */
}
Show hints
- With the default
content-box, the width would be 200 + 40 + 10 = 250px. box-sizing: border-boxmakeswidthinclude the padding and border.
Show solution
/* The page contains: <main> with an <h1>, a <p> and a <button> */
button {
width: 200px;
padding: 20px;
border: 5px solid black;
box-sizing: border-box;
margin-top: 30px;
}
Quick quiz
1. In what order do the layers of the box model go, from the inside out?
- A. Margin, border, padding, content
- B. Content, border, padding, margin
- C. Padding, content, margin, border
- D. Content, padding, border, margin
Show answer
D. Padding is inside the border and margin is outside it.
2. An element has width: 100px; padding: 10px; border: 2px solid; and the default box-sizing. How wide is it on screen?
- A. 124px
- B. 100px
- C. 112px
- D. 120px
Show answer
A. With content-box, padding and border on both sides are added: 100 + 20 + 4.
3. Two paragraphs are stacked. The first has margin-bottom: 30px and the second margin-top: 20px. What is the gap between them?
- A. 50px
- B. 30px
- C. 20px
- D. 10px
Show answer
B. Adjoining vertical margins collapse into one, equal to the larger of the two.
Interview questions
What is the difference between content-box and border-box?
Show answer
With box-sizing: content-box, the default, width and height set the size of the content area only, and padding and border are added on top, so the element ends up larger than the stated width. With border-box, the stated width includes padding and border, and the content area shrinks to fit. Most projects apply border-box to every element because it makes sizes predictable.
What is margin collapsing?
Show answer
When the vertical margins of two block elements touch, they combine into a single margin whose size is the larger of the two, not their sum. It happens between adjacent siblings and between a parent and its first or last child when no padding, border or content separates them. It does not happen to horizontal margins, or to the items of a flex or grid container.
Key Points to Remember
- Every element is a box made of content, padding, border, and margin, from the inside out.
- box-sizing: content-box adds padding and border on top of the declared width; border-box includes them within it.
- Adjacent vertical margins collapse to the larger single value rather than summing; horizontal margins never collapse.
- Setting box-sizing: border-box globally is a common, highly recommended reset for predictable sizing.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering