When we want page's main container align center, we usually use "margin: 0 auto;"
, but when I use "margin:auto;"
, behaviour are the same in some browser I have (Google Chrome, Firefox).
So in margin: 0 auto, the top/bottom margin is 0, and the left/right margin is auto, Where auto means that the left and right margin are automatically set by the browser based on the container, to make element centered.
The auto Value You can set the margin property to auto to horizontally center the element within its container. The element will then take up the specified width, and the remaining space will be split equally between the left and right margins.
First things first, each of the elements above are blocks and have set margin: 0 auto, but it does not work since blocks have width equal to 100% by default (the first example). The block covers the whole page and therefore cannot be centered.
margin-right: auto; The auto keyword will give the right side a share of the remaining space. When combined with margin-left: auto , it will center the element, if a fixed width is defined. First item.
Yes.
margin: 0 auto;
Sets the element's left and right margins to auto
, and the top and bottom margins to 0
.
margin: auto;
Sets all the margins to auto
. You are probably getting the same behaviour due to your <body>
being 100% height, hence the vertical auto
margins have no effect.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With