📘 MODULE 4 – CSS Layout & Design
📦 Lesson 4.1 – CSS Box Model
🎨 Understanding How HTML Elements Occupy Space on a Webpage
🎯 Learning Objectives
After completing this lesson, students will be able to:
✅ Understand the CSS Box Model
✅ Identify Content Area
✅ Understand Padding
✅ Understand Borders
✅ Understand Margins
✅ Calculate Element Size
✅ Create Proper Layout Spacing
✅ Build Professional Webpage Designs
📖 Introduction
Every HTML element on a webpage is treated as a:
📦 Box
Whether it is:
- Heading
- Paragraph
- Image
- Button
- Form
- Div
Everything is a box.
Imagine a cardboard box.
+----------------------+
| |
| CONTENT |
| |
+----------------------+
A webpage element works in a similar way.
🤔 What is the CSS Box Model?
The CSS Box Model describes how every HTML element occupies space on a webpage.
Every element consists of:
Margin
↓
Border
↓
Padding
↓
Content
🌟 Structure of Box Model
+----------------------------+
| Margin |
| |
| +----------------------+ |
| | Border | |
| | | |
| | +---------------+ | |
| | | Padding | | |
| | | | | |
| | | Content | | |
| | | | | |
| | +---------------+ | |
| | | |
| +----------------------+ |
| |
+----------------------------+
🎯 Four Parts of Box Model
1️⃣ Content
2️⃣ Padding
3️⃣ Border
4️⃣ Margin
1️⃣ Content Area
Content is the actual information inside an element.
Examples:
<h1>Welcome Students</h1>
Content:
Welcome Students
Example:
<p>Learning CSS Box Model</p>
Content:
Learning CSS Box Model
🌟 Content Example
HTML
<div>
Hello Students
</div>
Content:
Hello Students
2️⃣ Padding
Padding is the space between:
Content
and
Border
Without Padding
+------------------+
|Hello Students |
+------------------+
Text touches border.
With Padding
+----------------------+
| |
| Hello Students |
| |
+----------------------+
Looks better.
🎯 Padding Property
Syntax:
padding:value;
Example
padding:20px;
Meaning:
20 pixels space inside all sides.
Example Program
CSS
div{
padding:20px;
border:2px solid black;
}
Result
Space appears between content and border.
3️⃣ Border
Border surrounds the content and padding.
Example:
border:2px solid blue;
Meaning:
2px = Thickness
solid = Style
blue = Color
Example
div{
border:3px solid green;
}
Result
Green border appears around element.
4️⃣ Margin
Margin is the space outside the border.
Without Margin
+--------++--------+
| Box 1 || Box 2 |
+--------++--------+
Boxes touch each other.
With Margin
+--------+ +--------+
| Box 1 | | Box 2 |
+--------+ +--------+
Spacing appears.
🎯 Margin Property
Syntax
margin:value;
Example
margin:20px;
Meaning
20 pixels space outside element.
Example Program
div{
margin:20px;
}
🌟 Complete Box Model Example
HTML
<div>
Learning CSS Box Model
</div>
CSS
div{
width:300px;
padding:20px;
border:3px solid blue;
margin:30px;
}
Understanding This Code
Width:
width:300px;
Content Width = 300px
Padding:
padding:20px;
20px on all sides.
Border:
border:3px solid blue;
Blue border.
Margin:
margin:30px;
30px outside spacing.
🎯 Total Width Calculation
Suppose:
width:300px;
padding:20px;
border:3px solid blue;
Calculation:
Content = 300px
Left Padding = 20px
Right Padding = 20px
Left Border = 3px
Right Border = 3px
----------------
Total Width
= 346px
Why is Box Model Important?
Without understanding Box Model:
❌ Layout breaks
❌ Elements overlap
❌ Spacing issues occur
With Box Model:
✅ Proper spacing
✅ Professional layouts
✅ Better website design
🌟 Individual Padding Values
Top
padding-top:20px;
Right
padding-right:20px;
Bottom
padding-bottom:20px;
Left
padding-left:20px;
Example
padding-top:30px;
padding-left:10px;
🌟 Individual Margin Values
Top
margin-top:20px;
Right
margin-right:20px;
Bottom
margin-bottom:20px;
Left
margin-left:20px;
💻 Complete Practical Program
HTML
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div>
Welcome to NextGen Dev Hub
</div>
</body>
</html>
CSS
div{
width:300px;
background-color:lightblue;
padding:20px;
border:3px solid blue;
margin:40px;
font-size:20px;
}
🎯 Expected Output
A blue box containing:
Welcome to NextGen Dev Hub
with:
✅ Padding
✅ Border
✅ Margin
✅ Background Color
🏫 Real World Example
Login Form
Uses:
padding:20px;
for internal spacing.
Navigation Bar
Uses:
margin-bottom:20px;
for spacing.
Profile Cards
Use:
padding
border
margin
for professional appearance.
⚠️ Common Beginner Mistakes
Mistake 1
Confusing Margin and Padding
Wrong Thinking:
Both are same
Correct:
Padding = Inside Space
Margin = Outside Space
Mistake 2
Too Much Padding
Wrong:
padding:100px;
Creates oversized elements.
Mistake 3
No Margin
Elements stick together.
Always maintain spacing.
🌟 Best Practices
✅ Use padding for internal spacing.
✅ Use margin for external spacing.
✅ Keep spacing consistent.
✅ Use borders carefully.
✅ Understand box dimensions.
🏆 Mini Project – Information Box
Create:
Student Name
College Name
Branch
Inside a box.
Apply:
✅ Width
✅ Padding
✅ Border
✅ Margin
✅ Background Color
📝 Practical Activity
Create two boxes.
Box 1:
HTML
Box 2:
CSS
Apply:
✅ Width = 200px
✅ Padding = 20px
✅ Margin = 20px
✅ Border = 2px solid blue
📋 Assignment
Assignment 4.1
- What is CSS Box Model?
- Name the four parts of Box Model.
- What is Padding?
- What is Margin?
- What is Border?
- Calculate total width of an element with width 300px, padding 20px, and border 2px.
- Create a box using padding, margin, and border.
🧠 Quiz
Q1. Which is the innermost part of Box Model?
A. Margin
B. Border
C. Content
D. Padding
✅ Answer: C
Q2. Which property creates space inside an element?
A. Margin
B. Padding
C. Border
D. Width
✅ Answer: B
Q3. Which property creates space outside an element?
A. Margin
B. Padding
C. Border
D. Height
✅ Answer: A
Q4. Which property creates a visible outline?
A. Margin
B. Padding
C. Border
D. Width
✅ Answer: C
Q5. What comes immediately outside the content?
A. Border
B. Margin
C. Padding
D. Width
✅ Answer: C
📌 Lesson Summary
✅ Every HTML element is treated as a box.
✅ CSS Box Model consists of Content, Padding, Border, and Margin.
✅ Padding creates space inside the border.
✅ Margin creates space outside the border.
✅ Borders create visible outlines.
✅ Understanding Box Model is essential for professional web design.