Why does margin:auto takes items to the center

Why does margin:auto takes items to the center
0

As shown in the applied visual design section margin:auto took the colored box to the center of the page. How does that happen?

Hi @im_saurabhnegi, welcome to the FCC forum!

when margin is set auto the remaining space is filled up with a margin. In this case the left and right margin get the same margin. (see yellow parts in image)

Something you will often see is: margin: 0 auto;
Explicitly meaning the top and bottom margin (first parameter) are 0 and the left and right margin (second parameter) are auto.
You can see more examples of margin use cases here:
https://developer.mozilla.org/en-US/docs/Web/CSS/margin

image

1 Like

Got it. Thanks for the help. @michaelsndr

1 Like