Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there any difference between "margin: 0 auto;" and "margin: auto;"

Tags:

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).

like image 945
L42y Avatar asked Nov 29 '11 14:11

L42y


People also ask

What does margin auto 0 mean?

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.

What does margin auto mean?

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.

Why margin 0 Auto does not work?

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.

What is margin-right auto?

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.


1 Answers

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.

like image 88
Bojangles Avatar answered Sep 19 '22 01:09

Bojangles