CSS box model

The CSS box model module defines the height, width, margin, and padding properties, which along with border properties make up the CSS box model.

Every visible element on a webpage is a box laid out according to the visual formatting model. CSS properties define their size, position, and stacking level, with the box model properties (and others) defining the extrinsic size of each box, and the space around them.

Each box has a rectangular content area, inside which any text, images, and other content is displayed. The content may be surrounded by padding, a border, and a margin, on one or more sides. The padding is around the content, the border is around the padding, and the margin sits outside the border. The box model describes how these features — the content, padding, border, and margin — work together to create a box as displayed by CSS.

The components of the CSS box model

The CSS box model module defines physical (or "page relative") properties such as width and margin-top. Flow-relative properties such as inline-size and margin-block-start (which relate to text direction) are defined in Logical Properties and Values. The box model module is extended by the CSS box sizing module, which introduces the intrinsic size value and enables defining aspect ratios for elements that are auto-sized in at least one dimension.

Reference

Properties

Data types

Guides

Introduction to the CSS box model

Explains one of the fundamental concepts of CSS: the box model. This model defines how CSS lays out elements, including their content, padding, border, and margin areas.

Mastering margin collapsing

Sometimes, two adjacent margins are collapsed into one. This article describes the rules that govern when and why this happens, and how to control it.

Visual formatting model

Explains the visual formatting model.

Specifications

Specification
CSS Box Model Module Level 4
CSS Box Model Module Level 3

See also