Questions And Answers

More Tutorials

CSS Margin

The CSS margin properties are used to create space around elements, outside of any defined borders. With CSS, you have full control over the margins. There are properties for setting the margin for each side of an element (top, right, bottom, and left).

Parameter Details
0 set margin to none
auto used for centering, by evenly setting values on each side
units (e.g. px) see parameter section in Units for a list of valid units
inherit inherit margin value from parent element
initial restore to initial value

Margin Collapsing

When two margins are touching each other vertically, they are collapsed. When two margins touch horizontally, they do not collapse.

margin: 10px;

This is a div tag with margin 10px

They will be 10px apart since vertical margins collapse over one and other. (The spacing will not be the sum of two margins.)

Overlapping with different sizes

margin: 10px;
margin: 15px;

This is a div tag with margin 10px

These elements will be spaced 15px apart vertically. The margins overlap as much as they can, but the larger margin will determine the spacing between the elements./p>


In this page (written and validated by ) you learned about CSS margin . What's Next? If you are interested in completing CSS tutorial, your next topic will be learning about: CSS margin on given side.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.