CSS Box Model – Understanding Spacing

Let’s move on to Lesson 6 – an essential concept in CSS that every frontend developer must understand:


πŸ“˜ LESSON 6: CSS Box Model – Understanding Spacing

βœ… Learning Goals:

  • Understand how every HTML element is treated as a box
  • Learn the four parts of the CSS box model
  • Visualize spacing: content, padding, border, and margin
  • Practice adjusting spacing and layout

🧾 Lesson Content (Text for Website)


πŸ“¦ What is the CSS Box Model?

In CSS, every element is a box.
This box has layers:

+-----------------------------+
|         margin              |
|  +-----------------------+  |
|  |      border           |  |
|  |  +-----------------+  |  |
|  |  |   padding        |  |  |
|  |  |  +-----------+   |  |  |
|  |  |  |  content   |   |  |  |
|  |  |  +-----------+   |  |  |
|  |  +-----------------+  |  |
|  +-----------------------+  |
+-----------------------------+

🧱 The Four Layers:

  1. Content – The actual text or image
  2. Padding – Space between content and border
  3. Border – The outline around the element
  4. Margin – Space outside the element (between it and others)

πŸ§ͺ Example: Box Model in Action

<!DOCTYPE html>
<html>
<head>
  <style>
    .box {
      width: 200px;
      padding: 20px;
      border: 5px solid #ff6f00;
      margin: 30px;
      background-color: #ffe0b2;
    }
  </style>
</head>
<body>
  <div class="box">This is a box with padding, border, and margin.</div>
</body>
</html>

πŸ’‘ Save this as lesson6.html and test it in your browser.


πŸ“ Box Model Math

Total Width =
width + left/right padding + left/right border + left/right margin

Total Height =
height + top/bottom padding + top/bottom border + top/bottom margin


πŸ” DevTools Tip

Open your page in Chrome or Firefox
Right-click β†’ Inspect β†’ Click the box element β†’
Look in Computed β†’ You’ll see a box diagram showing spacing


πŸ§ͺ Practice Task

βœ… Create a card with:

  • 250px width
  • 20px padding
  • 10px solid border
  • 40px margin
  • Add background color

πŸ“Œ Quick Summary

  • Every element in CSS is a box
  • The box has content β†’ padding β†’ border β†’ margin
  • Understanding spacing is crucial for clean layouts
  • Use DevTools to debug spacing visually

πŸ”— Next Lesson:

πŸ‘‰ Lesson 7: CSS Positioning & Display Explained

Telegram Join Our Telegram Channel

One Response

  1. Pingback: ZRTech Solutions

Leave a Reply

Your email address will not be published. Required fields are marked *

Telegram Join Our Telegram Channel

Most Viewed

Monthly Best Selling Templates

Check the latest products added to the marketplace. Fresh designs with the finest HTML5 CSS3 coding.