DEV Community

Sujan Khadka
Sujan Khadka

Posted on

1 1

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!

👋 Kindness is contagious

Explore a sea of insights with this enlightening post, highly esteemed within the nurturing DEV Community. Coders of all stripes are invited to participate and contribute to our shared knowledge.

Expressing gratitude with a simple "thank you" can make a big impact. Leave your thanks in the comments!

On DEV, exchanging ideas smooths our way and strengthens our community bonds. Found this useful? A quick note of thanks to the author can mean a lot.

Okay