📅 Week 2 - Day 3: The Box Model & Layout Fundamentals

Friday | Duration: 3 hours

🎯 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

  1. Content: The actual content (text, images)
  2. Padding: Space between content and border
  3. Border: Edge around the padding
  4. Margin: Space outside the border (between elements)
div { width: 300px; /* Content width */ padding: 20px; /* Space inside */ border: 5px solid black; /* Border */ margin: 10px; /* Space outside */ } /* Total width = 300 + 20*2 + 5*2 + 10*2 = 370px */

📏 Margin (Outer Spacing)

Margin creates space outside an element's border.

/* All sides */ div { margin: 20px; } /* Individual sides */ div { margin-top: 10px; margin-right: 20px; margin-bottom: 30px; margin-left: 40px; } /* Shorthand - 4 values (top right bottom left) */ div { margin: 10px 20px 30px 40px; } /* Shorthand - 2 values (vertical horizontal) */ div { margin: 10px 20px; /* 10px top/bottom, 20px left/right */ } /* Center an element */ div { margin: 0 auto; width: 80%; } /* Negative margins */ div { margin-top: -10px; /* Pulls element up */ }

Live Example - Margin Spacing:

Box 1 - 10px margin

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.

/* Same syntax as margin */ div { padding: 15px; /* All sides */ padding: 10px 20px; /* vertical horizontal */ padding: 5px 10px 15px 20px; /* top right bottom left */ } /* Individual sides */ 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

/* Border shorthand */ div { border: 3px solid black; } /* Individual properties */ div { border-width: 2px; border-style: solid; /* solid, dashed, dotted, double */ border-color: red; } /* Individual sides */ div { border-top: 1px solid black; border-right: 2px dashed red; border-bottom: 3px dotted blue; border-left: 4px double green; } /* Rounded corners */ div { border-radius: 10px; /* All corners */ border-radius: 10px 20px; /* top-left/bottom-right, top-right/bottom-left */ border-radius: 5px 10px 15px 20px; /* Each corner */ } /* Perfect circle (use with square element) */ div { width: 100px; height: 100px; border-radius: 50%; }

Live Example - Border Styles:

Solid
Dashed
Dotted
Double

Border-Radius Examples:

0px
15px
30px
50%

Notice: Border-radius creates rounded corners. 50% on a square element creates a perfect circle.

📐 Width and Height

div { width: 300px; height: 200px; } /* Percentage */ div { width: 80%; /* 80% of parent */ height: 50vh; /* 50% of viewport height */ } /* Max and Min */ div { max-width: 1200px; min-width: 300px; max-height: 500px; min-height: 100px; } /* Auto */ div { width: auto; /* Default, fits content */ }

box-sizing Property:

/* Default behavior */ div { box-sizing: content-box; /* width/height = content only */ } /* Better for layouts */ div { box-sizing: border-box; /* width/height includes padding and border */ } /* Apply to all elements */ * { 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; /* Removes from layout */ } div { visibility: hidden; /* Hides but keeps space */ }

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):

Box 1
Box 2
Box 3

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)

/* Reset */ * { margin: 0; padding: 0; box-sizing: border-box; } body { font-family: Arial, sans-serif; line-height: 1.6; color: #333; background-color: #f4f4f4; } /* Header */ header { background: linear-gradient(135deg, #667eea 0%, #764ba2 100%); color: white; padding: 30px; text-align: center; } header h1 { font-size: 2.5em; } /* Container for columns */ .container { max-width: 1200px; margin: 30px auto; padding: 0 20px; display: flex; gap: 30px; } /* Profile Column (Left - 30%) */ .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 (Right - 70%) */ .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 */ footer { background: #333; color: white; text-align: center; padding: 20px; margin-top: 30px; } /* Responsive: Stack on small screens */ @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> <!-- Total space = 30px (not 50px!) -->

✅ 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"