Is there a way to resize (scale down) images proportionally using ONLY CSS?
I'm doing the JavaScript way, but just trying to see if this is possible with CSS.
Notice that width:50%
will resize it to 50% of the available space for the image, while max-width:50%
will resize the image to 50% of its natural size. This is very important to take into account when using this rules for mobile web design, so for mobile web design max-width
should always be used.
UPDATE: This was probably an old Firefox bug, that seems to have been fixed by now.
Revisited in 2015:
<img src="http://imageurl" style="width: auto; height: auto;max-width: 120px;max-height: 100px">
I've revisited it as all common browsers now have working auto suggested by Cherif above, so that works even better as you don't need to know if image is wider than taller.
older version: If you are limited by box of 120x100 for example you can do
<img src="http://image.url" height="100" style="max-width: 120px">
image_tag("/icons/icon.gif", height: '32', width: '32')
I need to set height: '50px', width: '50px' to image tag and this code works from first try note I tried all the above code but no luck so this one works and here is my code from my _nav.html.erb:<%= image_tag("#{current_user.image}", height: '50px', width: '50px') %>
max-width
value to adjust which size image is requested. – Lilith River