Skip to main content

What does margin do?

margin is the outer spacing around an element. It creates space outside the block, separating it from neighboring elements.

Structure:

javascript
content → padding → border → margin

Characteristic features:

  • does not affect the element's inner size
  • is not painted with the background (the background does not extend to the margin)
  • can collapse vertically with the margin of neighboring blocks (margin collapsing)
  • serves solely as outer spacing between elements

Example:

css
div { margin: 20px; }

In this case, the block will have 20px of outer spacing on all sides.

Short Answer

Interview ready
Premium

A concise answer to help you respond confidently on this topic during an interview.