New:Embedded & IoT Bootcamp
Tutorial 21 of 44 in series

CSS Box Model

Understand how elements occupy space

Abhishek Sharma 2026-05-30 Web Development / Frontend / CSS
48%

Box Model

Every element in CSS is a rectangular box. The box model describes how space is calculated:

┌─────────────────────────────┐
│         Margin              │
│  ┌───────────────────────┐  │
│  │       Border          │  │
│  │  ┌─────────────────┐  │  │
│  │  │     Padding     │  │  │
│  │  │  ┌───────────┐  │  │  │
│  │  │  │  Content  │  │  │  │
│  │  │  └───────────┘  │  │  │
│  │  └─────────────────┘  │  │
│  └───────────────────────┘  │
└─────────────────────────────┘
Demo: Visual Box Model
HTML
<div class="box">
Content area
</div>
CSS
.box {
width: 200px;
height: 80px;
padding: 20px;
border: 4px solid #6366f1;
margin: 20px;
background: #eef2ff;
color: #1e293b;
font-weight: bold;
text-align: center;
line-height: 80px;
}
Live Output Window

Box Model Parts

Part Description
Content The actual text or child elements
Padding Space between content and border (inside)
Border The edge around padding
Margin Space between elements (outside)

Total Width Calculation

Total width = content + padding-left + padding-right
              + border-left + border-right
              + margin-left + margin-right

For the example above: 200 + 20 + 20 + 4 + 4 + 20 + 20 = 288px total width

Keep learning

Related tutorials from across the library.

All tutorials
Tutorial2026-05-30

Introduction to CSS

Learn what CSS is and why it matters

Web Development / Frontend / CSS

CSSWeb Development
Tutorial2026-05-30

History of CSS

Understand how CSS evolved

Web Development / Frontend / CSS

CSSWeb Development
Tutorial2026-05-30

Adding CSS

Three ways to add CSS to a page

Web Development / Frontend / CSS

CSSWeb Development