you cannot fit both in width and in height in a frame to maintain aspect ratio. You can set the maximum or maximum value of the image to 100% to fit the frame. try my code. I use this method in my projects. I use wrap and inner to get the border and fill in the frame. javascript is not needed to set the image. but you should use to center the image in the frame, since the width and height of an individual image are dynamic. my sample set the maximum width of the image in the frame.
HTML:
<div class='wrap'>
<div class='inner'>
<img src='http://www.pacificrimmovie.net/wp-content/uploads/Pacific-Rim-Movie-Striker-Eureka-Australian-Jaeger.jpg' />
</div>
</div>
CSS
.wrap{
padding:10px;
border: 1px solid #777;
width: 150px;
height: 100px;
overflow:hidden;
float:left;
margin: 0px 20px 20px 0px;
}
.inner{
width: 100%;
height: 100%;
overflow:hidden;
text-align:center;
position:relative;
}
.inner img{
max-width: 100%;
position:absolute;
left:0;top:0;
}
JavaScript:
$("img").each(function(){
var top_dif= ($(this).height()-$(this).parent().height())/2;
var left_dif= ($(this).width()-$(this).parent().width())/2;
$(this).css("top",-top_dif);
$(this).css("left",-left_dif);
});
:
debug http://jsfiddle.net/7LLh14wL/3/ (: )
final http://jsfiddle.net/7LLh14wL/4/ (: )