LESSON 7.4
The Box Model
Understanding how every element is structured
Discover how the CSS Box Model ties together content, padding, border, and margin into a predictable system.
💡 The Concept
Now that you know how to use padding and margin, it helps to understand the system behind them. Every HTML element on the page is a rectangular box. The CSS Box Model describes the four layers that make up that box, from the inside out:
- Content: the text, image, or other content inside the element
- Padding: space between the content and the border, inside the border
- Border: the line that wraps around the padding and content
- Margin: space outside the border, separating the element from others

Understanding the box model helps you predict how elements behave when you resize them, add borders, or adjust spacing. When you look at a page in Chrome DevTools, the box model diagram in the bottom-right of the Styles panel shows the exact pixel values of each layer for any selected element.
box-sizing: border-box
By default, when you set a width on an element, that width applies only to the content area. Padding and border are added on top of it, which can make elements wider than you expect. Most developers add this rule to every project to fix that:
* {
box-sizing: border-box;
}
With border-box, the width you set includes the padding and border, so elements stay the size you intended. The * selector applies the rule to every element on the page. You will see this in almost every professional stylesheet.
🎬 Video Demonstration
This video demonstrates how the box model works and how all four layers interact.
Exercise: Try It Yourself
This is the same exercise as the previous lesson. If you completed it there, you can skip ahead. It is here in case you arrived at this page directly.
Step 1. Open JSFiddle and paste this into the HTML panel:
<h1>Spacing Demo</h1>
<img src="https://webdevstudents.com/wp-content/uploads/2025/09/axylotl.png" alt="pink axolotl in a tank" width="100" height="100">
Step 2. Paste this into the CSS panel:
img {
background-color: lightblue;
border: 5px solid steelblue;
}
Step 3. Add padding: 20px to img. Watch the space grow inside the border.
Step 4. Add margin: 30px to img. Watch the space push other elements away. Your JSFiddle now looks like the screenshot below. Note the lightblue background color shows the padding.

Step 5. Now try centering the image. Add display: block to img and change the margin to 0 auto. Images are inline by default, so display: block is needed for centering to work.

❓ Frequently Asked Questions
content-box is the CSS default. When you set a width, it applies only to the content area, and padding and border are added on top, making the element wider than the value you set. border-box is the modern approach: the width you set includes padding and border, so the element stays the size you intended. Almost every professional stylesheet today starts with * { box-sizing: border-box; } to avoid unexpected sizing surprises.
When you place elements side by side or try to fit them into a grid, unexpected sizing causes columns to break or overflow. If you expect a div to be 300px wide but it is actually 340px because of padding and border, your layout breaks. Understanding the box model lets you predict exactly how much space an element takes up, so your layouts behave the way you intend.
Vertical margins between block elements collapse: the space between them is the larger of the two margins, not the sum. For example, if one paragraph has margin-bottom: 20px and the next has margin-top: 30px, the space between them is 30px, not 50px. Horizontal margins do not collapse and always add up. This behavior is called margin collapsing and is one of the first things that surprises developers new to CSS.
Yes. In Chrome DevTools, click any element in the Elements panel and scroll down in the Styles panel. You will find a diagram showing the exact pixel values of the content area, padding, border, and margin for that element. It is color-coded to match what you see when you hover over elements on the page: padding in green and margin in salmon. This is one of the most useful tools for debugging spacing issues.
★ Key Takeaways
Every element on a web page lives inside an invisible box with four layers: content, padding, border, and margin. Understanding the box model gives you a mental picture of why your spacing and sizing behave the way they do. Next, you will learn how to center content on the page.
Google Ads
The costs of this website are partially offset by revenue from Google Ads.Tools of the Trade
HTML/CSS Lessons
-
-
-
1.3. HTML Tag Preview
-
-
-
2.2. The HTML Framework
-
-
2.4. HTML Headings
-
-
-
-
-
-
3.01. Introduction to CSS
-
3.02. How CSS Cascades
-
-
-
-
3.05. CSS Colors
-
3.06. RGBA Colors
-
-
3.08. Font and Text Properties
-
3.09. Classes and IDs
-
-
-
-
-
4.3. HTML Embedded Images
-
-
-
-
-
-
5.1. CSS Borders
-
5.2. CSS Border Radius
-
5.3. CSS Gradients
-
5.4. CSS Box Shadows
-
5.5. CSS Text Shadows
-
7.1. The Display Property
-
-
-
7.4. The Box Model
-
-
-
-
-
-
9.1. Website Tables
-
9.2. HTML Forms
-
-
9.4. Special Form Fields
-
9.5. Embedding Multimedia
-
-
9.7. Keyboard Shortcuts
-
10.1. What Is a Framework?
-
10.2. Font Awesome Icons
-
-
10.4. Bootstrap Grid System
-
-
10.6. Bootstrap Navigation
-
-
11.2. Purchasing a Domain Name
-
11.3. Website Hosting
-
11.4. Nameservers and DNS
-
11.5. FTP
-
11.6. GitHub Pages
