Box Model & Box Sizing in CSS

From the CSS cheat sheet ยท Box Model ยท verified Jul 2026

Box Model & Box Sizing

Content, padding, border, margin, and how box-sizing changes the calculation

css
.box {
  width: 300px;
  padding: 20px;
  border: 2px solid #ccc;
  margin: 10px;
}

/* border-box: width INCLUDES padding + border */
*, *::before, *::after {
  box-sizing: border-box;
}
๐Ÿ’ก Always use box-sizing: border-box globally โ€” it makes width actually mean total width
โšก margin-inline: auto centers a block element horizontally (works in all modern browsers)
๐Ÿ“Œ Vertical margins collapse โ€” two 20px margins between elements becomes 20px, not 40px
๐ŸŸข aspect-ratio: 16/9 maintains proportions without the old padding-top hack
box-modelpaddingmarginsizing

More CSS tasks

Back to the full CSS cheat sheet