23

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
  • 16,442
  • 7
  • 46
  • 56
L42y
  • 941
  • 8
  • 18

1 Answers1

36

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
  • 96,145
  • 48
  • 166
  • 203
  • 2
    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