100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
HTML & CSS

The CSS Box Model

Understand how content, padding, border, and margin combine to determine the size and spacing of every element.

CSS FundamentalsBeginner9 min readJul 8, 2026
Analogies

Introduction

Every element on a web page is rendered as a rectangular box. The CSS box model describes how the total size of that box is calculated from four layers: content, padding, border, and margin. Mastering the box model is essential for controlling layout, spacing, and alignment.

🏏

Cricket analogy: A batsman's crease setup is like the box model: the batting stance (content), the guard mark's safety margin (padding), the crease line (border), and the space to the umpire (margin) all combine to define how much room the batsman occupies.

Syntax

css
.box {
  width: 200px;
  padding: 20px;
  border: 5px solid #333;
  margin: 10px;
  box-sizing: content-box; /* default */
}

Explanation

Content is the innermost area holding text or child elements. Padding is the transparent space between the content and the border, adding breathing room inside the box. Border wraps around the padding and content. Margin is the transparent space outside the border, separating the box from neighboring elements. By default (box-sizing: content-box), the width and height properties apply only to the content area, so padding and border are added on top, increasing the box's rendered size. Setting box-sizing: border-box changes this so width and height include padding and border, making sizing far more predictable.

🏏

Cricket analogy: Content is like the pitch strip itself, padding the extra grass buffer around it, border the boundary rope, and margin the stand seating gap; by default the rope sits outside the strip's official length, but a 'border-box' groundskeeper design keeps the whole square exactly the stated size.

Example

css
* {
  box-sizing: border-box;
}

.card {
  width: 300px;
  padding: 16px;
  border: 2px solid #ccc;
  margin: 12px auto;
}

Output

With box-sizing: border-box, the .card element's total rendered width stays exactly 300px, with the 16px padding and 2px border squeezed inside that width instead of adding to it. Without border-box, the rendered width would be 300 + 16 + 16 + 2 + 2 = 336px. The margin of 12px auto centers the box horizontally within its container and adds 12px of vertical spacing above and below.

🏏

Cricket analogy: A groundskeeper marking a fixed 300-foot boundary rope keeps it exactly 300 feet with the extra buffer grass tucked inside, rather than adding buffer on top and ending up with a 336-foot boundary; centering the pitch with equal stand gaps on both sides mirrors margin: 12px auto.

Key Takeaways

  • The box model layers are, from inside out: content, padding, border, margin.
  • content-box (default) excludes padding/border from width; border-box includes them.
  • Applying box-sizing: border-box globally is a widely used best practice for predictable layouts.
  • Margins can collapse vertically between adjacent block elements; padding never collapses.

Practice what you learned

Was this page helpful?

Topics covered

#HTMLCSS#HTMLCSSStudyNotes#WebDevelopment#TheCSSBoxModel#CSS#Box#Model#Syntax#StudyNotes#SkillVeris