DEV Community

Sujan Khadka
Sujan Khadka

Posted on

What does auto do in margin:0 auto?

Top comments (2)

Collapse
 
richardjpleguen profile image
Richard JP Le Guen • Edited

Looking at the MDN article for CSS margin it says:

When two values are specified, the first margin applies to the top and bottom, the second to the left and right.

Citation: developer.mozilla.org/en-US/docs/W...

So margin: 0 auto is the same as the following 4 properties:

margin-top: 0;
margin-right: auto;
margin-bottom: 0;
margin-left: auto;

When margin-left and margin-right are both auto that will usually center-align a Block Level Element, like a <div>.

Collapse
 
sujankh22371674 profile image
Sujan Khadka

Thanks!