Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Box Model Calculation
📖 Scenario: You are creating a simple webpage layout. You want to understand how the CSS box model works by calculating the total width of a box including its content, padding, border, and margin.
🎯 Goal: Build a CSS style for a <div> element with specific content width, padding, border, and margin. Calculate the total width of the box as it will appear on the page.
📋 What You'll Learn
Create a CSS rule for a .box class with a content width of 200px
Add padding of 20px on all sides
Add a border of 5px solid black
Add a margin of 10px on all sides
Calculate the total width of the box including content, padding, border, and margin
💡 Why This Matters
🌍 Real World
Understanding the CSS box model helps you design web pages with precise control over layout and spacing.
💼 Career
Web developers and designers must know how to calculate box sizes to create responsive and visually appealing websites.
Progress0 / 4 steps
1
Create the base CSS for the box content width
Write a CSS rule for the class .box that sets the width property to 200px.
CSS
Hint
Use the width property inside the .box selector.
2
Add padding to the box
Add padding: 20px; inside the existing .box CSS rule to add space inside the box around the content.
CSS
Hint
Padding adds space inside the box around the content.
3
Add border to the box
Add a border of 5px solid black inside the .box CSS rule to create a visible border around the padding.
CSS
Hint
The border goes outside the padding but inside the margin.
4
Add margin to the box
Add a margin of 10px on all sides inside the .box CSS rule to create space outside the border.
CSS
Hint
Margin creates space outside the border.
Practice
(1/5)
1. Which parts of the CSS box model add space inside the element's border?
easy
A. Margin and padding
B. Padding and content
C. Margin and border
D. Border and margin
Solution
Step 1: Understand box model parts
The box model has content, padding, border, and margin. Padding is inside the border, margin is outside.
Step 2: Identify inside spaces
Padding adds space inside the border, content is inside padding. Margin is outside the border.
Final Answer:
Padding and content -> Option B
Quick Check:
Inside space = padding + content [OK]
Hint: Padding is inside border; margin is outside [OK]
Common Mistakes:
Confusing margin as inside space
Thinking border adds inside space
Mixing padding with margin
2. Given this CSS, what is the total width of the element?
Hint: Add vertical padding and border twice to height [OK]
Common Mistakes:
Using horizontal padding values for vertical calculation
Forgetting to double padding and border
Ignoring border size
4. This CSS code is meant to create a box 300px wide including padding and border. What is wrong?
width: 300px; padding: 20px; border: 10px solid;
medium
A. The box will be wider than 300px
B. The box will be exactly 300px wide
C. Padding and border are ignored in width
D. The box will be narrower than 300px
Solution
Step 1: Calculate total width with padding and border
Content width is 300px, but padding adds 20px*2=40px and border adds 10px*2=20px, total 360px.
Step 2: Understand box-sizing default
By default, width sets content box only, so padding and border add outside, making box wider than 300px.
Final Answer:
The box will be wider than 300px -> Option A
Quick Check:
Default box-sizing excludes padding and border [OK]
Hint: Width excludes padding and border unless box-sizing set [OK]
Common Mistakes:
Assuming width includes padding and border by default
Ignoring box-sizing property
Not doubling padding and border
5. You want a box with total width 400px including padding and border. Padding is 15px and border is 5px. What should the CSS width be if box-sizing: content-box;?
hard
A. 430px
B. 400px
C. 350px
D. 360px
Solution
Step 1: Calculate total padding and border width
Padding left+right = 15px * 2 = 30px. Border left+right = 5px * 2 = 10px. Total extra = 40px.
Step 2: Subtract padding and border from total width