Codetainer LogoCodetainer Acad

Lessons

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:

  • 10px space inside the box before the content touches the border
  • 2px black border wraps around it
  • 20px space 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 padding for inside spacing
  • Use margin to push things away from each other
  • border is 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 ✨

CodetainerAcad