Max-Width and Faking it for IE

The max-width CSS property is a great property to use when you don't want an element to get larger than a certain size. It does exactly what it says it does. Using it with images can be very handy. Of course it's best to size an image accordingly, sometimes it's good to have a backup for making sure you don't use an image larger than what the space allows for. Note that the max-width property does NOTHING for the file size if used with an image, so it's not a good idea to resize 1024x1000 3mb .jpg's down to a proper display size with max-width as you'll still be eating up bandwidth.

To use max-width the syntax is

selector {max-width:value;}

So for example, for Blogger users if you want to use max-width to keep your posted images in check you could add something like this to the CSS portion of your template

.post-body img {max-width:400px;}

Most Blogger templates have a class defined of 'post-body' for the blog posts (but your mileage may vary, it may be a different class or id that you'll need) and a statment like this will affect all images that are in the class of 'post-body'. Of course you could set the value of max-width to whatever you want. Max-width values can also be in percentages, so for those with fluid designs (that expand or collapse depending on the screen resolution) you could also do

.post-body img {max-width:95%;}

With a setting like that, the images will never exceed 95 percent of the available space (usually best to not go to 100% to leave a little wriggle room for padding and such ).

Of course there's a catch.. Internet Explorer up to version 6 doesn't support the max-width property (we can hope for IE 7), and with it's other problems and market share it's the browser that needs it the most. But there is a workaround.

IE has the ability to perform javascript like (or is it javascript? I dunno) functions on it's CSS so you can fake max-width with it. Here's the syntax.

selector {width: expression(this.width > value ? value: true);}

I'm not gonna pretend I know everything going on there, suffice to say this is how you might use it with the max-width property example we used before.

.post-body img {
  max-width:400px;
  width: expression(this.width > 400 ? 400: true);
}

The two values in the IE statement would be the same value as your max-width statement in pixels. Unfortunately, as far as I've been able to find out you can't use this method with percentages in IE(least I haven't found a way to ). Browsers other than IE will safely ignore the width:expression statement and just implement the max-width.

I probably should mention that max-width isn't just for images, it can be applied to any element that you would apply a width to. I should also note that if you try the IE fake to keep it from enlarging a container, you have to apply it to the problem element, not the container itself. You can apply the IE fake to a container (to keep it from going naturally over a certain width ) BUT if an element inside that container (an image, table, whatever) is larger than that width it will still enlarge it's container in IE. In those cases apply max-width and the fake to the element causing the issue, not the container itself. Did I just confuse the matter?

So for an example, if images in the #sidebar div were causing an issue doing this

#sidebar {width:220px;
  width: expression(this.width > 220 ? 220: true);
}

would NOT HELP IE. Here it's only applied to the containing div#sidebar. The offending images would still enlarge it.

Attack the element directly like

#sidebar img {max-width:200px;
   width: expression(this.width > 200 ? 200: true);
}

A statement like that would keep the images in the #sidebar (if that's the id of your sidebar, it could be different) in check.

Then again you could resize those damn images... ;-)

Post a Comment

55 comments:

Yokhannan said...

Thank you, thank you, thank you!!! I keep showing this method to all my friends and nobody believes me when I tell them HAHAHAH.

Chinchilla Verde said...

Thank you very much! This was extremely helpful and concise. (Why, oh why, is IE such a pain!?!?). Thanks again!

Anonymous said...

thanx!

I've used over 7 hours trying to figure this IE-width with images out. Thanx one again :o)

regards, Dan

Norway

Anonymous said...

Tried to do this for select elements... this is how:

select {width: expression(this.clientWidth > 100 ? '100px':this.clientWidth+'px')
}

Regards Patrik

naabster said...

Thanks, you saved my day.. damn IE..

Anonymous said...

it worked, but when the image has been cached already.
it means that if it's the first time you viewing the page you will get original size picture!

phydeaux3 said...

anonymous-

Dunno, I've seen it work with normal pages with/without the images being cached.

I do seem to remember seeing that behaviour when the images are added dynamically (via javascript)...don't know if that's how you're using it or not. In my case when I was using javascript, I just threw in some sort of mathmatics on the image and resized it in the script...but that was months ago.

So yeah, there probably are circumstances where it won't work well in IE. I think in most cases it does.

Christina Yates said...

wait, wait, wait! I'm totally lost. My cloud shows in firefox and not in ie. Is the max value thing the problem. So what do I do with it now?

phydeaux3 said...

yea, I'll say you're lost christina. :-D
From you comment I assume you need to be waaaaaaaaaaaaaaaaaaaaaay over here
http://phydeaux3.blogspot.com/2006/09/code-for-beta-blogger-label-cloud.html

And if that's what you are talking about, looks like you got it sorted. ;-)

Nitingke said...

Thank Youuuuuuuuuuuuuuu!!!! you saved my day :D

Patrik said...

For select elements in IE7 you should use

select {width: expression(this.clientWidth > 100 ? '100px':true)
}

Also works in IE6...

/Patrik

Jack said...

Unfortunately, as far as I've been able to find out you can't use this method with percentages in IE(least I haven't found a way to ).

I worked it as a percentage as follows:

width:expression(document.body.clientWidth/2 > this.width? "50%": "auto";

Anonymous said...

I can't get it to work! This is the exact code that I'm using right now:

#content img {
border: 1px dotted #595959;
padding: 3px;
margin: 0 0 25px 0;
max-width: 535px;
width: expression(this.width > 535 ? 535: true);
}

The max-width works perfect in Opera, Firefox and IE7, but the images are still 700px wide in IE6! The code has no effect! What am I doing wrong?

Anonymous said...

You are my hero. Seriously.

Manny said...

Thank you!!! Worked pretty well.

Insane Ninja said...

i had to do a little tweaking of my own...

div#display img {
max-height: 400px;
max-width: 450px;
height: expression(this.height > this.width && this.height > 400 ? '400px' : true);
width: expression(this.height < this.width && this.width > 450 ? '450px' : true);
}

works in ie7 and i suppose it would work in ie6

Dylan said...

Hey thanks, what a nifty little trick. I needed to calculate to containers width cause of my design. All container divs are calculated on the fly by JS and sent to php. I just needed something for my pictures which are in a gallery. The gallery is embedded in another CMS. And I could set max width on all the pictures, but then I only have 'small' pictures.

Since I already calculate all container divs sizes and positions anyway, I might as well set the IE6 max width for images.

Thanks for sharing.

Robert L said...

Thanks alot. Supergood to know. You could update your post with some of the great comments posted here though. :)

Anonymous said...

r0x0r. Thanks a ton. IE7 makes the baby jesus cry.

Anonymous said...

I tried this

.img_size2 {
max-width: 100px;
max-height: 150px;
width: expression(this.width > 100 ? 100:true);
height: expression(this.height > 150 ? 150:true);
}

and it works but for some weird reason on certain image sizes it makes them square????

I then tried another piece of code

.img_size_prods
{
max-height: 150px;
max-width: 150px;
height: expression(this.height > this.width && this.height > 150 ? '150px' : true);
width: expression(this.height < this.width && this.width > 150 ? '150px' : true);
}

and this i thought worked perfectly until i put a square image in and it doesn't size it at all! can anyone help?

Cheers! J

Anonymous said...

This works for me:
CSS Image Resize to Fit Bounding Box

Anonymous said...

In IE, to achieve the same affect of:
max-height: 100px;
max-width: 100px;

Use:
height: expression(this.height >= this.width && this.height > 100 ? 100 : true);
width: expression(this.height < this.width && this.width > 100 ? 100 : true);

Notice the '>=' for the height-- this takes care of square images.

Also, as mentioned before, you can use percentages or ems in the scaling side, like so:
height: expression(this.height >= this.width && this.height > 100 ? '8em' : true);
width: expression(this.height < this.width && this.width > 100 ? '8em' : true);

elizabeth said...

i lie prone at your feet for this nug o' code. thanks much.

zhao-zhuxi said...

God bless You! Thank You, thank you, thank you!

Eric said...

THANK YOU!

Bollywood Zone said...

Thank you for providing this simple and useful hack.

duniacyber.com said...

Thank you for this solution, i already tried but still same i’m stuck
but anyway thank again !!
this is my site with error css www.duniacyber.com
.gbrkiriadsbigdet {
float:right;
max-width:120px;
max-height:80px;
_height: auto !important;
_width: auto !important;
height: 80px;
width: 120px;
border:0;
padding-bottom:1px;
padding-right:3px;
padding-top:1px;
}

Now I have found the solution...
Thx a lot, God bless u
this is worling properly for ie 6 7 ff 1.5 2
.gbrkiriadsbigdet {
float:right;
max-width:120px;
max-height:80px;
height: expression(this.height >= this.width && this.height > 80 ? 80 : true);
width: expression(this.height < this.width && this.width > 120 ? 120 : true);
border:0;
padding-bottom:1px;
padding-right:3px;
padding-top:1px;
}

Anonymous said...

Just in case ya wanted to know, the
expression(this.width > 400 ? 400: true); is an inline programing statement.


(expression_to_evaluate) ? val_if_true : val_if_false;

ex.

if(time<12)
return "good morning";
else
return "good evening";


Is exactly the same as:

(time<12)?"good morning":"good evening";

There is no difference, it's just shorter to write and is useful in css because it only takes one line of code. Just thought i'd let ya know.

Lawrence said...

Hi,

Nice trick there.
But I still have a problem.
I'm using your trick to make a DIV (width 100% and height 100%) with a border of 10px and a margin with the body of 30px. The problem is to make it without any scrollbars and cross-browser.
so I used your trick (60 = 2x30px margin):

width: expression(document.body.clientWidth - 60);

and same for height. And man what was I glad to see this works just great! Now the problem is Mozilla FF ofcourse, any suggestion? I surfed google for hours :).
PS: the DIV is the only HTML I must use. (no table, no header-content-footer and no wrapper div's).

Cheers

Anonymous said...

max-width and faking it for IE - great workaround.

thank you very much,

Miguel

gane said...

thanks... thanks... this is what I wanted.. I googled a times... Thanks for the help.

bvdecicco said...

What is a good way to force IE to deal with 100% instead of pixels?

Anonymous said...

thank you so much!

Ale said...

Wow man thanx a lot!
That really helped!
The Ad Mad!

Anonymous said...

This is the best way around in the net.

Thanks to all

Anonymous said...

Doesnt anyone validate their CSS/(X)HTML?
Very great trick for IE but how would I do this so it also gets validated? ;(

Is said...

Hi phydeaux3,

That's why for those using 3 column template will be having right sidebar go down below :)

With your full explainations, it will benefit for them to encounter such a problem.

Jay said...

Thanks a lotttt!!! This really saved the day. Insane Ninja's version worked perfectly for my needs.

inko9nito said...

Awesome! Thank you so much for posting this! Works perfect.

My Best Choices Team said...

Thank you!!! Worked pretty well.
http://mcmasterr.blogspot.com/

enjoy...

My Best Choices Team said...

Thank you for providing this simple and useful hack.

http://mcmasterr.blogspot.com/

mcmaster

Anonymous said...

wow, thanks, exactly what I needed!

Anonymous said...

you can get width of element by using in expression this.clientWidth only if element position is absolute. I don't understand how you use this.width... to me returns 'undefined'. We also can call function that sets (prototype code):
test = element;
var els = test.style;
var originalVisibility = els.visibility;
var originalPosition = els.position;
var originalDisplay = els.display;
els.visibility = 'hidden';
els.position = 'absolute';
els.display = 'block';
var originalWidth = test.clientWidth;
var originalHeight = test.clientHeight;
els.display = originalDisplay;
els.position = originalPosition;
els.visibility = originalVisibility;
width = originalWidth;
height = originalHeight;
return {width: originalWidth, height: originalHeight};

but...
when I change style in js, browser refresh the element and call expression... and i got infinite loop

Kate Yoak said...

This is totally awesome, thanks. I like using max-width in combination with max-height basically, to say, "this is the box the image needs to fit into - resize accordingly".

Here is how to adapt your idea to this:

max-width: 300px;
width: expression(this.width>300 && this.width>this.height ? 300: true);
max-height: 300px;
height: expression(this.height>300 && this.height>this.width? 300: true);

Graphic Design Studio Sydney - Gotham said...

Thankyou SO much, I was struggling with an old edition of IE that just wasn't showing it how I needed, and with your code it is now up and running beautifully! This has saved me a lot of time that I would of spent trying to solve it myself via trial and error.

Am VERY glad I found your solution first.

Anonymous said...

Very helpful, thanks!!!

Ash said...

I just want to join the ranks of people to say thanks.
Too long have I been searching with too many rubbish suggestions.

This worked in 10seconds!

Amazing!

Regards, Ash

Anonymous said...

Thank you, thank you, thank you :)

Anonymous said...

THANK YOU, THANK YOU, THANK YOU!

greg said...

Worked perfectly for me on ff, opera, chrome, ie7 and ie8. Didn't test ie6 though. Great work!!

Anonymous said...

Thank you so much my friend, for making the "Web Designer-IE" relation less painful. /cheers :)

Alper said...

How was the song?
You are the best, you are the champion :)
Thanks for that life saver method..

Paul said...

It even works in IE6, so it must be good!

spazdaq said...

ran into an issue with IE7 and 8 not properly rendering containing elements of images where max-width was used. put in your example which serves as sort of a "catch all" and it works like a charm now. thanks for the info.

Jackie said...

Thanks for this, it worked for me!

Post a Comment

Newer Post Older Post Home