varatis varatis - 4 months ago 19
CSS Question

Contain an image within a div?

I want to contain an image into a 250 x 250 div, forcing the image to resize but not stretch. How can I do this? By the way, the image will usually be bigger than the div itself, which is why resizing comes into the picture.

<div id = "container">
<img src = "whatever" />
</div>

#container { width:250px; height:250px; border:1px solid #000; }


Here's a jsfiddle which someone can edit:

http://jsfiddle.net/rV77g/

Answer

Use max width and max height. It will keep the aspect ratio

#container img 
{
 max-width: 250px;
 max-height: 250px;
}

http://jsfiddle.net/rV77g/