CSS: how to vertically and horizontally align an image?
<div class="blog-thumbnail">
<img src="img.jpg" alt="img">
</div>
.blog-thumbnail {
height: 200px;
margin: 0 auto;
position: relative;
}
.blog-thumbnail img {
position: absolute;
bottom: 0;
left: 0;
right: 0;
top: 0;
margin: auto;
max-width: 100%;
max-height: 100%;
}
I know this is and old question already answered but now you can use flex
<div class="container">
<img src="http://placehold.it/200x200" />
</div>
CSS
.container {
display: flex;
justify-content: center;
align-items: center;
border: 1px solid;
width: 50vw;
height: 50vw;
}
.container img
{
max-width:100%;
max-height:100%;
}
Fiddle Demo
you can also customize the size of your container, some browser may not support flex you can check it here caniuse
Try this - http://jsfiddle.net/zYx4g/ This will work on image of any size and in all browsers.
.image_container {
width: 300px;
height: 300px;
background: #eee;
text-align: center;
line-height: 300px;
}
.image_container img {
vertical-align: middle;
}
Move your left top corner of the image to the middle and reset it half the image's width and height:
.image_container img{
position: relative;
top: 50%;
left: 50%;
margin-top: -50px;
margin-left: -50px;
}