Margin (CSS)

Margin is the transparent space outside an element's border that pushes it away from neighboring elements. It is the outermost layer of the CSS box model.

Web

You can set margins on all four sides at once or individually (margin-top, margin-right, etc.). Setting horizontal margins to auto is a common trick to center a fixed-width block.

Adjacent vertical margins can 'collapse' into one another, which sometimes surprises beginners — the larger of two touching margins wins instead of adding up.

Examples

CSS

.box {
  width: 300px;
  margin: 0 auto;
}

Output:

Centers the 300px box horizontally and removes its top and bottom margins.

Learn it in a lesson

See also

Back to the Dictionary