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
.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
* {
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
1. In the default box model (content-box), what does the `width` property control?
2. Which box model layer is transparent and does NOT share the element's background color?
3. What does `box-sizing: border-box` change?
4. What CSS phenomenon can cause two vertically stacked block elements' margins to combine into a single margin instead of adding together?
Was this page helpful?
You May Also Like
CSS Positioning
How the position property (static, relative, absolute, fixed, sticky) controls where elements sit in the document flow.
Backgrounds and Borders in CSS
Learn how to style element backgrounds and borders using CSS properties like background-image, border-radius, and box-shadow.
CSS Units for Layout (px, %, rem, em, vh/vw)
How absolute and relative CSS length units differ and when to use px, %, rem, em, and viewport units in layouts.
Related Reading
Related Study Notes in Web Development
Browse all study notesWebSockets Study Notes
Web Development · 30 topics
Web DevelopmentWebAssembly Study Notes
WebAssembly · 30 topics
Web DevelopmentgRPC Study Notes
Protocol Buffers · 30 topics
Web DevelopmentSpring Boot Study Notes
Java · 30 topics
Web DevelopmentFlask Study Notes
Python · 30 topics
Web DevelopmentDjango Study Notes
Python · 30 topics