CSS: Resizing Large Images to Fit in Containers (also work in MSIE!)

March 28, 2005 by  
Filed under CSS, Scripts and Coding

In my post about CSS: Resizing Large Images to Fit in Containers, I hadn’t noticed that the CSS2 code, “max-width” didn’t work in MS Internet Explorer. So I looked around and found this nice piece of code from Ozone Asylum Forums (based on this article).

So to make an image resize in MSIE, use this CSS Code.

<html>
<head>

<style type=”text/css”>

.resizeImage {

/* for Firefox, Opera and others: */

max-width: 250px;

}

</style>

<!–[if gte IE 5]>

<style type=”text/css”>

.resizeImage {

/* For Internet Explorer: */

width: expression(Math.min(parseInt(this.offsetWidth), 250 ) + “px”);

}

</style>
<![endif]–>
</head>

<body>

<img src=”myimage.jpg” class=”resizeImage” alt=”my image” />
</body>
</html>

So that’s how you get the ‘max-width’ CSS property to work in Internet Explorer? …You don’t. ;) Just use width and the Javascript trick! Thank you for pointing this out to me how max-width didn’t work in MSIE, John Adams.

Resources: Ozone Asylum Forums, svendtofte.com.

Related Posts Plugin for WordPress, Blogger...
  • http://danwu.net/blog Daniel

    Hi,

    I found your blog when I was searching for a solution to the max-width issue in wordpress. I’m a newbie at this whole css thing :-P I have a few questions.

    1. Do I append this code to the style.css file? Anywhere in the file?

    2. Do I need to include the “p img { max-width: 100%; }” as well as the code you mentioned above, or does the above trick work take care of the resizing in firefox as well?

    3. Is this code application to other themes as well, or just the MX4 theme?

    4. what’s the myimage.jpg reference?

    Thanks!

  • http://danwu.net/blog Daniel

    Hi,

    I found your blog when I was searching for a solution to the max-width issue in wordpress. I’m a newbie at this whole css thing :-P I have a few questions.

    1. Do I append this code to the style.css file? Anywhere in the file?

    2. Do I need to include the “p img { max-width: 100%; }” as well as the code you mentioned above, or does the above trick work take care of the resizing in firefox as well?

    3. Is this code application to other themes as well, or just the MX4 theme?

    4. what’s the myimage.jpg reference?

    Thanks!

  • http://daynah.net daynah

    1. You can put half of it your style.css file, except it’s probably easier to put it in the header of each theme since it needs the “if this is MSIE” code. Put it anywhere between the <head> tags.

    2. If you are using it for wordpress themes, instead of using “.resizeImage” use, “p img” instead.

    3. You can use it in any theme.

    4. myimage.jpg is the image you post in your post.

    I hope that helps. You might want to read up on more CSS articles before attempting to edit your files though. This is a good place to start: http://www.w3schools.com/css/default.asp

  • http://daynah.net daynah

    1. You can put half of it your style.css file, except it’s probably easier to put it in the header of each theme since it needs the “if this is MSIE” code. Put it anywhere between the <head> tags.

    2. If you are using it for wordpress themes, instead of using “.resizeImage” use, “p img” instead.

    3. You can use it in any theme.

    4. myimage.jpg is the image you post in your post.

    I hope that helps. You might want to read up on more CSS articles before attempting to edit your files though. This is a good place to start: http://www.w3schools.com/css/default.asp