Skip to main content
CSS for Beginners
CHAPTER 04 Beginner

CSS Borders, Margins, Padding, and Box Model

Updated: May 10, 2026
5 min read

# CSS Box Model: Borders, Margins, and Padding

1. Introduction

Every single element on a web page is a rectangular box. The CSS Box Model dictates how these boxes are sized and how they interact with each other. Understanding the box model is the most important step in learning web layout.

2. Learning Objectives

  • Understand the core Box Model (Content, Padding, Border, Margin).
  • Apply margins and padding effectively.
  • Use the box-sizing property to prevent layout breaks.

3. Detailed Explanations

The Box Model Breakdown

  1. 1. Content: The actual text or image.
  1. 2. Padding: The space *inside* the border, around the content.
  1. 3. Border: The wall surrounding the padding and content.
  1. 4. Margin: The invisible space *outside* the border, pushing other elements away.

Padding and Margins

You can set them on all sides, or individually.
css
12345
.box {
    padding: 20px; /* 20px on all 4 sides */
    margin-top: 10px; /* Only top margin */
    margin: 10px 20px; /* Top/Bottom 10px, Left/Right 20px */
}

Borders and Border Radius

Borders add visual outlines. border-radius rounds the corners.
css
12345678910
.card {
    border: 2px solid #333; /* thickness style color */
    border-radius: 10px; /* rounded corners */
}

.circle {
    width: 100px;
    height: 100px;
    border-radius: 50%; /* creates a perfect circle */
}

The Magic of box-sizing: border-box

By default, if you set width to 100px, and add 20px padding, the box becomes 140px wide! This breaks layouts. border-box fixes this.
css
12345678910
/* Best Practice: Add this to EVERY project! */
* {
    box-sizing: border-box;
}

.fixed-box {
    width: 100%; /* Will stay 100% wide, even with padding! */
    padding: 20px;
    border: 5px solid red;
}

4. Mini Project: Profile Card

Let's build a standard UI component using the Box Model.
html
12345
<div class="profile-card">
    <img src="avatar.jpg" class="avatar" alt="Avatar">
    <h2>Jane Doe</h2>
    <p>Frontend Developer</p>
</div>
css
123456789101112131415161718
.profile-card {
    width: 300px;
    margin: 40px auto; /* Centers the card horizontally */
    padding: 30px; /* Breathing room inside */
    border: 1px solid #e2e8f0;
    border-radius: 16px;
    background-color: white;
    text-align: center;
    box-shadow: 0 10px 15px rgba(0,0,0,0.1); /* Outer shadow */
}

.avatar {
    width: 120px;
    height: 120px;
    border-radius: 50%;
    margin-bottom: 20px; /* Pushes text down */
    border: 4px solid #indigo;
}

5. MCQs

Q1: Which property affects the space OUTSIDE the border of an element? A) Padding B) Margin C) Spacing D) Outline *Answer: B*

6. Summary

Mastering the Box Model means you control space. Margins push elements away, padding adds internal breathing room, and borders frame your content. Always use box-sizing: border-box!

Finish this Chapter

Save your progress on your learning path and prepare for coding interview challenges.

Discussion

Join the discussion

Log in or create a free account to participate.

Sort: ·