Fall Sale! Code FALL2024 takes 25% OFF our Pro Plugins & Books »
Web Dev + WordPress + Security

Maximum and Minimum Height and Width in Internet Explorer

Behold the seventh wonder of the virtual world: max/min-height and max/min-width properties are possible in Internet Explorer! Indeed, by taking advantage of IE’s proprietary CSS attribute, expression, you too can whip IE widths and heights into desirable proportions. The CSS expression attribute enables JavaScript commands to be executed within Internet Explorer. JavaScript via CSS? Thanks, Microsoft!

How it works in other browsers

Why is this so great? Well, because in other, standards-compliant browsers, max/min-height and max/min-width properties are easily accomplished with this simple bit of CSS..

<div style="max-width:333px;">
	[the contents of this division will be at most 333px in width on compliant browsers]
</p>

Of course, this is waaay tooo easy for Internet Explorer, which completely fails to understand the max-width attribute. Fortunately, IE supports its own expression attribute, which enables us to use JavaScript expressions to manipulate (X)HTML document properties such as max-width and max-height. For example, to specify a width property value via the expression attribute, we could use this:

div {
	width: expression(333 + "px");
}

..Which is equivalent to this:

div {
	width: 333px;
}

The down side to using IE’s expression attribute is that it fails when JavaScript is disabled (or otherwise missing) in the user’s browser. Nonetheless, establishing max/min-widths/heights (as well as other properties) remains an important tool in the web developer’s toolbox. So, without further ado, here are a few generalized examples for slapping IE widths and heights into proper shape..

max-width in IE

This method has been verified in IE6 and should also work in IE5. Simply change the values to suit your needs (code commented with explanatory notes) and include in your document via conditional comment. In this example, we are setting the max-width at 777px1 for IE and all standards-compliant browsers:

* html div#division { 
	width: expression( document.body.clientWidth > 776 ? "777px" : "auto" ); /* sets max-width for IE */
}
div#division { 
	max-width: 777px; /* this sets the max-width value for all standards-compliant browsers */
}

In this example, we are setting the max-width at 33em for IE and all standards-compliant browsers:

* html div#division {
	width: expression( document.body.clientWidth > (500/12) * parseInt(document.body.currentStyle.fontSize) ? "33em" : "auto" );
}
div#division {
	max-width: 33em; /* this sets the max-width value for all standards-compliant browsers */
}

min-width in IE

This method has been verified in IE6 and should also work in IE5. Simply change the values to suit your needs (code commented with explanatory notes). In this example, we are setting the min-width at 333px1 for IE and all standards-compliant browsers:

* html div#division { 
	width: expression( document.body.clientWidth < 334 ? "333px" : "auto" ); /* set min-width for IE */
}
div#division { 
	min-width: 333px; /* sets min-width value for all standards-compliant browsers */
}

max-height in IE

This method has been verified in IE6 and should also work in IE5. Simply change the values to suit your needs (code commented with explanatory notes). In this example, we are setting the max-height at 333px1 for IE and all standards-compliant browsers:

* html div#division { 
	height: expression( this.scrollHeight > 332 ? "333px" : "auto" ); /* sets max-height for IE */
}
div#division {
	max-height: 333px; /* sets max-height value for all standards-compliant browsers */
}

min-height in IE

This method has been verified in IE6 and should also work in IE5. Simply change the values to suit your needs (code commented with explanatory notes). In this example, we are setting the min-height at 333px1 for IE and all standards-compliant browsers:

* html div#division { 
	height: expression( this.scrollHeight < 334 ? "333px" : "auto" ); /* sets min-height for IE */
}
div#division { 
	min-height: 333px; /* sets min-height value for all standards-compliant browsers */
}

Footnotes

  • 1 Why is the conditional value slightly different than the target value in the expression? Read this comment to find out.

About the Author
Jeff Starr = Web Developer. Book Author. Secretly Important.
USP Pro: Unlimited front-end forms for user-submitted posts and more.

73 responses to “Maximum and Minimum Height and Width in Internet Explorer”

  1. Fixed the problem. Atleast it works with my code.
    Although I stumbled upon the solution by mere accident. I was wondering, why is the expression code different for max-width and max-height.
    And used the following code, and it worked.

    width: expression(this.scrollWidth > 776 ? "777px" : "auto" ); /* sets max-width for IE */

    Let me know if there is any flaw in my reasoning.

  2. Perishable 2007/07/29 9:30 am

    Chirag,

    Glad to hear you fixed the problem. As far the difference between max-width and max-height, they are targeting two different aspects of the client browser, and thus require two different JavaScript expressions to accomplish it:

    document.body.clientWidth – returns the width of the broswer window

    this.scrollHeight – returns the height of the broswer window

    Other than the property values, the JavaScript expressions should be the only difference between the two CSS selectors.

    So, if the code you provided is correct and works as it should, everything is go! — Congrats!

  3. About min-height for IE:

    use min-height for the real browsers an
    * hmtl #foo{ height:xxxpx; }
    for IE.
    IE threads height like min-height. no need to use the expression in this case.

  4. Perishable 2007/09/22 9:15 pm

    Ah! Very good, HDL.. I need to add this to the article.
    Every opportunity to avoid unnecessary JavaScript (especially in CSS!) should definitely be taken.
    — Thank you for sharing this info!

  5. Greg Robinson 2007/10/16 7:41 am

    Thank you so much for publishing this information. I have been searching for half a day for a max-height work around in IE and THIS is finally something that works. You’ve saved the rest of the hair on my head!

  6. Perishable 2007/10/16 9:19 am

    You’re welcome! — Thanks for the positive feedback :)

  7. document.body.clientWidth – returns the width of the broswer window, but I want to fix a max-width for one of the divs/classes that I am using.
    Can anybody help me on how to do that?

  8. I am slightly confused as to how to do this.

    That is the image I am having trouble with. It resizes in Firefox to 70% of the browser just fine, but in Internet Explorer, it is way too big. I was wanting to set the maximum width at 700px. Thanks very much for the help.

  9. Perishable 2007/11/12 5:08 pm

    Yes, I see what you mean.. way too big in IE. I see you are using table-based layouts. I have not tried this technique on tables or their various components. You might try wrapping the image in a <div> (which would itself be placed within a td element). Once you have that, then return to this article and apply the max-width portion of the tutorial to the div rather than to the image or a table element.

  10. Thanks so much for your help.

    I have tried using the max-width for IE code, but I guess I am not putting the script in correctly.

    This is the code for the picture that has the problem:

    CurrentProjectsBigSurvey070904a.html

    How would I insert the code to get it to work? Thank you so much.

  11. Perishable 2007/11/14 6:28 pm

    Jess,

    It looks like your code was gobbled up by WordPress, but I’m not entirely sure..

    I will take a look at this issue as soon as possible. I downloaded an example page and will try hitting it over the head with a shovel to make it work.

    Stay tuned..

  12. Thanks very much!

Comments are closed for this post. Something to add? Let me know.
Welcome
Perishable Press is operated by Jeff Starr, a professional web developer and book author with two decades of experience. Here you will find posts about web development, WordPress, security, and more »
GA Pro: Add Google Analytics to WordPress like a pro.
Thoughts
I disabled AI in Google search results. It was making me lazy.
Went out walking today and soaked up some sunshine. It felt good.
I have an original box/packaging for 2010 iMac if anyone wants it free let me know.
Always ask AI to cite its sources. Also: “The Web” is not a valid answer.
All free plugins updated and ready for WP 6.6 dropping next week. Pro plugin updates in the works also complete :)
99% of video thumbnail/previews are pure cringe. Goofy faces = Clickbait.
RIP ICQ
Newsletter
Get news, updates, deals & tips via email.
Email kept private. Easy unsubscribe anytime.