#container { width: 48px; height: 48px; } #container img { width: 100%; } -----------------------------------------------JS------------------------------------------- <div id="container"> <img src="something.jpg" alt="" /> </div> <script type="text/javascript"> (function() { var img = document.getElementById('container').firstChild; img.onload = function() { if(img.height > img.width) { img.height = '100%'; img.width = 'auto'; } }; }()); </script>
Preview:
downloadDownload PNG
downloadDownload JPEG
downloadDownload SVG
Tip: You can change the style, width & colours of the snippet with the inspect tool before clicking Download!
Click to optimize width for Twitter