Lesson 7: Spacing and The Box Model
📦 The Box Model
Every HTML element is a box. Whether it’s a button, paragraph, or div, they all follow the same structure. Understanding the box model is key to controlling layout and spacing like a ninja 🥷.
The CSS box model consists of:
- Content: The actual text, image, or element inside the box
- Padding: Space inside the box, around the content
- Border: A line wrapping around the padding (optional)
- Margin: Space outside the box, separating it from other elements
🔍 Visualizing the Box Model
div {
padding: 10px;
border: 2px solid black;
margin: 20px;
}
This means:
10pxspace inside the box before the content touches the border2pxblack border wraps around it20pxspace between this box and whatever’s outside it
🛠️ Dev Tools Tip
Right-click any element in your browser, inspect it, and you'll see a live box model in the dev tools. You can interactively change margin, padding, and border right there. Super useful for debugging spacing issues!
✅ Quick Recap
- Every element has
content → padding → border → margin - Use
paddingfor inside spacing - Use
marginto push things away from each other borderis optional and can be styled (dashed, solid, etc.)
Up next: we dive into CSS layout techniques like display, position, and flexbox — it’s where the magic begins ✨