Alex Rivera | Logout

CSS how do you stretch to fit and maintain the aspect ratio?

Asked 2011-09-12T22:43:54.067
19

I have the following css:

.mod.left {
background-image: url("http://www.myimage.jpg");
display: block;
height: 160px;
overflow: hidden;
width: 175px;
}

That corresponds to this HTML:

<div class="mod left"></div>

It results in this mess:

enter image description here

if I use the css3 background-size: 175px 160px; The aspect ratio is really messed up resulting in a mess like this:

enter image description here

Is there a way to stretch an image to fit a div? But in a way in which the aspect ratio is maintained? I want an auto crop.

Edit
Report

1 Answer

5

Does it need to be a background image?

img{
width:300px;
height:auto;
}


<img src="your-photo.png">
answered 2011-09-12T22:53:04.173

Your Answer