Class 11 - The Box Model
CSS Foundations
The Box Model 📦
CSCI 3403 - Web Programming
Week 6, Class 11
Today: The most fundamental CSS concept! 🎯
Today's Learning Objectives
- 🎯 Understand that everything is a box
- 🎯 Master the box model components
- 🎯 Control element sizing with padding, border, margin
- 🎯 Learn box-sizing: border-box
- 🎯 Understand margin collapsing
- 🎯 Center elements horizontally
Quick Recap
Our CSS Journey So Far:
- ✅ Introduction to CSS
- ✅ Selectors and properties
- ✅ The Cascade and specificity
- ✅ Browser developer tools
Today's Focus:
Layout fundamentals - How elements take up space!
The #1 CSS Concept 🤯
Everything on a webpage is a
RECTANGULAR BOX
Yes, even circles!
Let's See the Boxes! 👀
* {
outline: 2px solid red;
}
Try It Now!
Open DevTools on any website:
- Right-click → Inspect
- In Styles panel, click the + button
- Add the CSS above
- Watch the magic happen!
Every element is contained in a box!
The Box Model 📦
Every box has four parts!
Box Model Components
From Inside to Outside:
- Content: The actual content (text, images)
- Padding: Space between content and border
- Border: The edge of the box
- Margin: Space outside the border
Memory Trick: "Content Pads the Border from the Margin" (CPBM)
1. Content 📝
.box {
width: 300px;
height: 150px;
}
- The innermost part of the box
- Where your text and images live
- Size controlled by width and height
2. Padding 🛡️
.box {
padding: 20px;
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;
}
- Creates space INSIDE the border
- Background color extends into padding
- Makes content easier to read
- Can't be negative
Padding Shorthand ⚡
4 Values
padding: 10px 20px 30px 40px;
2 Values
padding: 10px 20px;
Remember: Think of a clock! Start at 12 and go clockwise.
3. Border 🔲
.box {
border: 5px solid #764ba2;
border-width: 5px;
border-style: solid;
border-color: #764ba2;
}
Border Styles:
- solid, dashed, dotted, double, none
- Adds to the total size of the element
- Can have different styles per side
4. Margin 🌊
.box {
margin: 20px;
margin: 0 auto;
}
- Space between elements
- Can be negative (pulls elements closer)
- Transparent - shows parent's background
auto
value for centering
Live Demo Time! 🎬
Let's Build a Box Together
We'll create a card component with:
- Content area with text
- Padding for breathing room
- A nice border
- Margin to separate from other elements
Open VS Code and follow along!
The Box-Sizing Problem 😤
You set width: 200px, but your box is 260px wide!
.box {
width: 200px;
padding: 20px;
border: 10px solid black;
}
Actual width:
200px (content) + 40px (padding) + 20px (border) = 260px
This is the default content-box
behavior!
The Border-Box Solution! ✨
.box {
box-sizing: border-box;
width: 200px;
padding: 20px;
border: 10px solid black;
}
With border-box:
Total width = 200px (includes padding and border!)
Pro Tip: Width now means the TOTAL width, not just content!
Set Border-Box for Everything! 🌍
*, *::before, *::after {
box-sizing: border-box;
}
Why use border-box?
- More intuitive sizing
- Easier to calculate layouts
- Industry standard practice
- Prevents unexpected overflow
Always include this in your projects from now on!
Content-Box vs Border-Box
content-box (default)
width: 200px
Actual: 260px wide
200 + 40 (padding) + 20 (border)
border-box
width: 200px
Actual: 200px wide
Padding and border included!
Margin Collapsing 🤝
When vertical margins meet, they collapse!
.box1 {
margin-bottom: 30px;
}
.box2 {
margin-top: 20px;
}
Space between boxes = 30px (not 50px!)
The larger margin wins!
Note: Only happens with vertical margins, not horizontal!
Centering Elements 🎯
The Classic Technique: margin: auto
.centered-box {
width: 500px;
margin: 0 auto;
}
Centered!
Requirements: Element must have a defined width!
Quick Practice 💪
10-Minute Challenge
Create a card component with:
- Width of 300px
- 20px padding all around
- 3px solid border (your choice of color)
- Centered on the page
- Use border-box sizing
.card {
}
DevTools Box Model Viewer 🔍
Let's Explore!
- Right-click any element → Inspect
- Look for the "Box Model" diagram
- Hover over different sections
- See the measurements for each part
Colors in DevTools:
- Blue = Content
- Green = Padding
- Yellow/Orange = Border
- Orange/Pink = Margin
When to Use What? 🤔
Use Padding
- Space inside a button
- Text breathing room
- Card/container spacing
- Background needs to extend
Use Margin
- Space between elements
- Centering blocks
- Creating gaps in layouts
- Pushing elements apart
Pro Tips 💡
- 🎯 Negative margins can overlap elements
- 🎯
margin: auto
only works horizontally by default
- 🎯 Padding percentages are based on width (even vertical!)
- 🎯
outline
doesn't affect layout (unlike border)
- 🎯 Use DevTools to experiment with values
Debug Tip: Can't figure out spacing? Add temporary background colors!
Avoid These Mistakes! ⚠️
- ❌ Forgetting
box-sizing: border-box
- ❌ Using padding when you need margin (or vice versa)
- ❌ Not setting width when using
margin: auto
- ❌ Fighting margin collapse instead of working with it
- ❌ Using only pixels - explore %, em, rem
- ❌ Ignoring the DevTools box model viewer
Homework Assignment 📚
Due: Before Next Class
Your Tasks:
- Complete the MDN Box Model Challenge:
MDN Box Model Tasks
- Add your completed Box Model challenge to your one-page website
- Use:
- All four box model properties
- border-box sizing
- Center at least one element
Push to GitHub and submit link on D2L!
Extra Practice Resources 📖
Videos to Watch:
- Learn CSS Box Model In 8 Minutes
- box-sizing: border-box (EASY!)
Articles to Read:
- MDN's lesson on the box model
- CSS Tricks: Margins
Interactive:
- Scrimba Box Model course
- CSS Diner (for selector practice)
Knowledge Check ✅
Can you answer these?
- What's the order from inside to outside?
- What does box-sizing: border-box do?
- Margin or padding for space between elements?
- Margin or padding for space inside an element?
- How do you center an element horizontally?
- What is margin collapsing?
If unsure about any, review the slides or ask me!
Next Class Preview
CSS Foundations: Block and Inline
- Display property
- Block vs inline vs inline-block
- How elements flow on the page
- More layout fundamentals
Building on today's box model knowledge! 🏗️
Today's Key Takeaways 🎯
- ✅ Everything is a rectangular box
- ✅ Content → Padding → Border → Margin
- ✅ Always use box-sizing: border-box
- ✅ Margins collapse vertically
- ✅ margin: auto centers elements
- ✅ DevTools is your best friend
Master the box model = Master CSS layout!
Excellent Work! 🌟
You Now Understand CSS Fundamentals!
The box model is THE foundation
Practice with DevTools
Experiment and break things!
See you Thursday! 👋
Don't forget your homework!
1 / 30