26

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

matino
  • 17,199
  • 8
  • 49
  • 58
L42y
  • 972
  • 1
  • 9
  • 18

1 Answers1

38

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.

Bojangles
  • 99,427
  • 50
  • 170
  • 208
  • 6
    The body's height (which is typically auto, not 100%) has no effect on how auto vertical margins are calculated. The spec simply defines auto vertical margins to *always* be zero on in-flow block boxes. So for in-flow block boxes, the answer to the question is not yes, but no - there is no difference between margin: 0 auto and margin: auto. The answer is only yes in one other specific case: absolutely positioned elements. – BoltClock Jan 18 '17 at 18:14