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:
- Content β The actual text or image
- Padding β Space between content and border
- Border β The outline around the element
- 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
20
One Response