To resize an image to fit the height of a container without distortion, jQuery can be used:
Simply apply overflow:hidden
to the div surrounding the image, set the image's height to 100%
, and use jQuery to center the image with position:absolute
.
(See example here: http://jsfiddle.net/kK4ZV/)
HTML:
<div class="container">
<img src="http://placehold.it/350x150">
</div>
<div class="container2">
<img src="http://placehold.it/350x150" class="imageid">
</div>
<div class="container2">
<img src="http://placehold.it/600x300" class="imageid">
</div>
CSS:
.container {
border:1px solid red;
width: 280px;
height:310px;
}
.container2 {
border:1px solid blue;
width: 280px;
height:310px;
overflow: hidden;
position:relative;
}
.container2 img {
height:100%;
}
.js-fix {
position:absolute;
top:0;
left:50%;
}
jQuery:
$(".imageid").each(function(){
var hWide = ($(this).width())/2; //half the image's width
hWide = '-' + hWide + 'px';
$(this).addClass("js-fix").css({
"margin-left" : hWide,
});
});
(Source for jQuery code: here)