Colors & the Box Model Lesson 9 of 33
The CSS Box Model, Width, Height and box-sizing
Every element is a box. Learn the four layers of the CSS box model, how width and height are calculated, and why box-sizing border-box makes layouts easier.
Here's the single most important idea in CSS layout: every element on a web page is a rectangular box. Headings, paragraphs, images, buttons, even a single word inside a <span> — all boxes. Once you understand how a box is built, spacing and layout stop being confusing.
The four layers
From the inside out, every box has:
- Content — the text, image or other elements inside
- Padding — transparent space around the content (shows the background)
- Border — the line around the padding
- Margin — empty space outside the border, separating it from other boxes
<style>
.margin { background: #fdf3dc; padding: 24px; font: 13px monospace; }
.border { background: #2b2b2b; padding: 10px; color: white; }
.padding { background: #7fd1b9; padding: 24px; color: #1d2321; }
.content { background: #fff; padding: 20px; text-align: center; border: 1px dashed #888; }
</style>
<div class="margin">margin
<div class="border">border
<div class="padding">padding
<div class="content">content</div>
</div>
</div>
</div>
You can see these layers in any website. On a computer, right-click an element, choose Inspect, and look for the box diagram in the "Computed" tab of the developer tools. Hover over it and the browser highlights each layer on the page.
Width and height
width and height set the size of a box:
<style>
.a { width: 200px; height: 80px; background: #e3f1ec; }
.b { width: 50%; background: #fbe9e6; }
</style>
<div class="a">200px × 80px</div>
<div class="b">50% of the parent's width</div>
max-width: the responsive choice
A fixed width: 800px is too wide for a phone, and the page will scroll sideways. max-width: 800px means "up to 800px, but shrink if the screen is smaller." This is the safe default for content containers.
<style>
.fixed { width: 700px; background: #fbe9e6; padding: 8px; margin-bottom: 8px; }
.flexible { max-width: 700px; background: #e3f1ec; padding: 8px; }
</style>
<div class="fixed">width: 700px — overflows on small screens</div>
<div class="flexible">max-width: 700px — shrinks to fit small screens</div>
There are also min-width, min-height and max-height. min-height is useful for sections that should be at least a certain height but can grow if the content is long.
The surprise: padding and border add to the width
By default, width sets only the content width. Padding and border are added on top. So this box is not 300px wide:
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
}
/* actual width = 300 + 20 + 20 + 5 + 5 = 350px */
That's confusing. You say 300 and get 350, which breaks layouts. Fortunately, there's a fix.
box-sizing: border-box
With box-sizing: border-box, the width includes the padding and border. You say 300px, you get 300px. The content area shrinks to make room.
<style>
.box { width: 300px; padding: 20px; border: 5px solid #0f5c4d; margin-bottom: 10px; font-family: Arial, sans-serif; }
.cb { box-sizing: content-box; background: #fbe9e6; }
.bb { box-sizing: border-box; background: #e3f1ec; }
.ruler { width: 300px; height: 8px; background: #f2b33d; }
</style>
<div class="ruler"></div>
<div class="box cb">content-box: 350px wide in total</div>
<div class="box bb">border-box: exactly 300px</div>
The yellow ruler is exactly 300px. Only the border-box version matches it. Because this is so much easier, almost every modern website starts its CSS with this rule:
*, *::before, *::after {
box-sizing: border-box;
}
It applies border-box to every element. Copy it to the top of every project you make. The ::before and ::after parts are covered in the Pseudo-elements lesson.
Inline elements are different
The box model works fully on block elements like div, p and h1. Inline elements like span, a and strong ignore width and height, and their top and bottom margins don't push other lines away. You'll learn how to change this with the display property in the Display lesson.
Practice: three equal cards
<style>
* { box-sizing: border-box; }
.card {
float: left;
width: 33.33%;
padding: 14px;
border: 3px solid #fff;
background: #0f5c4d;
color: white;
text-align: center;
font-family: Arial, sans-serif;
}
</style>
<div class="card">Math</div>
<div class="card">Science</div>
<div class="card">English</div>
Each card is 33.33% wide including its padding and border, so three fit on one line. Delete the * { box-sizing: border-box; } line and watch the third card fall to the next line, because each card becomes wider than a third. (Here we used float just to place the cards side by side. You'll learn better ways in the Flexbox lesson.)
Quick Quiz
Check what you learned. Walang grade ito, practice lang. 🙂
By Sir Reyel · Last updated 2026-12-04