The CSS Box Model: Content, Padding, Border, Margin, box-sizing, border-radius, box-shadow, Margin Collapse and Overflow

HTML5 and CSS3 55 min min read Updated: Nov 17, 2026 Beginner
The CSS Box Model: Content, Padding, Border, Margin, box-sizing, border-radius, box-shadow, Margin Collapse and Overflow
Beginner Topic 1 of 3

Every Element Is a Box

The browser draws every element as a rectangular box with four layers. From inside to outside:

LayerPropertyMeaning
Contentwidth, heightText, image or children
PaddingpaddingSpace inside the border, around the content
BorderborderA line around the padding
MarginmarginSpace outside the border, between boxes
css .card { width: 300px; padding: 20px; /* all four sides */ border: 5px solid #0b5fff; margin: 10px 0; /* top/bottom 10px, left/right 0 */ } .box { padding: 10px 20px; } /* vertical horizontal */ .box2 { padding: 5px 10px 15px 20px; } /* top right bottom left (clockwise) */

box-sizing: The Most Useful Reset

By default width sets only the content width. Padding and border are added on top. That makes sizes hard to predict.

css .a { box-sizing: content-box; width: 300px; padding: 20px; border: 5px solid #000; } .b { box-sizing: border-box; width: 300px; padding: 20px; border: 5px solid #000; }
Measured in Chrome

content-box total width: 350 border-box total width: 300

With border-box, the 300px includes padding and border. Most developers start every stylesheet with:

css *, *::before, *::after { box-sizing: border-box; }

Rounded Corners and Shadows

css .card { border-radius: 12px; /* rounded corners */ box-shadow: 0 4px 12px rgba(0, 0, 0, .15); /* x y blur color */ } .avatar { width: 80px; height: 80px; border-radius: 50%; } /* circle */ .btn:focus-visible { outline: 3px solid #8ab4ff; outline-offset: 2px; }

box-shadow values are: x offset, y offset, blur, optional spread, then color. Add inset for an inner shadow. Unlike a border, a shadow and an outline do not change the box size.

Margin Collapse

When the vertical margins of two blocks touch, they do not add. The larger one wins.

css .m1 { margin-bottom: 30px; } .m2 { margin-top: 20px; }
Measured in Chrome

gap between the two boxes: 30

Not 50 The space is 30px, not 30 + 20. Margin collapse happens only for vertical margins of block elements. Flex and grid items do not collapse.

Centering and Auto Margins

css .container { max-width: 60rem; margin-inline: auto; padding-inline: 1rem; }

A block with a set width and margin-inline: auto is centered horizontally.

Overflow

css .scroll-box { height: 8rem; overflow: auto; } .clip { overflow: hidden; } .dots { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
ValueResult
visibleContent spills out (default)
hiddenExtra content is cut
autoScrollbars only when needed
scrollAlways show scrollbars
See the box In DevTools, hover an element and look at the colored box model diagram: blue content, green padding, yellow border, orange margin.

Practice

  1. Make a card with padding, border, radius and a soft shadow
  2. Compare content-box and border-box with a 300px width
  3. Center a page container using max-width and auto margins

Conclusion

Content, padding, border and margin make every box. Use border-box so widths stay predictable, and remember vertical margins can collapse.

Quick Summary: Four layers; box-sizing; radius and shadow; margin collapse; centering; overflow.

Get Newsletter

Subscibe to our newsletter and we will notify you about the newest updates on Edugators