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.
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.
I believe this is the easiest way to do it, also possible using through the inline
style
attribute within the<img>
tag.or
Notice that
width:50%
will resize it to 50% of the available space for the image, whilemax-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 designmax-width
should always be used.If it's a background image, use background-size:contain.
Example css:
Revisited in 2015:
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
You can use object-fit property:
This will fit image, without changing the proportionally.
works for me. It scales down larger images to fit in the box, but leaves smaller images their original size.