CSS Box Model
Learn how the CSS Box Model controls the size and spacing of every HTML element using content, padding, border, margin, width, height, and box-sizing.
Introduction
In the previous lessons, you learned how borders create visible edges, margins create space outside elements, and padding creates space inside elements.
Now you will combine all of these concepts and understand one of the most important foundations of CSS: the CSS Box Model.
Every HTML element displayed by the browser is treated as a rectangular box.
That box consists of four main layers: content, padding, border, and margin.
Headings, paragraphs, images, buttons, cards, sections, forms, and containers are all treated as rectangular boxes by the browser.
What is the CSS Box Model?
The CSS Box Model is the system the browser uses to calculate the size, spacing, and position of HTML elements.
Every element is represented as a box containing content, padding, border, and margin.
┌─────────────────────────────────────────────┐
│ MARGIN │
│ │
│ ┌───────────────────────────────────┐ │
│ │ BORDER │ │
│ │ │ │
│ │ ┌─────────────────────────┐ │ │
│ │ │ PADDING │ │ │
│ │ │ │ │ │
│ │ │ ┌───────────────┐ │ │ │
│ │ │ │ CONTENT │ │ │ │
│ │ │ └───────────────┘ │ │ │
│ │ │ │ │ │
│ │ └─────────────────────────┘ │ │
│ │ │ │
│ └───────────────────────────────────┘ │
│ │
└─────────────────────────────────────────────┘| Layer | Purpose |
|---|---|
| Content | Contains text, images, and other content |
| Padding | Creates space around the content |
| Border | Creates the visible edge of the element |
| Margin | Creates space outside the element |
The easiest way to remember the box model is: Content → Padding → Border → Margin.
Why Do We Need It?
Understanding the box model is necessary because CSS width and height do not always represent the final visible size of an element.
Accurate Sizing
Understand the real width and height of elements.
Better Spacing
Control internal and external space correctly.
Predictable Layouts
Prevent unexpected layout movement and overflow.
Responsive Design
Build elements that fit correctly on different screens.
Easier Debugging
Find the reason behind incorrect sizes and spacing.
Better Components
Create consistent cards, buttons, forms, and containers.
Without Box Model Knowledge
- Elements become larger than expected.
- Width: 100% can overflow.
- Spacing feels unpredictable.
- Layouts break unexpectedly.
With Box Model Knowledge
- Element size becomes predictable.
- Overflow problems are easier to solve.
- Spacing becomes intentional.
- Layouts remain controlled.
Real-World Analogy
Imagine ordering a product packed inside a delivery box.
The product is the content. Protective foam around it is the padding. The cardboard wall is the border. The empty space between this box and other boxes is the margin.
| Package Part | CSS Box Model |
|---|---|
| Product | Content |
| Protective foam | Padding |
| Cardboard wall | Border |
| Space between packages | Margin |
Space Between Packages
↓
┌───────────────────────────────┐
│ CARDBOARD WALL │
│ │
│ Protective Foam │
│ │
│ ┌─────────────┐ │
│ │ PRODUCT │ │
│ └─────────────┘ │
│ │
│ Protective Foam │
│ │
└───────────────────────────────┘
Product = Content
Protective Foam = Padding
Cardboard Wall = Border
Outside Space = MarginThe Four Box Model Layers
The CSS Box Model contains four layers arranged from the inside to the outside.
| Order | Layer | Location |
|---|---|---|
| 1 | Content | Center of the element |
| 2 | Padding | Around the content |
| 3 | Border | Around the padding |
| 4 | Margin | Outside the border |
1. Content Area
The content area is the innermost part of the box model.
It contains the actual content of the element, such as text, images, videos, form controls, or child elements.
<div class="box">
CSS Box Model
</div>.box {
width: 300px;
height: 100px;
}With the default content-box sizing model, width and height define the size of the content area.
Content Width = 300px
Content Height = 100px
┌─────────────────────────────┐
│ │
│ CONTENT AREA │ 100px
│ │
└─────────────────────────────┘
300pxUnder the default content-box model, declared width and height apply to the content area before padding and borders are added.
2. Padding Area
The padding area surrounds the content and creates internal space between the content and the border.
.box {
width: 300px;
height: 100px;
padding: 20px;
}┌───────────────────────────────────┐
│ PADDING │
│ │
│ ┌─────────────────────────┐ │
│ │ │ │
│ │ CONTENT │ │
│ │ │ │
│ └─────────────────────────┘ │
│ │
│ PADDING │
└───────────────────────────────────┘The element background extends through both the content and padding areas.
3. Border Area
The border area surrounds the content and padding.
.box {
width: 300px;
height: 100px;
padding: 20px;
border: 5px solid #2d3436;
}┌─────────────────────────────────────┐
│ BORDER │
│ ┌─────────────────────────────┐ │
│ │ PADDING │ │
│ │ │ │
│ │ ┌───────────────────┐ │ │
│ │ │ CONTENT │ │ │
│ │ └───────────────────┘ │ │
│ │ │ │
│ └─────────────────────────────┘ │
└─────────────────────────────────────┘4. Margin Area
The margin area is the outermost layer of the box model.
It creates transparent space between the element and surrounding elements.
.box {
width: 300px;
height: 100px;
padding: 20px;
border: 5px solid #2d3436;
margin: 30px;
}┌───────────────────────────────────────────┐
│ MARGIN │
│ │
│ ┌───────────────────────────────────┐ │
│ │ BORDER │ │
│ │ ┌───────────────────────────┐ │ │
│ │ │ PADDING │ │ │
│ │ │ ┌───────────────────┐ │ │ │
│ │ │ │ CONTENT │ │ │ │
│ │ │ └───────────────────┘ │ │ │
│ │ └───────────────────────────┘ │ │
│ └───────────────────────────────────┘ │
│ │
└───────────────────────────────────────────┘The element background does not extend into the margin area because margin exists outside the border.
Complete Box Model Structure
The following example uses all four parts of the CSS Box Model.
<div class="box">
CSS Box Model
</div>.box {
width: 300px;
height: 100px;
padding: 20px;
border: 5px solid #2d3436;
margin: 30px;
background: #6c5ce7;
color: white;
}| Property | Value | Purpose |
|---|---|---|
| width | 300px | Content width |
| height | 100px | Content height |
| padding | 20px | Internal spacing |
| border | 5px | Visible edge |
| margin | 30px | External spacing |
Width Calculation
With the default content-box model, the total width of an element includes content width, horizontal padding, and horizontal borders.
Total Width =
Content Width
+ Left Padding
+ Right Padding
+ Left Border
+ Right BorderIf you also want to calculate the complete horizontal space occupied in the layout, include the left and right margins.
Total Horizontal Space =
Left Margin
+ Left Border
+ Left Padding
+ Content Width
+ Right Padding
+ Right Border
+ Right MarginMargin affects the space occupied around an element, but it is not part of the visible border box.
Height Calculation
The total height of an element follows the same principle.
Total Height =
Content Height
+ Top Padding
+ Bottom Padding
+ Top Border
+ Bottom BorderTotal Vertical Space =
Top Margin
+ Top Border
+ Top Padding
+ Content Height
+ Bottom Padding
+ Bottom Border
+ Bottom MarginExample 1: Total Width
.box {
width: 300px;
padding-left: 20px;
padding-right: 20px;
border-left: 5px solid black;
border-right: 5px solid black;
margin-left: 30px;
margin-right: 30px;
}Content Width = 300px
Left Padding = 20px
Right Padding = 20px
Left Border = 5px
Right Border = 5px
Border Box Width
= 300 + 20 + 20 + 5 + 5
= 350pxLeft Margin = 30px
Border Box = 350px
Right Margin = 30px
Total Horizontal Space
= 30 + 350 + 30
= 410pxExample 2: Total Height
.box {
height: 100px;
padding-top: 20px;
padding-bottom: 20px;
border-top: 5px solid black;
border-bottom: 5px solid black;
margin-top: 30px;
margin-bottom: 30px;
}Content Height = 100px
Top Padding = 20px
Bottom Padding = 20px
Top Border = 5px
Bottom Border = 5px
Border Box Height
= 100 + 20 + 20 + 5 + 5
= 150pxTop Margin = 30px
Border Box Height = 150px
Bottom Margin = 30px
Total Vertical Space
= 30 + 150 + 30
= 210pxThe box-sizing Property
The box-sizing property controls how the browser calculates the declared width and height of an element.
selector {
box-sizing: value;
}| Value | Behavior |
|---|---|
| content-box | Width and height apply only to content |
| border-box | Width and height include content, padding, and border |
The same width, padding, and border values can produce different final sizes depending on the box-sizing value.
content-box
content-box is the default box-sizing value.
With content-box, width and height apply only to the content area.
.box {
box-sizing: content-box;
width: 300px;
padding: 20px;
border: 5px solid black;
}Declared Width = 300px
Content = 300px
Left Padding = 20px
Right Padding = 20px
Left Border = 5px
Right Border = 5px
Final Border Box Width
= 300 + 20 + 20 + 5 + 5
= 350pxWith content-box, padding and borders are added outside the declared width and height.
border-box
With border-box, the declared width and height include the content, padding, and border.
.box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid black;
}Declared Width = 300px
Total Border Box Width = 300px
Inside the 300px:
Left Border = 5px
Left Padding = 20px
Content = 250px
Right Padding = 20px
Right Border = 5px
5 + 20 + 250 + 20 + 5 = 300pxborder-box is commonly preferred because the declared width remains the final border box width.
content-box vs border-box
The following example compares both sizing models using identical width, padding, and border values.
.box {
width: 300px;
padding: 20px;
border: 5px solid black;
}
.content-box {
box-sizing: content-box;
}
.border-box {
box-sizing: border-box;
}| Feature | content-box | border-box |
|---|---|---|
| Default value | Yes | No |
| Width includes content | Yes | Yes |
| Width includes padding | No | Yes |
| Width includes border | No | Yes |
| Final size predictable | Less predictable | More predictable |
| Common in modern layouts | Less common | Very common |
Global border-box Rule
Many modern projects apply border-box sizing to every element.
* {
box-sizing: border-box;
}A more complete version also includes pseudo-elements.
*,
*::before,
*::after {
box-sizing: border-box;
}Applying border-box globally makes component sizing more predictable because padding and borders remain inside declared dimensions.
Box Model with Percentage Width
The difference between content-box and border-box becomes especially important when using percentage widths.
.box {
width: 100%;
padding: 30px;
border: 5px solid black;
}With content-box, the element starts at 100% width and then adds padding and borders.
Content Width = 100%
Then Add:
Left Padding
Right Padding
Left Border
Right Border
Final Width > 100%
Result: Possible Horizontal Overflow.box {
width: 100%;
padding: 30px;
border: 5px solid black;
box-sizing: border-box;
}Total Border Box Width = 100%
Content + Padding + Border
all fit inside the 100% width.
Result: No Extra WidthWhy Elements Overflow
A common beginner problem occurs when an element unexpectedly becomes wider than its parent.
<div class="container">
<div class="box">
Content
</div>
</div>.container {
width: 500px;
}
.box {
width: 100%;
padding: 40px;
border: 5px solid black;
}Parent Width = 500px
Child Content Width = 100%
= 500px
Padding = 80px
Borders = 10px
Final Child Width
= 500 + 80 + 10
= 590px
Overflow = 90px.box {
width: 100%;
padding: 40px;
border: 5px solid black;
box-sizing: border-box;
}Box Model and Background
An element background covers the content and padding areas and normally extends underneath the border.
The background does not extend into the margin area.
┌─────────────────────────────────────┐
│ MARGIN │ ← Transparent
│ │
│ ┌─────────────────────────────┐ │
│ │ BORDER │ │
│ │ ┌───────────────────────┐ │ │
│ │ │ PADDING │ │ │ ← Background
│ │ │ ┌─────────────────┐ │ │ │
│ │ │ │ CONTENT │ │ │ │ ← Background
│ │ │ └─────────────────┘ │ │ │
│ │ └───────────────────────┘ │ │
│ └─────────────────────────────┘ │
│ │
└─────────────────────────────────────┘Box Model and Inline Elements
Inline elements participate in the box model differently from normal block elements.
<p>
Learn <span class="highlight">CSS Box Model</span> today.
</p>.highlight {
width: 300px;
height: 100px;
padding: 10px;
border: 2px solid #6c5ce7;
margin: 10px;
}For normal inline elements, width and height do not behave like they do on block elements.
Horizontal padding and margins affect layout normally, while vertical spacing can visually extend without pushing surrounding lines in the same way as a block box.
.highlight {
display: inline-block;
width: 300px;
height: 100px;
}The inline-block display value allows an element to remain inline while supporting width, height, padding, borders, and margins more predictably.
Box Model and Block Elements
Block elements create rectangular boxes that normally occupy the available horizontal space of their containing block.
<div class="box">
Block Element
</div>.box {
width: 300px;
padding: 20px;
border: 5px solid #6c5ce7;
margin: 30px;
}Width, height, padding, border, and margin can be directly controlled on block elements.
| Feature | Block Element | Inline Element |
|---|---|---|
| Starts on new line | Usually yes | No |
| Width property | Works normally | Usually ignored |
| Height property | Works normally | Usually ignored |
| Horizontal padding | Works | Works |
| Vertical padding | Works in layout | Can overlap surrounding lines |
| Horizontal margin | Works | Works |
| Vertical margin | Works | Limited effect on line layout |
Inspecting the Box Model
Browser developer tools provide a visual representation of the box model for every element.
┌─────────────────────────────────────┐
│ margin: 30 │
│ ┌─────────────────────────────┐ │
│ │ border: 5 │ │
│ │ ┌───────────────────────┐ │ │
│ │ │ padding: 20 │ │ │
│ │ │ ┌─────────────────┐ │ │ │
│ │ │ │ 300 × 100 │ │ │ │
│ │ │ └─────────────────┘ │ │ │
│ │ └───────────────────────┘ │ │
│ └─────────────────────────────┘ │
└─────────────────────────────────────┘When an element is too large, too small, or incorrectly spaced, inspect its box model before changing random CSS values.
Complete Card Example
The following example combines every major box model concept in a practical course card.
<article class="course-card">
<span class="badge">
CSS
</span>
<h2>
Master CSS
</h2>
<p>
Learn modern website styling,
layouts, and responsive design.
</p>
<a href="#">
Start Learning
</a>
</article>*,
*::before,
*::after {
box-sizing: border-box;
}
.course-card {
width: 100%;
max-width: 420px;
padding: 32px;
border: 1px solid #dfe6e9;
border-radius: 16px;
margin: 30px auto;
background: white;
}
.badge {
display: inline-block;
padding: 6px 12px;
margin-bottom: 16px;
background: rgba(108, 92, 231, 0.12);
color: #6c5ce7;
border-radius: 20px;
}
.course-card h2 {
margin: 0 0 16px;
}
.course-card p {
margin: 0 0 24px;
line-height: 1.7;
}
.course-card a {
display: inline-block;
padding: 12px 24px;
background: #6c5ce7;
color: white;
border-radius: 8px;
text-decoration: none;
}| Box Model Property | Purpose |
|---|---|
| width | Controls card width |
| padding | Creates internal card spacing |
| border | Creates the card edge |
| margin | Creates space around the card |
| box-sizing | Keeps sizing predictable |
Browser Calculation Flow
The browser follows a calculation process to determine the final size of each element.
HTML Element
│
▼
Read CSS Rules
│
▼
Determine Display Type
│
▼
Read Width and Height
│
▼
Check box-sizing
│
├───────────────┐
│ │
▼ ▼
content-box border-box
│ │
▼ ▼
Add Padding Fit Padding Inside
│ │
▼ ▼
Add Border Fit Border Inside
│ │
└───────┬───────┘
│
▼
Apply Margin
│
▼
Position Element
│
▼
Render Final BoxReal-World Applications
Cards
The box model controls card size, inner spacing, borders, and external spacing.
Buttons
Content and padding determine button size and clickable area.
Forms
Inputs rely on border-box for predictable full-width sizing.
Containers
Page containers use width, padding, and margins to organize content.
Navigation
Navigation items use padding, borders, and margins for structure.
Responsive Layouts
border-box prevents percentage-width elements from overflowing.
Images
Borders, padding, and margins affect the final space occupied by images.
UI Components
Reusable components depend on consistent box model calculations.
Advantages
Accurate Sizing
The box model explains the actual dimensions of elements.
Controlled Spacing
Padding and margin provide separate internal and external spacing.
Clear Structure
Content, padding, border, and margin have distinct responsibilities.
Easier Debugging
Unexpected sizes can be traced to specific box model layers.
Better Responsiveness
border-box helps percentage-width elements remain inside containers.
Reusable Components
Predictable sizing makes components easier to reuse.
Better Layout Control
Developers can calculate exactly how much space an element occupies.
Consistent Design
A consistent box model strategy improves the entire interface.
Common Beginner Mistakes
With content-box, padding and borders are added to the declared width.
Horizontal padding increases width and vertical padding increases height under content-box.
Borders also contribute to the final border box size.
Margin affects surrounding layout space but is outside the border box.
Padding creates internal space, while margin creates external space.
The element background does not extend into the margin area.
With content-box, this can make the element wider than its parent.
The box-sizing value completely changes how declared dimensions are calculated.
The default value is content-box unless your CSS changes it.
Inconsistent sizing models can make component behavior harder to predict.
Global box-sizing rules often include ::before and ::after.
A large fixed width can overflow narrow screens.
Content can overflow when a fixed height is too small.
Normal inline elements do not respond to width and height like block elements.
The box model behavior depends partly on whether the element is block, inline, inline-block, flex, or grid.
Changing width repeatedly without checking padding and borders often hides the real problem.
Sometimes border-box is a cleaner solution than manually subtracting padding from width.
Vertical margins between normal block elements can collapse in some situations.
The browser box model inspector can immediately reveal unexpected spacing and dimensions.
Using content-box and border-box without a clear reason can make layouts difficult to maintain.
Best Practices
- Remember that every HTML element is treated as a box.
- Learn the box model from inside to outside.
- Remember the order: Content, Padding, Border, Margin.
- Use content for the actual element data.
- Use padding for internal spacing.
- Use borders for visible element edges.
- Use margins for external spacing.
- Do not confuse padding with margin.
- Remember that backgrounds cover content and padding.
- Remember that backgrounds do not cover margin.
- Understand the default content-box model.
- Remember that content-box applies width to content only.
- Include padding when calculating content-box width.
- Include borders when calculating content-box width.
- Include vertical padding when calculating height.
- Include top and bottom borders when calculating height.
- Keep margin separate from border box calculations.
- Include margins when calculating total layout space.
- Use box-sizing intentionally.
- Prefer border-box for predictable component sizing.
- Apply a global border-box rule in modern projects.
- Include ::before and ::after in the global box-sizing rule.
- Use consistent box-sizing across components.
- Remember that border-box includes padding inside width.
- Remember that border-box includes borders inside width.
- Remember that margin remains outside border-box.
- Use border-box with width: 100% elements.
- Use border-box with full-width form controls.
- Use border-box with responsive cards.
- Use border-box with grid and flex items when appropriate.
- Check available parent width before setting fixed dimensions.
- Prefer max-width for responsive components.
- Avoid unnecessary fixed heights for dynamic content.
- Allow content to determine height when possible.
- Use min-height when a minimum size is required.
- Check content overflow after setting dimensions.
- Understand how padding changes available content space.
- Understand how borders change available content space.
- Use consistent padding inside similar components.
- Use consistent margins between similar components.
- Use gap instead of child margins in flex and grid layouts when appropriate.
- Keep box model responsibilities clear.
- Do not use padding only to separate unrelated elements.
- Do not use margin to create internal component spacing.
- Do not use border width as a spacing tool.
- Use developer tools to inspect actual dimensions.
- Inspect computed width and height.
- Inspect padding values.
- Inspect border widths.
- Inspect margin values.
- Check the active box-sizing value.
- Check whether width comes from another CSS rule.
- Check whether max-width limits the element.
- Check whether min-width prevents shrinking.
- Check whether content causes overflow.
- Check whether child elements are wider than the parent.
- Test percentage-width elements with padding.
- Test components on narrow screens.
- Test components with long content.
- Test cards with different text lengths.
- Test buttons with different labels.
- Test form fields at full width.
- Use responsive widths instead of unnecessary fixed widths.
- Use max-width to limit very wide content.
- Use margin auto to center fixed or limited-width block elements.
- Remember that auto margins are outside the border box.
- Understand that inline elements behave differently.
- Use inline-block when an inline element needs controlled width and height.
- Understand that block elements support full box dimensions.
- Do not assume all elements have identical box behavior.
- Learn how display affects the box model.
- Keep calculations simple.
- Use border-box instead of repeatedly subtracting padding.
- Avoid magic numbers used only to repair overflow.
- Find the actual source of unexpected size.
- Use the browser box model diagram.
- Keep component width predictable.
- Keep component height flexible when possible.
- Keep internal spacing consistent.
- Keep external spacing consistent.
- Use a spacing system across the project.
- Use CSS variables for reusable spacing values.
- Use CSS variables for reusable border widths when appropriate.
- Use CSS variables for container sizes.
- Document unusual content-box requirements.
- Use content-box only when its behavior is intentionally required.
- Avoid mixing sizing models without a clear purpose.
- Remember that the visible box ends at the border edge.
- Remember that margin controls surrounding space.
- Calculate horizontal and vertical dimensions separately.
- Consider borders when exact dimensions matter.
- Consider padding when exact dimensions matter.
- Consider margins when total occupied space matters.
- Keep responsive components within their parent containers.
- Prevent horizontal scrolling caused by accidental overflow.
- Use overflow properties only when overflow is intentional.
- Do not hide overflow before understanding why it occurs.
- Build reusable components with predictable box dimensions.
- Use the box model as the foundation for every CSS layout.
- Practice calculating element sizes manually.
- Verify calculations using browser developer tools.
- Keep the box model predictable.
- Keep the box model consistent.
Flexbox and Grid become much easier when you already understand how width, height, padding, borders, margins, and box-sizing affect every element.
Frequently Asked Questions
What is the CSS Box Model?
The CSS Box Model is the system browsers use to calculate the size and spacing of HTML elements using content, padding, border, and margin.
What are the four parts of the box model?
The four parts are content, padding, border, and margin.
What is the correct box model order?
From inside to outside, the order is Content, Padding, Border, Margin.
What is the content area?
The content area contains the actual text, images, videos, or child elements.
What is the padding area?
The padding area creates space between the content and the border.
What is the border area?
The border area creates the visible edge around the content and padding.
What is the margin area?
The margin area creates transparent space outside the border.
Does the background cover padding?
Yes. The element background extends through the padding area.
Does the background cover margin?
No. Margin exists outside the element background.
What is the default box-sizing value?
The default box-sizing value is content-box.
What is content-box?
content-box applies the declared width and height only to the content area.
What is border-box?
border-box includes content, padding, and borders inside the declared width and height.
Which is better: content-box or border-box?
border-box is commonly preferred for modern layouts because it makes element sizing easier to predict.
Does margin count inside border-box?
No. Margin always remains outside the border box.
Why does padding increase element width?
With content-box, padding is added outside the declared content width.
Why does border increase element width?
With content-box, border widths are added outside the content and padding areas.
How do I calculate total width?
For content-box, add content width, left and right padding, and left and right borders.
How do I calculate total height?
For content-box, add content height, top and bottom padding, and top and bottom borders.
Should margins be included in total width?
Include margins when calculating the total horizontal space occupied in the layout, but not when calculating the border box width.
Why does width: 100% sometimes overflow?
With content-box, padding and borders are added outside the 100% content width, making the final element wider than its parent.
How can I prevent width: 100% overflow?
Using box-sizing: border-box usually keeps padding and borders inside the available width.
What is the recommended global box-sizing rule?
A common rule is *, *::before, *::after { box-sizing: border-box; }.
Are pseudo-elements part of the box model?
Yes. Generated ::before and ::after pseudo-elements create boxes and can benefit from the same box-sizing rule.
Do inline elements use the box model?
Yes, but normal inline elements handle width, height, and vertical spacing differently from block elements.
How can I give an inline element width and height?
You can use display: inline-block or another display mode that supports controlled dimensions.
Do block elements use the full box model?
Yes. Width, height, padding, borders, and margins can be directly controlled on block elements.
How can I inspect the box model?
Open browser developer tools, select the element, and inspect the box model diagram in the computed styles section.
Should I hide overflow when an element is too wide?
Not immediately. First identify whether width, padding, borders, child content, or box-sizing is causing the overflow.
Why is the box model important for responsive design?
It helps elements remain within available space and prevents unexpected overflow on smaller screens.
What comes after the CSS Box Model?
The next lesson covers CSS Width and Height, including fixed sizes, percentages, min-width, max-width, min-height, max-height, viewport units, responsive sizing, and practical examples.
Key Takeaways
- Every HTML element is treated as a rectangular box.
- The CSS Box Model controls element size and spacing.
- The four layers are content, padding, border, and margin.
- The order from inside to outside is Content, Padding, Border, Margin.
- Content contains text, images, and child elements.
- Padding creates space around content.
- Border surrounds the content and padding.
- Margin creates space outside the border.
- The background covers content and padding.
- The background does not cover margin.
- content-box is the default sizing model.
- With content-box, width applies only to content.
- Padding increases final size under content-box.
- Borders increase final size under content-box.
- border-box includes content, padding, and borders inside declared dimensions.
- Margin always remains outside the border box.
- border-box creates more predictable component sizing.
- width: 100% can overflow when combined with padding under content-box.
- border-box helps prevent percentage-width overflow.
- A global border-box rule is common in modern CSS.
- Pseudo-elements should often use the same box-sizing rule.
- Inline and block elements handle box dimensions differently.
- Developer tools can visually display every box model layer.
- Understanding the box model makes layout debugging easier.
- The box model is the foundation of CSS sizing and layout.
Summary
The CSS Box Model is the system browsers use to calculate the size, spacing, and layout of every HTML element.
Every element consists of four main layers: content, padding, border, and margin.
The content area contains the actual content, padding creates internal space, the border creates the visible edge, and margin creates external space.
With the default content-box sizing model, declared width and height apply only to the content area.
Under content-box, padding and borders are added to the declared dimensions and can make the final element larger than expected.
The border-box sizing model includes content, padding, and borders inside the declared width and height.
Many modern projects use border-box globally because it makes component sizing more predictable.
Margin remains outside the border box and affects the total space an element occupies in the surrounding layout.
Understanding the box model helps prevent overflow, incorrect sizing, broken responsive layouts, and inconsistent spacing.
Browser developer tools can display the content, padding, border, and margin values of any element, making box model problems easier to debug.
The CSS Box Model is the foundation for understanding element dimensions and is essential before learning advanced layout systems such as Flexbox and Grid.
In the next lesson, you will learn CSS Width and Height, including fixed dimensions, percentages, min-width, max-width, min-height, max-height, viewport units, responsive sizing, and practical examples.