Margin and padding

All HTML elements can be considered a box, and each box (HTML element) has the properties margin, border, padding and content. Together these properties is called the box model.

The box model

The padding area extends to the border surrounding the padding. When the content area has a background, color, or image set on it, will be extended into the padding. Text will not be extended. The padding is located inside the padding edge.

The space between the padding and the content edge can be controlled using the padding-top, padding-right, padding-bottom, padding-left and the shorthand padding CSS properties.

The border area extends the padding area to the area containing the borders. It is the area inside the border edge, and its dimensions are defined by the border-width property, or the shorthand border.

The margin area extends the border area with an empty area used to separate the element from its neighbors. It is the area inside the margin edge, and its dimensions are the margin-box width and the margin-box height.

The size of the margin area is controlled using the CSS properties margin-top, margin-right, margin-bottom, margin-left, or the shorthand margin.

Last updated