CSS Box Model

The CSS box model is a way of describing the layout and sizing of HTML elements on a web page. It consists of four parts: the content area, padding, border, and margin.

Illustration of the box model, showing (from inside to out) content, padding, border, and margin

  • Content area: This is the innermost part of the box, and it contains the actual content of the element, such as text, images, or other HTML elements.

  • Padding: This is the space between the content area and the border. It can be used to create space between the content and the border or to add a background color to the element.

  • Border: This is the line that surrounds the content area and padding. It can be used to create a visible boundary around the element or to separate it from other elements on the page.

  • Margin: This is the space between the border and the next element on the page. It can be used to create space between elements or to center an element on the page.

The size of the box itself is calculated like this:

Width

width + padding-left + padding-right + border-left + border-right

Height

height + padding-top + padding-bottom + border-top + border-bottom