Welcome to Lesson 11 ยท one of the most important lessons in this entire CSS course.
The CSS Box Model is the single most foundational concept in all of CSS layout. Every single element you place on a webpage ยท every heading, paragraph, image, button, card, and container ยท is treated by the browser as a rectangular box. Understanding exactly how that box is structured, how it is measured, and how its layers interact is what separates beginners who guess at spacing from developers who calculate layouts precisely and confidently.
Once you truly understand the Box Model, you will be able to:
- Predict exactly how wide and tall any element on your page will be
- Stop being surprised when adding padding makes your layout break
- Understand why CSS layouts sometimes overflow or collapse unexpectedly
- Control space inside and outside every element with complete confidence
This lesson also introduces box-sizing: border-box ยท a single property that changes how sizing is calculated and that every professional web developer uses on every project they build.
By the end of this lesson you will be able to:
- Name and describe all four layers of the CSS Box Model
- Calculate the total rendered width and height of any element
- Understand the difference between
content-box(default) andborder-boxsizing - Apply
box-sizing: border-boxto an entire project using the universal selector - Use browser DevTools to inspect the Box Model of any live element
- Complete all Box Model code challenges from W3Schools
- Build a complete product card layout using precise Box Model calculations
Before we go deeper, let's make sure you are solid on these ideas.
What is an HTML Element?
An HTML element is a piece of content marked up with a tag, like <p>, <div>, <h1>, or <button>. Every element you write in HTML gets rendered in the browser as a visible (or invisible) block of content on the page.
What is CSS?
CSS (Cascading Style Sheets) is the language used to style HTML elements ยท controlling their colour, size, font, position, and spacing.
What is a CSS Property?
A CSS property is an instruction you give to the browser about how to display an element. For example:
p {
color: navy;
font-size: 16px;
width: 300px;
}colorโ text colour propertyfont-sizeโ text size propertywidthโ how wide the element should be
In this lesson, the key properties we work with are width, height, padding, border, and margin ยท and how they all interact inside the Box Model.
1.1 The Core Idea: Everything is a Box
Open any webpage right now ยท a news article, a social media post, an e-commerce product listing. Every single visible piece of content you see on that page, no matter how it looks visually, is a rectangle inside the browser.
The CSS Box Model is the system that describes the structure of that rectangle. It defines four distinct layers that surround every element, from the innermost content to the outermost edge.
๐ก Analogy ยท The Gift Box: Imagine a gift inside a box. The gift itself is the content. The tissue paper cushioning the gift inside the box is the padding. The cardboard walls of the box form the border. The gap between this box and the other boxes sitting next to it on the shelf is the margin.
Each of these layers has a CSS property that controls it. Together, they define exactly how much space an element takes up on the page.
1.2 The Four Layers of the Box Model
Here is the complete diagram of the CSS Box Model:
+===============================================+
โ MARGIN โ โ Layer 4: outside space (transparent)
โ +---------------------------------------+ โ
โ | BORDER | โ โ Layer 3: the visible frame/edge
โ | +-------------------------------+ | โ
โ | | PADDING | | โ โ Layer 2: inside space (fills w/ bg)
โ | | +-----------------------+ | | โ
โ | | | CONTENT | | | โ โ Layer 1: your actual text/image
โ | | | width ร height | | | โ
โ | | +-----------------------+ | | โ
โ | +-------------------------------+ | โ
โ +---------------------------------------+ โ
+===============================================+Let's go through each layer from the inside out.
Layer 1 ยท Content
The content area is where your actual text, images, or other HTML lives. This is what you see when you type words inside a <p> tag or place an <img> tag on the page.
When you set width and height in CSS, by default you are only setting the size of this content area ยท not the total size of the element on screen. This is the most important and most misunderstood aspect of the Box Model.
div {
width: 300px;
height: 150px;
}This says: "Make the content area inside this div 300px wide and 150px tall." It says nothing about padding, border, or margin ยท those are separate.
Layer 2 ยท Padding
Padding is the transparent space between the content and the border. It sits inside the border, so it shares the element's background colour.
Think of padding as the cushion or breathing room around your content inside the box.
div {
padding: 20px;
}This adds 20px of cushioning on all four sides between the content and the border.
Key fact: Padding is part of the element's clickable/visible area. If an element has a background colour, the padding area will show that background colour too.
Layer 3 ยท Border
The border is the line that wraps around the padding and content. It is the element's visible frame or edge.
div {
border: 5px solid gray;
}This adds a 5px wide, solid grey border around the element. The border has a measurable thickness ยท and that thickness adds to the element's total size.
Layer 4 ยท Margin
Margin is the transparent space outside the border. It separates this element from neighbouring elements. Margin is never coloured and never shows the element's background ยท it is always invisible.
div {
margin: 30px;
}This pushes 30px of empty space on all sides outside the border, creating distance from adjacent elements.
1.3 All Four Layers Together ยท A Complete Example
div {
width: 320px;
height: 50px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}Let's see each layer in action:
- Content is 320px wide and 50px tall
- Padding adds 10px on all four sides ยท cushioning between content and border
- Border adds 5px on all four sides ยท the visible grey frame
- Margin is 0 ยท no space outside the border
This element appears as a grey-bordered box. But what is its actual total width on screen? That brings us to the most critical calculation in the entire Box Model.
2.1 The Big Surprise: width Is Not the Full Width
This is the concept that trips up beginners more than anything else in CSS.
When you write width: 320px, you are only setting the width of the content area. The padding and border are added on top of that width to calculate what the browser actually renders.
โ ๏ธ Critical Rule: By default,
widthandheightonly apply to the content area. Padding and border are added on top to get the real rendered size.
2.2 The Total Width Formula
Total Element Width = width
+ left padding + right padding
+ left border + right border2.3 The Total Height Formula
Total Element Height = height
+ top padding + bottom padding
+ top border + bottom border2.4 Step-by-Step Width Calculation ยท The W3Schools Example
Let's carefully work through the canonical W3Schools Box Model example:
div {
width: 320px;
height: 50px;
padding: 10px;
border: 5px solid gray;
margin: 0;
}Step 1 ยท Identify each value:
- Content width:
320px - Content height:
50px - Padding:
10pxon all four sides (so left padding = 10, right padding = 10) - Border:
5pxon all four sides (so left border = 5, right border = 5) - Margin:
0(does not affect the element's own size)
Step 2 ยท Calculate total width:
Width = 320px (content area)
+ 10px (left padding)
+ 10px (right padding)
+ 5px (left border)
+ 5px (right border)
= -------
350px โ TOTAL RENDERED WIDTHStep 3 ยท Calculate total height:
Height = 50px (content area)
+ 10px (top padding)
+ 10px (bottom padding)
+ 5px (top border)
+ 5px (bottom border)
= -------
80px โ TOTAL RENDERED HEIGHTExpected result: This div is painted as a 350px ร 80px box on the screen ยท even though you only wrote width: 320px and height: 50px. The padding and border expand it beyond what you declared.
2.5 What About Margin?
This is a subtle but important point.
Margin does NOT affect the size of the element itself. The element's rendered box stops at the border. Margin is space outside the box ยท it affects how much room the element takes up on the page, but it is not counted in the element's own width or height.
The official W3Schools rule states it clearly:
"The margin property also affects the total space that the box will take up on the page, but the margin is not included in the actual size of the box. The box's total width and height stops at the border."
Think of it this way: if you measure a picture frame, you measure up to and including the frame itself. The gap between this frame and the next frame on the wall is real space ยท but it is not part of this frame's measurement.
2.6 More Worked Examples
Example A ยท Large padding, no border:
.card {
width: 400px;
height: 200px;
padding: 30px;
border: 0;
margin: 20px;
}Total width calculation:
400px (content)
+ 30px (left padding)
+ 30px (right padding)
+ 0px (left border)
+ 0px (right border)
= 460px โ total rendered widthTotal height calculation:
200px (content)
+ 30px (top padding)
+ 30px (bottom padding)
+ 0px (top border)
+ 0px (bottom border)
= 260px โ total rendered heightMargin (20px) does NOT change these sizes ยท it just creates space around the box.
Example B ยท Asymmetric padding and border:
.banner {
width: 600px;
height: 80px;
padding-top: 10px;
padding-bottom: 10px;
padding-left: 40px;
padding-right: 40px;
border-top: 2px solid black;
border-bottom: 2px solid black;
border-left: 0;
border-right: 0;
}Total width calculation:
600px (content)
+ 40px (left padding)
+ 40px (right padding)
+ 0px (left border โ none)
+ 0px (right border โ none)
= 680px โ total rendered widthTotal height calculation:
80px (content)
+ 10px (top padding)
+ 10px (bottom padding)
+ 2px (top border)
+ 2px (bottom border)
= 104px โ total rendered height๐ก Thinking Prompt: In Example B, what if you changed
padding-leftto60px? The content width stays600px, but the total rendered width becomes600 + 60 + 40 + 0 + 0 = 700px. The content area itself does not shrink ยท the whole box grows wider.
2.7 Why This Causes Problems in Real Layouts
Here is the classic layout-breaking scenario that every developer runs into:
Imagine you want two boxes side by side inside a 600px container. You give each box width: 300px ยท exactly half. Perfect.
.container {
width: 600px;
}
.box {
width: 300px; /* 300 + 300 = 600, they should fit! */
padding: 20px;
border: 2px solid black;
float: left;
}What actually happens:
Each box actual width:
300px + 20px + 20px + 2px + 2px = 344px
Two boxes: 344px + 344px = 688px
Container: 600px
688 > 600 โ The second box WRAPS to the next line! ๐ฅThe layout breaks because width: 300px only set the content area. Padding and border pushed each box to 344px, which no longer fits.
This is the exact problem that box-sizing: border-box was designed to solve ยท which we cover in Part 3.
3.1 The Default: box-sizing: content-box
By default, every browser uses box-sizing: content-box. This is what we have been describing all along:
widthandheightapply only to the content area- Padding and border are added on top โ the element is larger than declared
/* Default behaviour โ content-box */
div {
box-sizing: content-box; /* this is the default, you don't need to write it */
width: 300px;
padding: 20px;
border: 5px solid black;
/* actual rendered width = 300 + 20 + 20 + 5 + 5 = 350px */
}3.2 The Better Way: box-sizing: border-box
When you set box-sizing: border-box, the calculation changes completely:
widthandheightdefine the total rendered size including padding and border- Padding and border are subtracted from inside the declared size โ the element is exactly as wide as you declared
- The content area shrinks to accommodate the padding and border
/* border-box โ the element is exactly 300px total */
div {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
/* actual rendered width = 300px (padding + border eat into content space) */
/* content area = 300 - 20 - 20 - 5 - 5 = 250px */
}The total rendered width is exactly 300px ยท no wider.
3.3 Side-by-Side Comparison
/* Box 1 โ content-box (default) */
.box-a {
box-sizing: content-box;
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid blue;
}
/* Total rendered width = 300 + 50 + 50 + 1 + 1 = 402px */
/* Total rendered height = 100 + 50 + 50 + 1 + 1 = 202px */
/* Box 2 โ border-box */
.box-b {
box-sizing: border-box;
width: 300px;
height: 100px;
padding: 50px;
border: 1px solid red;
}
/* Total rendered width = 300px (exactly as declared) */
/* Total rendered height = 100px (exactly as declared) */
/* Content area width = 300 - 50 - 50 - 1 - 1 = 198px */
/* Content area height = 100 - 50 - 50 - 1 - 1 = -2px โ content collapses */โ ๏ธ Note: In
.box-bwithpadding: 50pxon each side and only100pxheight, the content area height becomes negative ยท meaning there is no room for content at all. In practice you would not use such extreme padding. The example shows thatborder-boxredistributes the space inward.
Expected result: .box-a appears much larger than .box-b even though both declare width: 300px. .box-a is 402px wide; .box-b is exactly 300px wide.
3.4 Solving the Two-Box Layout Problem with border-box
Remember the layout-breaking problem from Section 2.7? Here is the fix:
.container {
width: 600px;
}
.box {
box-sizing: border-box; /* magic fix */
width: 300px; /* NOW this includes padding and border */
padding: 20px;
border: 2px solid black;
float: left;
}Now each box:
- Is exactly
300pxwide (total, including padding and border) - Two boxes:
300 + 300 = 600px - Container:
600px - They fit perfectly side by side โ
3.5 The Universal border-box Reset
Because border-box is so much more intuitive and predictable, the vast majority of professional developers apply it to every element on every project. The standard way to do this is with the universal selector *:
* {
box-sizing: border-box;
}The * selector targets every single HTML element on the page. This one line ensures that width and height always mean the total box size (including padding and border) for every element ยท no surprises, no overflow bugs from unexpected box growth.
๐ก This is considered a best practice and is used in virtually every professional CSS project, framework (like Bootstrap and Tailwind), and design system in the world. Most developers add it at the very top of their CSS file before writing anything else.
3.6 Complete Comparison Table
| Scenario | content-box (default) | border-box |
|---|---|---|
width applies to | Content area only | Content + padding + border |
| Adding padding | Makes box grow beyond declared width | Box stays declared width; content shrinks |
| Adding border | Makes box grow beyond declared width | Box stays declared width; content shrinks |
| Layout maths | Harder ยท must subtract padding/border from width | Easier ยท width is the final size |
| Industry usage | Browser default (legacy) | Used by almost all professionals |
4.1 Seeing the Box Model Live
Every modern browser (Chrome, Firefox, Edge) has a built-in tool that draws the Box Model for any element you click on. This is one of the most valuable debugging tools in web development.
How to open DevTools:
- Windows/Linux:
F12orCtrl + Shift + I - Mac:
Cmd + Option + I - Or: right-click any element on the page โ click Inspect
4.2 What You See in the Box Model Panel
In Chrome/Edge DevTools:
- Click the Elements tab at the top
- Click on any HTML element in the panel
- Scroll down in the right-hand Styles pane until you see the box diagram
The diagram looks like this:
+-------------------------------+
| margin |
| +-----------------------+ |
| | border | |
| | +---------------+ | |
| | | padding | | |
| | | +-------+ | | |
| | | |content| | | |
| | | |320ร50 | | | |
| | | +-------+ | | |
| | +---------------+ | |
| +-----------------------+ |
+-------------------------------+Each layer is labelled and shows its exact pixel values. Hovering over the diagram highlights that layer on the webpage itself in a different colour:
- Blue โ content area
- Green โ padding
- Orange/yellow โ border
- Beige/tan โ margin
This makes it extremely easy to:
- See what is causing unexpected sizing
- Verify your calculations are correct
- Identify which layer is pushing elements out of alignment
4.3 Live Editing in DevTools
You can double-click any value in the DevTools Styles panel and change it instantly. The page updates in real time ยท without saving any files. This is perfect for experimenting with different spacing values before committing to them in your CSS.
๐ก Professional tip: Never guess spacing values. Open DevTools, inspect the element, tweak the numbers live until it looks right, then copy those final values back into your CSS file.
Exercise 1 ยท Total Width and Height Calculations
Work through each of the following. Calculate the total rendered width and total rendered height for each element.
Problem A:
.box-a {
width: 200px;
height: 100px;
padding: 15px;
border: 3px solid black;
margin: 10px;
}Work it out before reading the answer.
Total Width:
200px (content)
+ 15px (left padding)
+ 15px (right padding)
+ 3px (left border)
+ 3px (right border)
= 236px โ total rendered widthTotal Height:
100px (content)
+ 15px (top padding)
+ 15px (bottom padding)
+ 3px (top border)
+ 3px (bottom border)
= 136px โ total rendered heightMargin (10px) does NOT affect the element's own size.
Problem B:
.box-b {
width: 500px;
height: 300px;
padding-top: 20px;
padding-bottom: 20px;
padding-left: 40px;
padding-right: 40px;
border: 1px solid red;
margin: 0;
}Total Width:
500px (content)
+ 40px (left padding)
+ 40px (right padding)
+ 1px (left border)
+ 1px (right border)
= 582px โ total rendered widthTotal Height:
300px (content)
+ 20px (top padding)
+ 20px (bottom padding)
+ 1px (top border)
+ 1px (bottom border)
= 342px โ total rendered heightProblem C:
.box-c {
width: 150px;
height: 150px;
padding: 0;
border: 10px double navy;
margin: 25px;
}โ ๏ธ Note:
doubleis a border-style value (two thin lines instead of one solid line). The border thickness is still10px.
Total Width:
150px (content)
+ 0px (left padding)
+ 0px (right padding)
+ 10px (left border)
+ 10px (right border)
= 170px โ total rendered widthTotal Height:
150px (content)
+ 0px (top padding)
+ 0px (bottom padding)
+ 10px (top border)
+ 10px (bottom border)
= 170px โ total rendered height (a perfect square)Self-check questions:
- In Problem A, would a
marginof50pxchange the total rendered width? (No ยท margin is outside the box, it does not change the element's own size.) - In Problem C, is the total rendered size affected by the
doublestyle vssolid? (No ยท only the thickness value10pxmatters for size calculation, not the style.)
Exercise 2 ยท Working Backwards: Finding Content Width
Sometimes you know the total width you need, and you have to work out what width value to set given known padding and border.
Formula:
Content width = Total desired width
โ left padding โ right padding
โ left border โ right borderProblem A: You want a .sidebar to be exactly 280px wide on screen. It has padding: 20px and border: 2px solid grey. What width value should you set?
Content width = 280 โ 20 โ 20 โ 2 โ 2
= 280 โ 44
= 236pxSolution:
.sidebar {
width: 236px; /* content area */
padding: 20px;
border: 2px solid grey;
/* total rendered width: 236 + 20 + 20 + 2 + 2 = 280px โ
*/
}Problem B: A .hero-banner must be exactly 960px wide. It has padding-left: 60px, padding-right: 60px, and border: 4px solid transparent. What should width be?
Content width = 960 โ 60 โ 60 โ 4 โ 4
= 960 โ 128
= 832pxSolution:
.hero-banner {
width: 832px;
padding-left: 60px;
padding-right: 60px;
border: 4px solid transparent;
/* total rendered width: 832 + 60 + 60 + 4 + 4 = 960px โ
*/
}Problem C (with border-box): What if you simply use box-sizing: border-box? Solve Problem B again.
/* With border-box: you just write the total you want! */
.hero-banner {
box-sizing: border-box;
width: 960px; /* this IS the total โ no calculation needed */
padding-left: 60px;
padding-right: 60px;
border: 4px solid transparent;
/* total rendered width: 960px exactly โ
*/
}๐ก This is exactly why
border-boxis so widely adopted. The maths disappears ยท you just write the width you want.
Exercise 3 ยท Spotting the Problem and Fixing It
Scenario: A developer tried to create a three-column layout inside a 900px container. Each column is supposed to be exactly 300px wide. But the third column keeps wrapping to the next line. Find the bug and fix it.
Broken code:
.container {
width: 900px;
}
.column {
width: 300px;
padding: 15px;
border: 2px solid #ccc;
float: left;
}Diagnosing the bug:
Each column actual width:
300px + 15px + 15px + 2px + 2px = 334px
Three columns: 334 ร 3 = 1002px
Container: 900px
1002 > 900 โ Third column wraps! ๐ฅFix using border-box:
.container {
width: 900px;
}
.column {
box-sizing: border-box; /* โ the fix */
width: 300px; /* now truly 300px total */
padding: 15px;
border: 2px solid #ccc;
float: left;
}
/* Three columns: 300 ร 3 = 900px โ fits perfectly โ
*/These challenges mirror the style of questions from the W3Schools CSS Box Model Code Challenge page.
Challenge 1 ยท Calculate a Total Width
Question: Using the default box-sizing: content-box, what is the total rendered width of this element?
div {
width: 250px;
padding: 25px;
border: 5px solid black;
}Work it out:
250px + 25px + 25px + 5px + 5px = 310pxAnswer: 310px
Challenge 2 ยท Calculate a Total Height
Question: What is the total rendered height of this element?
section {
height: 120px;
padding-top: 10px;
padding-bottom: 30px;
border-top: 4px solid blue;
border-bottom: 4px solid blue;
}Work it out:
120px + 10px + 30px + 4px + 4px = 168pxAnswer: 168px
Challenge 3 ยท What Does border-box Change?
Question: With box-sizing: border-box applied, what is the total rendered width of this element?
div {
box-sizing: border-box;
width: 400px;
padding: 40px;
border: 10px solid red;
}Answer: 400px ยท with border-box, the declared width IS the total. The content area shrinks to accommodate the padding and border: 400 โ 40 โ 40 โ 10 โ 10 = 300px content area.
Challenge 4 ยท Identify the Difference
Question: Two divs both have width: 300px. One uses content-box, one uses border-box. Both have padding: 20px and border: 5px solid. How much wider is the content-box div than the border-box div?
content-box total width:
300 + 20 + 20 + 5 + 5 = 350pxborder-box total width:
300px (exactly as declared)Difference: 350 โ 300 = 50px
The content-box div is 50px wider on screen, even though both declare the same width: 300px.
Challenge 5 ยท Fix the Overflowing Layout
Question: A developer has a container that is 500px wide. They add two divs inside it, each with width: 250px, padding: 20px, and border: 5px solid. The second div wraps to the next line. Add one property to each div to fix this.
Fix:
div {
box-sizing: border-box; /* โ add this */
width: 250px;
padding: 20px;
border: 5px solid;
float: left;
}
/* Each div is now exactly 250px; 250 + 250 = 500px โ fits! โ
*/Challenge 6 ยท Write the CSS for a Given Total Size
Question: Write CSS for a .notification-bar that:
- Must be exactly
760pxwide and60pxtall on screen - Must have
padding: 10pxon all sides - Must have a
border: 3px solid orange - Use
content-boxsizing (nobox-sizing: border-box)
Solution:
Content width = 760 โ 10 โ 10 โ 3 โ 3 = 734px Content height = 60 โ 10 โ 10 โ 3 โ 3 = 34px
.notification-bar {
width: 734px;
height: 34px;
padding: 10px;
border: 3px solid orange;
/* total: 734 + 20 + 6 = 760px wide โ
*/
/* total: 34 + 20 + 6 = 60px tall โ
*/
}Mistake 1 ยท Assuming width Is the Total Width
/* WRONG assumption: "this box is 300px wide" */
.box {
width: 300px;
padding: 20px;
border: 5px solid;
}
/* Actual width: 300 + 20 + 20 + 5 + 5 = 350px */
/* RIGHT approach: use border-box */
.box {
box-sizing: border-box;
width: 300px; /* THIS is actually 300px */
padding: 20px;
border: 5px solid;
}Mistake 2 ยท Including Margin in the Size Calculation
.box {
width: 200px;
padding: 10px;
border: 2px solid;
margin: 30px;
}
/* WRONG: "total size = 200 + 10 + 10 + 2 + 2 + 30 + 30 = 284px" โ */
/* RIGHT: total rendered size = 200 + 10 + 10 + 2 + 2 = 224px โ
*/
/* Margin (30px each side) affects page space, NOT the element's own size */Mistake 3 ยท Forgetting to Set box-sizing Globally
/* INCOMPLETE โ only applies to div, not to every element */
div {
box-sizing: border-box;
}
/* CORRECT โ applies to every single element */
* {
box-sizing: border-box;
}If you only apply border-box to some elements, you end up with inconsistent sizing behaviour across your page ยท some elements use one model, others use another, and layouts become unpredictable.
Mistake 4 ยท Setting a Width That Leaves No Room for Content
/* PROBLEMATIC with border-box */
.tiny-box {
box-sizing: border-box;
width: 50px;
padding: 30px; /* 30 + 30 = 60px โ more than the total width! */
border: 5px solid;
}
/* Content area = 50 - 30 - 30 - 5 - 5 = -20px โ collapses to 0 */With border-box, if your padding and border together exceed the declared width, the content area collapses to zero. Content will overflow or be invisible. Always make sure your width is large enough to accommodate your padding and border.
Mistake 5 ยท Thinking Padding and Margin Do the Same Thing
/* PADDING โ space INSIDE the border, fills with background colour */
.card {
background-color: lightblue;
padding: 20px; /* the lightblue area extends 20px around the content */
}
/* MARGIN โ space OUTSIDE the border, always transparent */
.card {
margin: 20px; /* transparent gap between this card and other elements */
}Use padding when you want space between content and border (inner cushion). Use margin when you want space between this element and the elements around it (outer gap).
Mistake 6 ยท Adding Border Without Accounting for It
/* You want a nav bar that is exactly 960px wide */
.navbar {
width: 960px;
padding: 0 20px;
border: 2px solid #333; /* often forgotten in the size calculation */
}
/* Actual width (content-box): 960 + 20 + 20 + 2 + 2 = 1004px */
/* The navbar is wider than intended and may overflow the page! */
/* Fix โ use border-box */
.navbar {
box-sizing: border-box;
width: 960px; /* total, including border */
padding: 0 20px;
border: 2px solid #333;
}Every CSS layout system ever built is grounded in the Box Model. Flexbox, CSS Grid, Bootstrap's 12-column grid, Tailwind CSS utility classes ยท all of them are ultimately arranging boxes defined by the Box Model rules you learned in this lesson.
box-sizing: border-box is in every major CSS framework. Bootstrap resets to border-box in its base stylesheet. Tailwind does too. Any professional boilerplate or starter template you encounter in the industry will include * { box-sizing: border-box; } at the very top.
The DevTools Box Model diagram is one of the first things professional developers open when something in a layout looks wrong. Being fluent with this tool will dramatically reduce your debugging time on every project.
Precise size calculations matter enormously in UI components. When building a product card, a modal dialog, a navigation bar, or a form input, you need to know exactly how wide something will be so it aligns correctly with everything around it. The Box Model gives you the formula to always know.
Build a polished product card component that demonstrates complete mastery of the Box Model ยท every dimension will be precisely calculated and explained.
Stage 1 ยท Project Goal and Setup
You will build a product card like you would see on an e-commerce website. The card must:
- Be exactly 320px wide as rendered on screen
- Contain an image placeholder, product name, description, price, and a buy button
- Use
box-sizing: border-boxglobally - Have all spacing controlled with precise padding, border, and margin values
- Include a comment for each CSS rule explaining the Box Model reasoning
Create two files: product.html and product.css.
Stage 2 ยท HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Product Card โ Box Model Demo</title>
<link rel="stylesheet" href="product.css">
</head>
<body>
<div class="page">
<div class="card">
<!-- Image placeholder -->
<div class="card-image">
<span class="image-label">Product Image</span>
</div>
<!-- Product details -->
<div class="card-body">
<p class="card-category">Electronics</p>
<h2 class="card-title">Wireless Noise-Cancelling Headphones</h2>
<p class="card-description">
Crystal-clear audio with up to 30 hours of battery life.
Foldable design for easy travel.
</p>
<div class="card-footer">
<span class="card-price">โฆ89,500</span>
<button class="card-button">Add to Cart</button>
</div>
</div>
</div>
</div>
</body>
</html>