📦 CSS Box Model

Understand margin, padding, border, and outline to master element spacing and layout

← Back to Content Next Topic: CSS Dimensions →

🎯 What is the CSS Box Model?

The CSS Box Model is a fundamental concept that describes how elements are structured and spaced on a webpage. Every HTML element is essentially a rectangular box with four main areas: content, padding, border, and margin.

Box Model Components (from inside out):

  • Content: The actual content (text, images, etc.)
  • Padding: Space between content and border (inside)
  • Border: A line around the padding and content
  • Margin: Space outside the border (between elements)

Visual Representation:

Content
⬆️ Padding
⬆️ Border
⬆️ Margin

📐 Padding

Padding creates space inside an element, between the content and the border. It's part of the element's background.

Padding Properties:

/* Individual sides */
padding-top: 10px;
padding-right: 20px;
padding-bottom: 10px;
padding-left: 20px;

/* Shorthand - all sides */
padding: 15px;

/* Shorthand - vertical horizontal */
padding: 10px 20px;  /* top/bottom  left/right */

/* Shorthand - top right/left bottom */
padding: 10px 20px 15px;

/* Shorthand - top right bottom left (clockwise) */
padding: 10px 20px 15px 5px;

Padding Shorthand Memory Trick:

  • padding: 10px; - All sides
  • padding: 10px 20px; - Vertical | Horizontal
  • padding: 10px 20px 15px; - Top | Right/Left | Bottom
  • padding: 10px 20px 15px 5px; - Top | Right | Bottom | Left (🕐 clockwise)

Example:

.box {
    padding: 20px;
    background-color: lightblue;
}

.button {
    padding: 10px 25px;  /* 10px top/bottom, 25px left/right */
}

🔲 Border

The border is a line that goes around the padding and content. You can style its width, style, and color.

Border Properties:

/* Individual properties */
border-width: 2px;
border-style: solid;
border-color: blue;

/* Shorthand */
border: 2px solid blue;

/* Individual sides */
border-top: 1px solid red;
border-right: 2px dashed green;
border-bottom: 3px dotted blue;
border-left: 4px double orange;

Border Styles:

border-style: solid;    /* Solid line */
border-style: dashed;   /* Dashed line */
border-style: dotted;   /* Dotted line */
border-style: double;   /* Double line */
border-style: groove;   /* 3D grooved */
border-style: ridge;    /* 3D ridged */
border-style: inset;    /* 3D inset */
border-style: outset;   /* 3D outset */
border-style: none;     /* No border */
border-style: hidden;   /* Hidden border */

Border Radius (Rounded Corners):

/* All corners */
border-radius: 10px;

/* Specific corners (top-left, top-right, bottom-right, bottom-left) */
border-radius: 10px 20px 30px 40px;

/* Circle */
border-radius: 50%;

/* Ellipse */
border-radius: 50px / 25px;

Common Border Patterns:

Solid Border
Dashed Border
Dotted Border
Double Border

📏 Margin

Margin creates space outside an element, between elements. Margins are transparent and don't have a background color.

Margin Properties:

/* Individual sides */
margin-top: 10px;
margin-right: 20px;
margin-bottom: 10px;
margin-left: 20px;

/* Shorthand (same as padding) */
margin: 20px;              /* All sides */
margin: 10px 20px;         /* Vertical | Horizontal */
margin: 10px 20px 15px;    /* Top | Right/Left | Bottom */
margin: 10px 20px 15px 5px; /* Top | Right | Bottom | Left */

/* Auto - centering */
margin: 0 auto;            /* Center horizontally */

/* Negative margins */
margin-top: -10px;         /* Pulls element up */

Centering Elements:

/* Center block element horizontally */
.container {
    width: 800px;
    margin: 0 auto;  /* Top/bottom: 0, Left/right: auto */
}

⚠️ Margin Collapse

When two vertical margins meet, they collapse into a single margin equal to the larger of the two. This doesn't happen with horizontal margins.

🖍️ Outline

The outline is drawn outside the border and doesn't affect the element's dimensions. Unlike borders, outlines don't take up space.

Outline Properties:

/* Individual properties */
outline-width: 2px;
outline-style: solid;
outline-color: red;

/* Shorthand */
outline: 2px solid red;

/* Outline offset */
outline-offset: 5px;  /* Space between outline and border */

/* Remove default outline (accessibility concern!) */
button:focus {
    outline: none;  /* ⚠️ Provide alternative focus indicator */
}

Border vs Outline:

  • Border: Part of box model, takes up space, can have rounded corners
  • Outline: Outside border, doesn't take space, always rectangular

📦 Box Sizing

The box-sizing property controls how the total width and height of an element is calculated.

Content-box (Default):

/* Width/height applies to content only */
.box {
    box-sizing: content-box;  /* Default */
    width: 200px;
    padding: 20px;
    border: 5px solid black;
}
/* Total width: 200 + 40 (padding) + 10 (border) = 250px */

Border-box (Recommended):

/* Width/height includes padding and border */
.box {
    box-sizing: border-box;
    width: 200px;
    padding: 20px;
    border: 5px solid black;
}
/* Total width: 200px (padding and border included) */

💡 Best Practice:

Use border-box for easier sizing calculations:

* {
    box-sizing: border-box;
}

💼 Practical Examples

Example 1: Card Component

.card {
    width: 300px;
    padding: 20px;
    margin: 20px;
    border: 1px solid #ddd;
    border-radius: 8px;
    box-shadow: 0 2px 8px rgba(0,0,0,0.1);
}

Example 2: Button with States

.button {
    padding: 12px 24px;
    margin: 10px;
    border: 2px solid #667eea;
    border-radius: 25px;
    background: white;
    color: #667eea;
    transition: all 0.3s;
}

.button:hover {
    background: #667eea;
    color: white;
}

.button:focus {
    outline: 3px solid rgba(102, 126, 234, 0.3);
    outline-offset: 2px;
}

Example 3: Spacing System

/* Consistent spacing */
.m-1 { margin: 8px; }
.m-2 { margin: 16px; }
.m-3 { margin: 24px; }
.m-4 { margin: 32px; }

.p-1 { padding: 8px; }
.p-2 { padding: 16px; }
.p-3 { padding: 24px; }
.p-4 { padding: 32px; }

🎨 Live Example

Box Model Visualization:

Content Area
This is the content
Blue area = Padding | Purple dashed = Border | Outside = Margin

Border & Outline Example:

Border (blue) & Outline (pink)

📝 Test Your Knowledge - MCQ

Q1: Which property creates space INSIDE an element?

Q2: What is the correct order of the box model from inside to outside?

Q3: Which box-sizing value includes padding and border in the width?

Q4: How do you center a block element horizontally?

Q5: What's the difference between border and outline?

💪 Practice Questions

  1. Create a box with 20px padding on all sides, a 2px solid red border, and 15px margin on top and bottom.
  2. Make a circular element using border-radius (hint: use 50%).
  3. Center a 600px wide container horizontally on the page.
  4. Create a button with 10px vertical padding, 25px horizontal padding, rounded corners, and a 2px border.
  5. Use box-sizing: border-box to create two boxes that are exactly 50% width each, side by side.

📌 Summary

  • Box Model: Content → Padding → Border → Margin (inside to outside)
  • Padding: Space inside element, part of background
  • Border: Line around padding, can be styled with width/style/color
  • Margin: Space outside element, transparent, used for spacing between elements
  • Outline: Outside border, doesn't affect layout, always rectangular
  • Shorthand Order: Top → Right → Bottom → Left (clockwise 🕐)
  • border-radius: Creates rounded corners (50% for circles)
  • box-sizing: border-box: Includes padding and border in width (recommended)
  • Centering: Use margin: 0 auto for horizontal centering
  • Margin Collapse: Vertical margins collapse, horizontal don't
  • Best Practice: Apply box-sizing: border-box to all elements
← Back to Content Next Topic: CSS Dimensions →