📖 Scenario: You are designing a simple webpage section that contains a box with text inside. You want to understand how the box model works and how to control the space around and inside the box.
🎯 Goal: Build a basic box using HTML and CSS that demonstrates the box model properties: content, padding, border, and margin. You will create the box, add spacing inside and outside it, and see how these properties affect the layout.
📋 What You'll Learn
Create a box element with a fixed width and height
Add padding inside the box to create space between the content and the border
Add a visible border around the box
Add margin outside the box to separate it from other elements
💡 Why This Matters
🌍 Real World
Understanding the box model is essential for designing and laying out web pages with proper spacing and alignment.
💼 Career
Web designers and front-end developers use the box model daily to create visually appealing and user-friendly websites.
Progress0 / 4 steps
1
Create the box element
Create a <div> element with the class box inside the <body> tag.
No-Code
Hint
Use a <div> tag and add class="box" inside the <body>.
2
Add basic box styles
Inside the <style> tag, create a CSS rule for the class .box that sets width to 200px, height to 100px, and background-color to #add8e6 (light blue).
No-Code
Hint
Use the CSS properties width, height, and background-color inside the .box selector.
3
Add padding and border
In the .box CSS rule, add padding of 20px and a border of 3px solid black.
No-Code
Hint
Use padding: 20px; and border: 3px solid black; inside the .box CSS rule.
4
Add margin outside the box
In the .box CSS rule, add margin of 30px to create space outside the box.
No-Code
Hint
Add margin: 30px; inside the .box CSS rule to create space outside the box.
Practice
(1/5)
1. Which part of the box model adds space inside the element but outside the content?
easy
A. Margin
B. Content
C. Border
D. Padding
Solution
Step 1: Understand the box model layers
The box model has content in the center, then padding, border, and margin outside it.
Step 2: Identify the layer adding space inside the element
Padding adds space inside the element, between content and border.
Final Answer:
Padding -> Option D
Quick Check:
Padding = space inside element [OK]
Hint: Padding is inside space around content, margin is outside [OK]
Common Mistakes:
Confusing margin with padding
Thinking border adds inner space
Mixing content with padding
2. Which CSS property correctly sets the space outside an element's border?
easy
A. margin
B. content-spacing
C. border-width
D. padding
Solution
Step 1: Recall box model spacing properties
Margin controls the space outside the border of an element.
Step 2: Match property to outside space
Only margin sets space outside the border; padding is inside, border-width sets border thickness.
Final Answer:
margin -> Option A
Quick Check:
Margin = outside space [OK]
Hint: Margin controls outside space, padding controls inside space [OK]
Common Mistakes:
Using padding instead of margin
Confusing border-width with margin
Using non-existent property content-spacing
3. If an element has content width 100px, padding 10px, border 5px, and margin 20px, what is its total width including margin?
medium
A. 150px
B. 170px
C. 190px
D. 210px
Solution
Step 1: Calculate width including padding and border
Total width without margin = content width + 2*padding + 2*border = 100 + 20 + 10 = 130px.
Step 2: Add margin on both sides
Total width including margin = 130 + 2*20 = 130 + 40 = 170px.
Step 3: Recalculate carefully
Correction: Padding 10px each side = 20px total, border 5px each side = 10px total, margin 20px each side = 40px total. So total width = 100 + 20 + 10 + 40 = 170px.
Hint: Default box-sizing excludes padding from width [OK]
Common Mistakes:
Assuming width includes padding
Confusing margin with padding
Ignoring box-sizing property
5. You want two boxes side by side inside a 500px container. Each box has width: 220px;, padding: 10px;, border: 5px;, and margin: 10px;. Which CSS change ensures they fit exactly without wrapping?
hard
A. Remove padding from boxes
B. Increase container width to 600px
C. Set box-sizing: border-box; on the boxes
D. Set margin to 0 on boxes
Solution
Step 1: Calculate total width per box without box-sizing