Ajay Gupta Ajay Gupta - 11 months ago 39
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

to center things sometimes.

doesn't work, is using margins the only or best alternative?

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


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.