Here is the script update version that uses the CSS3 background-size
property: http://jsfiddle.net/EyW99/2/
What you need to add to your style, mainly background-size: contain;
to scale the image to the maximum possible image size, which is still suitable for your element, or background-size:X% X%;
or background-size:Xpx Xpx;
if you want to control the size yourself.
Note
As background size is a property of CSS3, it has limited browser support.
IE9 +, Firefox 4+, Opera, Chrome, and Safari 5+.
Translator armor
This solution may not be as good as the new CSS property, but to support cross-browser, you can change the div
to img
, which allows the browser to resize the image.
source share