- Name the four layers of a box:
content,padding,border,margin - Tell block and inline elements apart (
display) - Use the
marginandpaddingshorthands and center a block - Calculate an element's full size for
content-boxandborder-box
Leyla gave a card width: 300px, then added some padding and a border — and suddenly the card no longer fit in a 300-pixel column. Why? Because to the browser, every element on a page is a box, and its size is more than just width. Once you understand the box model, half of all CSS surprises disappear.
Every element is a box
A box has four layers, from the inside out. Think of a parcel: the item inside is the content, the foam around it is the padding, the cardboard box is the border, and the space between it and the other parcels is the margin.
- content — the text, image or child elements. Its size is set by
widthandheight. - padding — the space between the content and the border. The element's background extends into it.
- border — the frame of the box:
border: 2px solid tealgives the thickness, line style and color. - margin — the transparent space between the box and its neighbors.
<style>
body { font-family: system-ui, sans-serif; padding: 12px; }
.margin-zone { display: inline-block; background: #fde68a; }
.box {
width: 200px;
padding: 20px;
border: 6px solid #6366f1;
margin: 24px;
background: #e0e7ff;
}
</style>
<div class="margin-zone">
<div class="box">Content: 200px wide</div>
</div>
<p>Yellow = margin, blue frame = border,<br>light blue around the text = padding.</p>padding, border and margin one by one and watch which zone grows. The visible width of the box is 200 + 2 × 20 + 2 × 6 = 252px.Block and inline elements
The display property decides how a box behaves. Block elements (<div>, <p>, <h1>, <ul>) start on a new line and take the full available width. Inline elements (<span>, <a>, <strong>) stay inside the text flow; width and height don't work on them, and vertical margin doesn't push the lines around. inline-block combines the best of both: it stays in the text flow but accepts a size.
<style>
body { font-family: system-ui, sans-serif; padding: 12px; line-height: 1.8; }
.demo { background: #fde68a; border: 1px solid #f59e0b; }
span.demo { width: 300px; }
.ib { display: inline-block; width: 110px; text-align: center; background: #bbf7d0; }
</style>
<div class="demo">A div is a block: it takes the whole line.</div>
<p>A <span class="demo">span is inline</span>: width 300px is ignored.
An <span class="ib">inline-block</span> accepts a width.</p>display: block; to the span.demo rule — the <span> will jump onto its own line and become 300px wide.Padding and margin shorthands
You can set each side separately: margin-top, margin-right, margin-bottom, margin-left. But usually the shorthand is used — one property with one, two, three or four values:
| Written as | Meaning |
|---|---|
padding: 10px | all four sides 10px |
padding: 10px 20px | top and bottom 10px, right and left 20px |
padding: 10px 20px 30px | top 10px, right and left 20px, bottom 30px |
padding: 10px 20px 30px 40px | top, right, bottom, left |
To center a block element that has a width, set its left and right margins to auto: margin: 0 auto. The browser splits the remaining free space equally between the two sides.
<style>
body { font-family: system-ui, sans-serif; background: #f1f5f9; padding: 12px; }
.card {
max-width: 260px;
margin: 0 auto;
padding: 12px 20px 20px;
background: white;
border: 1px solid #cbd5e1;
border-radius: 12px;
}
.btn { display: inline-block; padding: 8px 16px; border-radius: 999px;
background: #14b8a6; color: white; }
</style>
<div class="card">
<h3>Centered card</h3>
<p>margin: 0 auto puts me in the middle.</p>
<span class="btn">Read more</span>
</div>border-radius: 999px makes the corners fully round, giving a pill-shaped button. Remove margin: 0 auto and watch the card slide to the left.`box-sizing`: how size is counted
By default box-sizing: content-box applies: width is only the width of the content, and padding and border are added on top. That was Leyla's problem. With box-sizing: border-box, width is the width of the whole visible box including the border, and padding and border take their space from the inside. That makes the math much easier.
<style>
body { font-family: system-ui, sans-serif; padding: 12px; }
.ruler { width: 200px; height: 6px; background: #1e293b; margin-bottom: 8px; }
.demo { width: 200px; padding: 20px; border: 5px solid #0ea5e9;
margin-bottom: 12px; background: #e0f2fe; }
.bb { box-sizing: border-box; border-color: #f97316; background: #ffedd5; }
</style>
<div class="ruler"></div>
<div class="demo">content-box: 200 + 40 + 10 = 250px</div>
<div class="demo bb">border-box: exactly 200px</div>An element has width: 300px; padding: 15px; border: 2px solid; margin: 10px;. How much horizontal space does the box take in the default content-box mode? And how wide is the content area in border-box mode?
Show solutionHide solution
content-box: visible box = 300 + 2 · 15 + 2 · 2 = 334px.Space taken including the margin: 334 + 2 · 10 = 354px.
border-box: the visible box is exactly 300px, and the content is 300 − 2 · 15 − 2 · 2 = 266px.Key points
- Every element is a box: from the inside out,
content→padding→border→margin. - Block elements take the full width; inline elements ignore
width/height;inline-blocksits in between. - Shorthand values go clockwise: top, right, bottom, left;
margin: 0 autocenters a block. - Vertical margins collapse: the gap equals the larger of the two.
- In
content-box, padding and border are added towidth; inborder-box,widthis the whole visible box.
Check yourself
10 questions. Every correct answer earns XP.