🎯 Learning Objectives
- Understand the CSS Box Model
- Master margin, padding, and borders
- Learn display properties
- Create basic layouts using width and height
- Build a 2-column layout
📦 The CSS Box Model
Every HTML element is a rectangular box. The Box Model describes how space is calculated around elements.
MARGIN (transparent, outside spacing)
BORDER (visible edge)
PADDING (space inside border)
CONTENT
Box Model Components
- Content: The actual content (text, images)
- Padding: Space between content and border
- Border: Edge around the padding
- Margin: Space outside the border (between elements)
div {
width: 300px;
padding: 20px;
border: 5px solid black;
margin: 10px;
}
📏 Margin (Outer Spacing)
Margin creates space outside an element's border.
div {
margin: 20px;
}
div {
margin-top: 10px;
margin-right: 20px;
margin-bottom: 30px;
margin-left: 40px;
}
div {
margin: 10px 20px 30px 40px;
}
div {
margin: 10px 20px;
}
div {
margin: 0 auto;
width: 80%;
}
div {
margin-top: -10px;
}
Live Example - Margin Spacing:
Box 2 - 30px margin (notice the larger space)
Box 3 - margin: 0 auto (centered with 70% width)
Notice: Each box has different margin values creating different spacing around them.
🎨 Padding (Inner Spacing)
Padding creates space inside an element's border.
div {
padding: 15px;
padding: 10px 20px;
padding: 5px 10px 15px 20px;
}
div {
padding-top: 10px;
padding-right: 15px;
padding-bottom: 10px;
padding-left: 15px;
}
This box has 20px padding. Notice the space between this text and the border.
Different Padding Values:
10px padding on all sides
20px top/bottom, 40px left/right padding
Different padding: 5px top, 15px right, 25px bottom, 35px left
🔲 Borders
div {
border: 3px solid black;
}
div {
border-width: 2px;
border-style: solid;
border-color: red;
}
div {
border-top: 1px solid black;
border-right: 2px dashed red;
border-bottom: 3px dotted blue;
border-left: 4px double green;
}
div {
border-radius: 10px;
border-radius: 10px 20px;
border-radius: 5px 10px 15px 20px;
}
div {
width: 100px;
height: 100px;
border-radius: 50%;
}
Live Example - Border Styles:
Solid
Dashed
Dotted
Double
Border-Radius Examples:
Notice: Border-radius creates rounded corners. 50% on a square element creates a perfect circle.
📐 Width and Height
div {
width: 300px;
height: 200px;
}
div {
width: 80%;
height: 50vh;
}
div {
max-width: 1200px;
min-width: 300px;
max-height: 500px;
min-height: 100px;
}
div {
width: auto;
}
box-sizing Property:
div {
box-sizing: content-box;
}
div {
box-sizing: border-box;
}
* {
box-sizing: border-box;
}
Live Example - Box-Sizing Comparison:
Both boxes below have width: 200px, padding: 20px, and border: 5px solid
content-box (default)
200px content
Total width: 200 + 40 + 10 = 250px
border-box (recommended)
200px total
Total width: 200px (includes padding & border)
Notice: border-box makes it easier to predict element sizes since width includes padding and border.
🎭 Display Property
Block Elements
div, h1, p {
display: block;
}
- Takes full width available
- Starts on new line
- Respects width, height, margin, padding
Inline Elements
span, a, strong {
display: inline;
}
- Takes only necessary width
- Does not start on new line
- Ignores width and height
- Vertical margin/padding has no effect
Inline-Block
div {
display: inline-block;
}
- Flows like inline but respects width/height
- Perfect for horizontal menus
Hide Element
div {
display: none;
}
div {
visibility: hidden;
}
Live Example - Display Property:
Block elements (take full width):
Block 1
Block 2
Inline elements (flow horizontally, ignore width/height):
Inline 1
Inline 2
Inline 3
Inline-block elements (flow horizontally but respect width/height):
Notice: Block elements stack vertically, inline elements flow horizontally, and inline-block combines both behaviors.
💻 Main Activity: Build 2-Column Layout
Task: Create profile-bio.html
Build a page with profile on left and bio on right.
HTML Structure
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Profile & Bio</title>
<link rel="stylesheet" href="css/layout.css">
</head>
<body>
<header>
<h1>My Profile & Biography</h1>
</header>
<div class="container">
<div class="profile-column">
<img src="https://via.placeholder.com/200" alt="Profile">
<h2>Ahmad Tijjani Garba</h2>
<p>Web Developer Student</p>
<h3>Contact Info</h3>
<ul>
<li>Email: ahmad@example.com</li>
<li>Phone: 0801234567</li>
<li>Location: Kano, Nigeria</li>
</ul>
<h3>Skills</h3>
<ul>
<li>HTML</li>
<li>CSS</li>
<li>Git/GitHub</li>
</ul>
</div>
<div class="bio-column">
<h2>About Me</h2>
<p>
I am a passionate web development student at Next-Step Technology.
I love building beautiful and functional websites.
</p>
<h3>Education</h3>
<p>
<strong>Bayero University Kano</strong><br>
Department of Information Technology<br>
Expected Graduation: 2026
</p>
<h3>Experience</h3>
<p>
<strong>Web Development Intern</strong> - Next-Step Technology<br>
December 2025 - Present<br>
Learning HTML, CSS, and JavaScript to build modern web applications.
</p>
<h3>Projects</h3>
<ul>
<li>Personal Portfolio Website</li>
<li>Student Registration Form</li>
<li>News Article Page</li>
</ul>
<h3>Goals</h3>
<p>
My goal is to become a full-stack web developer and build applications
that solve real-world problems in my community.
</p>
</div>
</div>
<footer>
<p>© 2025 Ahmad Tijjani Garba. All rights reserved.</p>
</footer>
</body>
</html>
CSS for 2-Column Layout (layout.css)
* {
margin: 0;
padding: 0;
box-sizing: border-box;
}
body {
font-family: Arial, sans-serif;
line-height: 1.6;
color: #333;
background-color: #f4f4f4;
}
header {
background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
color: white;
padding: 30px;
text-align: center;
}
header h1 {
font-size: 2.5em;
}
.container {
max-width: 1200px;
margin: 30px auto;
padding: 0 20px;
display: flex;
gap: 30px;
}
.profile-column {
width: 30%;
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.profile-column img {
width: 100%;
border-radius: 50%;
border: 5px solid #667eea;
margin-bottom: 20px;
}
.profile-column h2 {
color: #667eea;
margin-bottom: 10px;
text-align: center;
}
.profile-column > p {
text-align: center;
color: #666;
margin-bottom: 20px;
}
.profile-column h3 {
color: #555;
margin-top: 25px;
margin-bottom: 10px;
border-bottom: 2px solid #667eea;
padding-bottom: 5px;
}
.profile-column ul {
list-style: none;
}
.profile-column li {
padding: 8px 0;
border-bottom: 1px solid #eee;
}
.bio-column {
width: 70%;
background: white;
padding: 30px;
border-radius: 10px;
box-shadow: 0 2px 10px rgba(0,0,0,0.1);
}
.bio-column h2 {
color: #667eea;
font-size: 2em;
margin-bottom: 20px;
}
.bio-column h3 {
color: #555;
margin-top: 25px;
margin-bottom: 15px;
border-bottom: 2px solid #667eea;
padding-bottom: 5px;
}
.bio-column p {
margin-bottom: 15px;
text-align: justify;
}
.bio-column ul {
margin-left: 30px;
margin-bottom: 15px;
}
.bio-column li {
margin-bottom: 10px;
}
footer {
background: #333;
color: white;
text-align: center;
padding: 20px;
margin-top: 30px;
}
@media (max-width: 768px) {
.container {
flex-direction: column;
}
.profile-column,
.bio-column {
width: 100%;
}
}
✏️ Practice Exercises
Exercise 1: Box Model Playground
Create a page with different colored boxes to practice:
- Try different margin and padding values
- Experiment with borders and border-radius
- Use Chrome DevTools to see the box model
Exercise 2: Create a Card Layout
Build a page with 3 product cards side by side:
- Use inline-block or float
- Add padding, borders, and box-shadow
- Include image, title, description, and price
Exercise 3: Margin Collapse
Experiment with vertical margin collapse:
<div style="margin-bottom: 30px;">Box 1</div>
<div style="margin-top: 20px;">Box 2</div>
✅ Today's Deliverable
- profile-bio.html with 2-column layout
- layout.css with proper box model styling
- Screenshot showing the layout
- Committed to GitHub with message: "Add 2-column layout"