🧱 Day 8 – CSS Box Model, Margin, Padding, Border, Display & Visibility
📥 Download Notes + Example Code (PDF):
👉 Click here to download Day 8 Notes
📚 Introduction
Understanding the CSS Box Model is one of the most important steps in becoming a front-end web developer. If your layouts look broken, misaligned, or messy, the solution often lies in how well you use margins, padding, borders, and display properties.
In this blog post, we’ll cover:
-
CSS Box Model
-
Margin, Padding, Border
-
Display and Visibility properties
With code examples and best practices.
🧱 1. What Is the CSS Box Model?
The CSS Box Model treats every HTML element like a rectangular box with 4 layers:
-
Content – Actual text/image inside the box
-
Padding – Space between content and border
-
Border – The line around the element
-
Margin – Space outside the element (between it and others)
🖼 Think of it like a photo frame:
-
The photo is content
-
The space inside the frame is padding
-
The wooden edge is the border
-
The gap between two frames on the wall is the margin
✍️ Example – Box Model CSS
🔹 This example adds spacing inside, outside, and around a div box.
🎯 2. Margin, Padding, and Border in Detail
| Property | Purpose | Example |
|---|---|---|
margin |
Creates space outside the element | margin: 10px; |
padding |
Creates space inside the element | padding: 15px; |
border |
Adds a visible line around the element | border: 2px solid #000; |
📌 You can set each side separately:
👁️ 3. CSS Display & Visibility
CSS display and visibility control how an element appears on the page.
display values:
-
block– Occupies full width (like<div>,<h1>) -
inline– Flows within text (like<span>,<a>) -
inline-block– Allows width/height, but still flows inline -
none– Completely removes the element from the layout
visibility values:
-
visible– Default; element is shown -
hidden– Element is invisible but space remains
🧪 Example:
🛠️ Real-World Example
This kind of styling is perfect for product listings, testimonials, or feature boxes on websites.
✅ Best Practices
-
Use margin for external spacing, padding for internal spacing
-
Always define borders in pixels or rem for consistency
-
Combine display and visibility wisely for dynamic UI
-
Use browser inspect tools to debug spacing/layout issues