How do I center an <img> in a containing <div>?
The usual margin-left: auto; margin-right: auto; isn’t working.
The image can be larger than the container, and is scaled either horizontally or vertically by the max-width and max-height.
A jsfiddle with the below code: http://jsfiddle.net/9ShGj/1/
HTML:
<div class="container">
<img src="http://www.yensa.com/fat/fat-dude10.jpg" />
</div>
<div class="container">
<img src="http://oi43.tinypic.com/bje2wn.jpg" />
</div>
CSS:
.container {
width: 200px;
height: 200px;
background: rgb(120, 120, 120);
border: 1px solid red;
margin: 5px;
}
.container img {
max-width: 200px;
max-height: 200px;
margin-left: auto;
margin-right: auto;
vertical-align: middle;
}
You can write like this:
CSS
HTML
Check this http://jsfiddle.net/dP89y/