Which CSS property sets the margin around an element?

The story behind the answer

The CSS property that sets the margin around an element is `margin`.

In the CSS box model, an element is treated as a rectangular box with content, padding, border, and margin. Margin is the outermost layer: it creates space between an element's border and nearby elements. It does not change the element's background color, because backgrounds normally extend through the content and padding areas but not the margin.

The `margin` shorthand can set all four sides at once, while `margin-top`, `margin-right`, `margin-bottom`, and `margin-left` target individual sides. Values may use lengths such as `16px`, percentages, or the keyword `auto`; for a block with a defined width, horizontal `auto` margins are commonly used to center it.

A frequent mix-up is confusing margin with padding. Padding creates space inside the border, around the content. Border is the visible edge itself, while outline is drawn outside the border and does not participate in the box model's layout dimensions. Another subtle behavior is margin collapsing, in which adjacent block margins can combine rather than simply add together.

Source: Wikipedia · fact-checked Aug. 2026

Add question to a list

Choose a list to keep this question in: