Resizeable Images (without losing quality!)
Ads by DZone
Most web browsers make resizing text an easy thing to do*, but not all web browsers will resize images along with that text**. Good web designers know this and go to great lengths to make sure their web layouts don't bork when text is resized. But did you know you can make images grow and shrink along with the text?
The trick is to give your images a unique class and set their widths with an em value.
HTML:
CSS:
img.expand { width: 10em; }
Now because you are forcing a specific width on this image, the web browser is in charge of doing the resizing, which is generally frowned upon. I tend to disagree though, I know that a web browser will never be as intelligent at retaining image quality as Photoshop, but the proof is in the pudding and I think generally web browsers do a fine job at it.
Here is the trick: make sure your starting image is larger than the default size. If the size your image is exactly the size as it is displayed on screen at the default text size, you are going to lose image quality as you size it up. But if your image is larger, it will be scaled down at the default size and have room to grow while maintaining resolution.
[VIEW EXAMPLE]
*Except, of course, IE <= v6 which will not resize font sizes set in pixels.
**Except, of course, really nice web browsers like the latest releases of Firefox and Opera.
Original article here.
(2 votes)
- Login or register to post comments
- 6098 reads
- Printer-friendly version
(Note: Opinions expressed in this article and its replies are the opinions of their respective authors and not those of DZone, Inc.)











Comments
David Walsh replied on Wed, 2008/01/30 - 9:39am
Awesome post Chris! The only thing I'd add is that people should try to keep the large image within reasonable size. You never cease to impress!
David Walsh
Web Developer & Zone Leader
http://davidwalsh.name/
rocky1138 replied on Wed, 2008/01/30 - 10:22am
The problem with this method is that, like others have pointed out, the image must be quite large for this effect to work. Take your example for example, the original source image is only maybe 80x80 in size but it's 150k because it's a scaled down large image. This method would work, but maybe only for a few of the text sizes. After all, it's probably not important to be able to scale up to size 72 font if you get my drift.
rip747 replied on Wed, 2008/01/30 - 10:23am
this is by far the most retarded thing I've ever read.
so what your saying, with your example, is that it's perfectly fine to send a 1200x18000 ~150K picture over the wire and displaying it at 200x300, instead of using photoshop to resize it to 200x300 in the first place thus reducing the file size to about ~20K and saving yourself from transferring ~130K; in conclusion making the page load faster.
Did I get that right?
David Walsh replied on Wed, 2008/01/30 - 11:37am
Keep an open mind, Rip. I work with a visually-impaired person who this would help out tremendously. He resizes the page to x-hundred percent but can't view the pictures at a decent quality.
This method most definitely has its place.
David Walsh
Web Developer & Zone Leader
http://davidwalsh.name/
Chris Coyier replied on Wed, 2008/01/30 - 6:38pm
There are some things that will be sacrificed here.
Yes, the page will be "bloated", in that the images will be larger sized than they need to be for the default size.
Yes, there is a chance at some sizes that the web browser doesn't do a very good job at resizing and it looks slightly off.
Yes, several very modern browsers do this automatically so it's already sort of out-dated.
It's just a technique. It's good to know. It's interesting. It could be potentially good for use in ultra-accessible sites.
David Walsh replied on Thu, 2008/01/31 - 2:33pm
David Walsh
Web Developer & Zone Leader
http://davidwalsh.name/