Scalable Images with CSS

If you use ems to define an image’s height property, the image will be scaled relative to an implicit 1em text height.

For example, here is code to display the same image twice — first normally, then scaled to exactly five times the normal text height:

<p>Normal: <img src="http://anoved.net/images/cssize/flower.jpg" />
 Scalable: <img src="http://anoved.net/images/cssize/flower.jpg" style="height:5em;" /></p>

Here is how these images appear:

Normal:
Scalable:

Try using your browser’s controls to increase or decrease the displayed text size. The first instance of the image will remain the same as the neighboring text changes size, but the second instance of the image will grow or shrink with the text, preserving the proportions of the original design.

I use this trick to fit my impish logo in the title bar at the top of the page regardless of the visitor’s text size preferences. It works surprisingly well in contemporary browsers, but of course it looks dumb without CSS. The scaling does appear to be off by one pixel at alternating sizes in Safari.

Worth noting is that Opera scales the entire page layout instead of just textual elements, which strikes me as clever and correct.

Comments and technical corrections welcome.

Posted on Wednesday, January 16th, 2008. Tags: .

4 Responses to “Scalable Images with CSS”

Posted by krishna on Thursday, June 26th, 2008 at 12:23 PM. Reply.

Helped to me to assure that there is way that image is scalable.

But in whcih browsers it works?

If my style is like background: #fc0 url(‘image.jpg’), how will it work??

Posted by Jim on Thursday, June 26th, 2008 at 1:11 PM. Reply.

I’ve tested this with Firefox 2, Safari 2/3 (Webkit), and IE 6/7. Firefox 3 and Opera appear to scale all images anyway.

I don’t know how to achieve similar results with CSS background images.

Posted by Mike Leibel on Tuesday, November 16th, 2010 at 4:55 PM. Reply.

I’m not sure I understand. I did this with the banner on top of my page (Added style=”height:5em;”) to the img, but it doesn’t scale. Do I have to add something to the css? Great article if only I could get it to work it would make me very happy indeed.

Posted by Jim on Sunday, November 21st, 2010 at 6:48 PM. Reply.

Hi MIke, I think that current versions of most web browsers do something like this automatically now (images change size along with text when you zoom in or out), so I’m not sure it remains a helpful trick. Of course that doesn’t help users of other browsers – so, what browser are you using?

Leave a Reply

Comments are held for moderation before being published. You can format your comment with the following tags: <a href="http://anoved.net/">, <blockquote>, <code>, <em>, and <strong>. Don't forget the closing tag (</a>, etc.).