The CSS Box Model (box-sizing: border-box)
π’ Beginner
π Definition
The CSS Box Model is the foundational layout engine concept where every HTML element is treated as a rectangular box consisting of four concentric layers: Content, Padding, Border, and Margin. The box-sizing property determines whether padding and borders are included in or added to an elementβs specified width and height.
π Multilingual Explanation
English
The Box Model consists of content, padding (inner space), border, and margin (outer space). Always use box-sizing: border-box so padding and borders do not inflate the total element width.
Hindi
Box model mein Content, Padding (andaruni space), Border, aur Margin (bahari space) hote hain. Width ka sahi hisaab rakhne ke liye hamesha box-sizing: border-box ka use karein.
Marathi
Box model madhye Content, Padding (aatil jaga), Border, ani Margin (baheril jaga) aastat. box-sizing: border-box mule ekun rundi sthir rahte.
π€ Why Is The Box Model So Important?
In the default CSS box model (content-box), if you set an elementβs width to 200px, and then add 20px padding and a 2px border, its actual rendered width on screen becomes 200 + 20 + 20 + 2 + 2 = 244px! This causes grid columns and cards to overflow and break onto new lines.
Setting box-sizing: border-box fixes this: the element stays exactly 200px wide, automatically absorbing padding and borders inward.
π§ Simple Explanation & Picture Frame Analogy
Think of a picture framed on a wall:
- Content: The photograph itself.
- Padding: The blank matting area around the photo inside the frame.
- Border: The wooden frame surrounding the picture and matting.
- Margin: The empty wall space separating this framed picture from neighboring objects.
+------------------------------------------------+
| MARGIN (Outer transparent space) |
| +------------------------------------------+ |
| | BORDER (Boundary line) | |
| | +------------------------------------+ | |
| | | PADDING (Inner space) | | |
| | | +------------------------------+ | | |
| | | | CONTENT | | | |
| | | | (Text, images, elements) | | | |
| | | +------------------------------+ | | |
| | +------------------------------------+ | |
| +------------------------------------------+ |
+------------------------------------------------+
βοΈ content-box vs. border-box
| Box-Sizing Value | Width Calculation Formula | Rendered Width Example (width: 200px, padding: 20px, border: 2px) |
|---|---|---|
content-box (Default) |
Width + Left/Right Padding + Left/Right Border | 244px (Inflated box size breaks layouts) |
border-box (Recommended) |
Width includes content, padding, and border | 200px (Exact predictable box size) |
π The Universal Global Box-Sizing Reset
Professional web developers apply box-sizing: border-box to every element using the universal selector *:
*, *::before, *::after {
box-sizing: border-box;
margin: 0;
padding: 0;
}
π» Examples
/* Box model rules */
.card-border-box {
box-sizing: border-box;
width: 300px;
padding: 20px;
border: 5px solid #007bff;
margin: 15px;
background-color: #ffffff;
}
π HTML + CSS Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Box Model Comparison</title>
<link rel="stylesheet" href="style.css">
</head>
<body>
<div class="box content-box">
<h3>content-box (Total: 250px)</h3>
<p>Specified width 200px + 40px padding + 10px border.</p>
</div>
<div class="box border-box">
<h3>border-box (Total: 200px)</h3>
<p>Specified width 200px (padding & border absorbed inward).</p>
</div>
</body>
</html>
/* style.css */
body {
font-family: Arial, sans-serif;
background-color: #f4f6f8;
padding: 20px;
}
.box {
width: 200px;
padding: 20px;
border: 5px solid #007bff;
margin-bottom: 20px;
background-color: #ffffff;
}
/* Default browser box model */
.content-box {
box-sizing: content-box;
}
/* Recommended box model */
.border-box {
box-sizing: border-box;
}
π What You Will See
In your browser, .content-box renders visually wider (250px) than .border-box (200px), even though both have width: 200px declared! .border-box maintains exact predictable container dimensions.
π§ͺ Try It Yourself
- Inspect both boxes in DevTools (
F12). - Hover over the computed Box Model diagram at the bottom of the Styles panel to see the blue (content), green (padding), yellow (border), and orange (margin) overlays.
β οΈ Common Mistakes
- Forgetting the universal
border-boxreset: Leading to column calculation bugs where two50%width flex items break onto two lines because ofpadding. - Confusing margin and padding: Margins push other elements away outside the border; padding pushes content inward inside the border.
π‘ Real-World Usage
The universal * { box-sizing: border-box; } rule is included at the top of every modern CSS reset, Bootstrap, Tailwind, and production stylesheet worldwide.
π Related Topics
β Remember
- Box Model layers: Content β Padding β Border β Margin.
box-sizing: border-boxmakes element width calculations predictable.- Always include
*, *::before, *::after { box-sizing: border-box; }at the top of your stylesheets.
π§ Navigation
| β Previous | CSS Home | Next β |