views:

139

answers:

4

I've created a <div> with width:10em and height:5em.
Now I would like to display an <img> (GIF image) in this <div> with the exact same size. But apparently it doesn't "know" the em unit. It shows the picture in 10x5 pixels instead.

How can I display this <img> using the "em" unit?

+3  A: 

Here is a good tutorial on this subject.

Waggers
+4  A: 

Use this code:

<html>
  <body>
    <p>This is 10em x 5em</p>
    <div style="width:10em; height:5em;">
    <img style="width:10em; height:5em;" src="logo.png" />
    </div>
  </body>
</html>

It works in Firefox 3.5, Internet Explorer 8 and Opera 10.0 Beta on Windows XP.

bjelli
No point in repeating the exact same width/height values. You might as well either a) Do away with the enclosing div or b) change the image's values to 100%.
Bobby Jack
+1  A: 

You can style img with CSS:

img.MyImage {
    width: 10em;
    height: 5em;
}

<img class="MyImage" src="image.gif" />
Myra
A: 

OK, thanks a lot :o)

No need for such 'thank you' answers - to accept an answer just click on the V sign next to the answer you've used. This will also triple your reputation.
Kobi