Solution (JavaScript Solution)
Date of the Solution
Fri Jul 17 13:31:20 2009
Fixed Versions
all of the affected
Description
I CAN HAS MIN-HEIGHT!
Lorem ipsum
dolor sit amet,
consectetuer adipiscing elit.
Suspendisse vel velit at ipsum
tristique porttitor. Suspendisse
potenti. Nam non arcu sit amet
nulla volutpat bibendum. Aenean
I CAN HAS MIN-HEIGHT!
- HTML Code:
-
<p id="buggy">I CAN HAS MIN-HEIGHT!</p>
<p id="biggie">
Lorem ipsum<br>
dolor sit amet,<br>
consectetuer adipiscing elit.<br>
Suspendisse vel velit at ipsum<br>
tristique porttitor. Suspendisse<br>
potenti. Nam non arcu sit amet<br>
nulla volutpat bibendum. Aenean<br>
</p>
<p id="normal">I CAN HAS MIN-HEIGHT!</p>
- CSS Code:
#buggy,
#biggie {
min-height: 100px;
overflow: hidden; /* only to show that
everything works fine */
}
- Conditional Comments:
-
<!--[if lt IE 7]>
<style type="text/css">
#buggy,
#biggie {
height: expression(
this.scrollHeight <= 100
? "100px"
: "auto"
);
}
</style>
<![endif]-->
Once again, markup and CSS code are the same as in the original demo. I have added #buggy, #biggie { overflow: hidden; } only to demonstrate that overflow is not an issue anymore as it was with "Solution (CSS)".
What makes the fix tick is the proprietary expression() value for the height property inside Conditional Comments which, once again, target IE versions below 7. It allows us to execute JavaScript code right in the stylesheet and assign the return value to the property it is the value of.
If you are not familiar with JS I will explain what the code inside expression() does. First, it gets the height of the elements matching the selector (#buggy, #biggie). Then using ternary operator we will choose which height we will assign to the height property (in CSS code). Ternary operator basically works like this: "true or false value" ? "return this if value is true" : "return this if value is false". Thus our JS code says: Is height of this element less than or equal to 100 pixels? If yes, set height to 100px if not set it to auto
.
Keep in mind that height returned by this.scrollHeight will be in pixels, if you need some other unit use JavaScript code that will calculate an appropriate unit, this is beyond the scope of this tutorial.
Alternatively you may use MinMax script provided by DoxDesk. The script provides IE support for min-width, max-width, min-height and max-height properties (neither of which are supported in IE versions below 7). I saved a local copy of the script which you may use in case DoxDesk's link is broken. Simply include <script type="text/javascript" src="minmax.js"></script> inside your <head> tag and script will do its magic.