How do I fit an image (img) inside a div and keep the aspect ratio?

Bin Chen picture Bin Chen · Dec 9, 2010 · Viewed 502.2k times · Source

I have a 48x48 div and inside it there is an img element, I want to fit it into the div without losing any part, in the mean time the ratio is kept, is it achievable using html and css?

Answer

Michael picture Michael · Dec 9, 2010

Use max-height:100%; max-width:100%; for the image inside the div.