SyntaxStudy
Sign Up
CSS Beginner 1 min read

The CSS Box Model

The CSS Box Model

Every HTML element is a rectangular box. The box model describes the layers that make up that box:

  1. Content — The actual text or image
  2. Padding — Space between content and border (inside)
  3. Border — A line around the padding
  4. Margin — Space outside the border (between elements)

box-sizing

By default (content-box), padding and border are added to the width. With box-sizing: border-box, the width includes padding and border — almost always the better choice.

Example
/* Reset to border-box globally (recommended) */
*, *::before, *::after {
  box-sizing: border-box;
}

.card {
  width: 300px;
  padding: 24px;         /* space inside */
  border: 2px solid #ddd;
  margin: 16px auto;     /* center + spacing outside */
  background: white;
}