Lesson 3.4: CSS Box Model (Margin, Border, Padding, Content)
The CSS Box Model is one of the most fundamental concepts in web design. Every HTML element on a webpage is treated as a rectangular box, and the box model defines how its size, spacing, and content are calculated.
📦 Structure of the Box Model
Each box has 4 layers (from inside to outside):
-
Content – The actual text, image, or other data inside the element.
-
Padding – The space between the content and the border.
-
Border – The line surrounding the padding and content.
-
Margin – The space outside the border that separates one element from another.
🔹 Example of Box Model Layers
Total width calculation:
🔹 Properties in Detail
1. Content
-
The main area where text or images appear.
-
Defined by
widthandheight.
2. Padding
-
Space inside the border around the content.
-
Transparent (takes background color).
3. Border
-
A line around the element.
-
Has style, width, color.
4. Margin
-
Space outside the border, separating elements.
-
Does not take background color (transparent).
🔹 Box-Sizing Property
By default, CSS calculates width without including padding & border.
To make calculations easier, use:
👉 This makes width include content + padding + border, preventing layout issues.
📌 Quick Visualization
✅ Key Takeaways:
-
The box model defines how elements take space.
-
Always consider content, padding, border, margin when designing layouts.
-
Use
box-sizing: border-box;for easier and consistent layout management.
