Ajay Gupta Ajay Gupta - 4 months ago 9
HTML Question

when will margin: auto; work and not work?

Is this an issue in regards to block and inline elements?

It can be frustrating having to use

margin-left
to center things sometimes.

When
margin:auto
doesn't work, is using margins the only or best alternative?

Please Any one Explain with an Example (or with Jsfiddle)

Answer

margin:auto wont work when you have not set a width or when you have a float. To center a block element you will have to give it a width then only you can use margin: 0 auto. This is for block elements. For inline elements you will have to use text-align:center.