Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Understanding the Box Model in a Practical Way | Section
CSS Fundamentals

bookUnderstanding the Box Model in a Practical Way

Swipe to show menu

Every element on a web page is rendered as a rectangular box. Each box consists of four parts:

  • Content: the actual text or elements;
  • Padding: space inside the element;
  • Border: surrounds the content and padding;
  • Margin: space outside the element.

Visual Structure

Margin → Border → Padding → Content. The total size of an element depends on all these parts.

Content

The visible part of the element: text, images, or other elements.

Padding (Inner Space)

Creates space between content and border.

.box {
  padding: 20px;
}

Shorthand

padding: 20px; /* all sides */
padding: 10px 20px; /* top/bottom | left/right */
padding: 10px 20px 5px; /* top | left/right | bottom */
padding: 10px 15px 20px 25px; /* top | right | bottom | left */

Margin (Outer Space)

Creates space between elements.

.box {
  margin: 20px;
}

Shorthand

margin: 20px;
margin: 10px 20px;
margin: 10px 20px 5px;
margin: 10px 15px 20px 25px;

Border

Defines the edge of the element.

.box {
  border: 4px solid darkblue;
}

Key Difference

  • Padding → inside space (affects background);
  • Margin → outside space (does NOT affect background).

Box Sizing

Controls how width and height are calculated.

box-sizing: content-box; /* default */
box-sizing: border-box;
  • content-box: padding + border added to size;
  • border-box: everything included in size.

In real projects, border-box is preferred.

Note
Note

The box model determines element size, spacing, and layout behavior. If the layout looks broken, the box model is usually the reason.

Everything was clear?

How can we improve it?

Thanks for your feedback!

Section 1. Chapter 11

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Section 1. Chapter 11
some-alt