Sub topic 1
Every element is a box
Prerequisites
This chapter assumes you can write basic HTML elements, write CSS rules with selectors and declarations, and use common length units such as px, em, rem, and %. No other background is needed.
The core idea
Every element in a page generates a rectangular box. The browser lays out these boxes, sizes them, and places them relative to each other. Text, images, buttons, and div elements all follow this rule, even when they look round or irregular. A rounded corner or a transparent background changes how the box is painted, not its rectangular shape.
The CSS box model describes how each box is built and how its size is calculated. Almost every layout problem you meet later, such as unexpected scrollbars, gaps that are too big, or elements that overflow their parent, comes back to this model.
The four layers
Each box has four layers, from the inside out:
- Content area. Holds the text, image, or child elements.
widthandheightsize this area by default. - Padding. Space between the content and the border. It shows the element's background.
- Border. A line drawn around the padding. It has its own thickness, style, and color.
- Margin. Transparent space outside the border. It separates the box from its neighbors.
The padding and border belong to the box itself. The margin does not. The margin only controls the space around the box.
A first look in code
The following rule sets a value for every layer.
.card {
width: 300px;
padding: 20px;
border: 5px solid #333;
margin: 10px;
}With the default box-sizing value of content-box, the width applies only to the content area. The browser adds the other layers to it:
| Layer | Left and right total |
|---|---|
| Content | 300px |
| Padding | 20px + 20px = 40px |
| Border | 5px + 5px = 10px |
| Border box total | 350px |
| Margin | 10px + 10px = 20px |
| Space occupied in the row | 370px |
So width: 300px produces a box that is 350px wide, and it takes up 370px of horizontal space once the margins are counted. The same addition applies vertically to height, padding, border, and margin.
Common errors
- Assuming
widthis the full visible width. Withcontent-box, padding and border are added on top. Awidth: 100%element with padding will be wider than its parent. - Confusing padding and margin. If you want the background to extend into the space, use padding. If you want empty space between two boxes, use margin.
- Expecting margin to be part of the clickable or painted box. Margin is not painted with the background and does not receive clicks for that element.
What comes next
You now have the vocabulary: content, padding, border, and margin. The next chapters take each layer in turn, starting with the content area and how width and height size it. Keep the table above in mind. It is the calculation you will use most often when debugging layout.
Exercise
A .panel has width: 200px; padding: 10px 15px; border: 2px solid; margin: 20px; and uses the default box-sizing. What is its border box width, and how much horizontal space does it occupy?
Show answer
The border box width is 200 + 30 (padding, 15px per side) + 4 (border, 2px per side) = 234px. With 40px of horizontal margin added (20px per side), it occupies 274px of horizontal space.