I want to place an image inside a div 200x200px and also the image must be centered horizontally and vertically.
I start with SO question "Align image in center and middle within div" and try out the first answer:
#over img {
display: block;
margin-left: auto;
margin-right: auto;
}
It doesn't work... see jsfiddle
Then, I continue with the second answer:
<div style="display:table-cell; vertical-align:middle; text-align:center">
<img src="img.png">
</div>
It doesn't work... see jsfiddle
Finally, I try this:
display: table-caption;
vertical-align: middle;
margin-left: auto;
margin-right: auto;
and it seems to work! see jsfiddle
Can somebody explain to me why the first two approaches fail to keep the outer div to 200x200px
? I see very similar code snippets and can't understand what really makes the difference. Thank you in advance...