Sub topic 1
Every element is a box
This chapter introduces the box model as a whole. Later chapters cover each layer, box-sizing, margin collapsing, and the other topics in detail. Here you build the working model that those chapters rely on, and you learn the most common mistakes people make with it.
Prerequisites
This chapter assumes you can do the following:
- Read basic HTML: elements, nesting, and attributes.
- Write a CSS rule with a selector and declarations, such as
p { color: red; }. - Recognise common length units:
px,em,rem, and%.
You do not need to know Flexbox, Grid, or positioning. Those layout systems still use the box model internally.
The core idea
Every element in a page produces at least one rectangular box. The browser's layout engine does not see headings, paragraphs, or images as shapes of their own. It sees boxes, and it decides each box's size and position.
A box has four layers. From the inside out:
- Content area. The space where text, images, or child elements appear. The
widthandheightproperties size this area by default. - Padding. Space between the content and the border. It takes on the element's own background.
- Border. A line that wraps the padding and content.
- Margin. Transparent space outside the border. It separates the box from its neighbours.
Each layer has a matching edge, named after the layer that ends there:
| Edge | Where it sits |
|---|---|
| Content edge | Around the content area |
| Padding edge | Around the content plus padding |
| Border edge | Around the content, padding, and border |
| Margin edge | Around everything, including margin |
The border edge is also called the border box. When people say "the element is 350px wide", they usually mean the border box.
A box you can measure
Consider this rule:
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}With the default box-sizing: content-box, the width property sets only the content area. The other layers are added on each side:
- Content: 300px
- Padding: 20px on the left and 20px on the right, so 40px
- Border: 5px on each side, so 10px
- Border box width: 300 + 40 + 10 = 350px
- Margin: 10px on each side, so 20px
- Horizontal space occupied, including margins: 350 + 20 = 370px
Height works the same way. If no height is set, the content area grows to fit its content, and the padding, border, and margin are added around it.
Shorthand for the four sides
margin, padding, and the border-width part of border can be set per side. The shorthand values run clockwise from the top.
/* top right bottom left */
.box {
margin: 10px 20px 30px 40px;
padding: 8px 16px;
}The number of values changes how they are applied:
| Values | Meaning |
|---|---|
| One value | All four sides |
| Two values | Vertical (top and bottom), then horizontal (left and right) |
| Three values | Top, then horizontal, then bottom |
| Four values | Top, right, bottom, left |
In the example above, padding: 8px 16px gives 8px above and below the content and 16px on the left and right.
Why this model matters
Most layout surprises come from one question: which of these layers does a given number include? Typical symptoms include:
- A column that is meant to be 50% wide overflows its parent because padding was added on top of the width.
- Two boxes stack with a smaller gap than the sum of their margins.
- A child element appears to push its parent away from the page, instead of sitting inside it.
Each symptom traces back to a specific rule of the box model. The rest of the tutorial covers these rules, and this chapter gives you the vocabulary for them.
Block and inline boxes
Not all boxes behave the same way. The display property decides the outer behaviour of a box. The two types you meet first are:
- Block boxes. They start on a new line and, by default, stretch to fill the width of their container.
width,height,margin, andpaddingall work on all four sides. Examples arediv,p,h1, andul. - Inline boxes. They flow within a line of text.
widthandheighthave no effect. Horizontal margin and padding work as expected. Vertical padding and border are drawn, but they do not push neighbouring lines away, and vertical margin has no effect. Examples arespan,a, andstrong.
A third type, inline-block, flows like an inline box but accepts width and height like a block box. A later chapter compares all three.
<style>
.a, .b {
width: 200px;
height: 40px;
padding: 10px;
background: lightblue;
}
</style>
<div class="a">Block: the width and height apply.</div>
<span class="b">Inline: the width and height are ignored.</span>In this example, the div is 220px wide and 60px tall (content-box sizing). The span ignores the 200px and 40px. Its padding is painted, but it takes only as much space as its text needs.
Seeing the boxes
The fastest way to learn the box model is to look at real boxes in the browser. In Chrome, Firefox, or Edge, right-click an element and choose Inspect. In the Elements (or Inspector) panel, open the Computed or Layout tab. You will see a diagram of nested rectangles that shows the content size and the padding, border, and margin on each side.
Hovering over an element in the tree also highlights its boxes on the page. Content is usually tinted blue, padding green, and margin orange. A later chapter covers this tool in more detail.
Common errors
These mistakes appear again and again. Each one is explained more fully in a later chapter.
Forgetting that padding and border add to the width
With content-box, width: 100% plus any padding or border makes the box wider than its parent. The fix is to set box-sizing: border-box, which makes width cover the content, padding, and border together.
*,
*::before,
*::after {
box-sizing: border-box;
}
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
}Now the border box stays 300px wide, and the content area shrinks to 250px (300 − 40 − 10).
Expecting margins to add up
Vertical margins between block siblings can collapse into one margin. Collapsing only occurs between vertical margins of block boxes, never between horizontal margins.
<style>
h2 { margin-bottom: 24px; }
p { margin-top: 16px; }
</style>
<h2>Heading</h2>
<p>Paragraph</p>The gap here is 24px, the larger of the two margins, and not 40px.
Using margin when padding is needed
Margin is outside the border, so it does not show the element's background. If you want a coloured area to extend around the text, use padding. If you want empty space between two boxes, use margin.
Setting height on inline elements
A span ignores height. Change its display value, or use a block-level or inline-block element.
Direction-aware spacing
Physical properties such as margin-left always refer to the left side of the screen. In a right-to-left language such as Arabic, or in vertical text, "left" may not be the side you mean. Logical properties describe the same sides relative to the writing direction:
.article {
max-inline-size: 40rem;
margin-inline: auto;
padding-block: 1rem;
}Here, max-inline-size is the maximum width in horizontal writing modes. margin-inline: auto centres the box along the inline axis. padding-block sets the space at the start and end of the block axis, which is the top and bottom in horizontal text. A later chapter covers these properties.
What is not part of the box
Some visual effects are drawn around a box without changing its layout size. Two examples are outline and box-shadow. They can overlap neighbouring elements and do not shift anything. They are useful for focus rings and elevation effects, and they do not change the numbers in the calculations above.
Summary of the model
- Every element generates at least one box with content, padding, border, and margin layers.
- Under
content-box,widthandheightsize the content area only. - The border box is content plus padding plus border. Margin lies outside it.
- Block boxes accept all dimensions. Inline boxes ignore
width,height, and vertical margin. - Setting
box-sizing: border-boxmakes sizing easier to predict. - Browser developer tools show the boxes of any element.
Exercise
An element has width: 200px; padding: 10px 15px; border: 4px solid; margin: 12px; and uses the default box-sizing. What is the width of its border box, and how much horizontal space does it occupy including margins?
Show answer
The default is content-box, so the border box width is 200 + (15 × 2 padding) + (4 × 2 border) = 200 + 30 + 8 = 238px. Adding the 12px margin on each side (24px) gives 262px of horizontal space.
Exercise
A span has width: 150px; height: 50px; margin-top: 30px;. Which of these declarations take effect, and how could you make all of them work?
Show answer
None of them take effect, because a span is an inline box by default and ignores width, height, and vertical margin. Setting display: inline-block (or display: block) on the span makes all three apply.
Exercise
The same .card rule uses box-sizing: border-box with width: 300px; padding: 20px; border: 5px solid #333;. How wide is the content area?
Show answer
The content area is 250px wide. With border-box, the 300px width includes padding and border, so the content gets 300 − 40 − 10 = 250px.