Auto margins don't center image in page

Web_Designer picture Web_Designer · Apr 20, 2011 · Viewed 177.7k times · Source

In this example the image is not centered. Why? My browser is Google Chrome v10 on windows 7, not IE.

<img src="/img/logo.png" style="margin:0px auto;"/>

Answer

Ross picture Ross · Apr 20, 2011

add display:block; and it'll work. Images are inline by default

To clarify, the default width for a block element is auto, which of course fills the entire available width of the containing element.

By setting the margin to auto, the browser assigns half the remaining space to margin-left and the other half to margin-right.