Is there a good cross-browser way to set a max-height property of a DIV and when that DIV goes beyond the max-height, it turns into an overflow with scroll bars?
A:
Could you have a wrapper div with the height set as your height and overflow: scrolling. Then the inner div has no height set and as it grows it will fill then use the scrollbars of the first div?
RedWolves
2008-11-18 02:31:21
Good thought, but it still requires setting a base height to more than he wants for a block level element. Otherwise, he'd just set that height on the element he's really concerned with.
Joel Coehoorn
2008-11-18 02:35:27
+1
A:
Sadly ie doesn't so you have to use an expression for ie, then set the max-height for all other browsers:
div{
height: expression( this.scrollHeight > 332 ? "333px" : "auto" ); /* sets max-height for IE */
max-height: 333px; /* sets max-height value for all standards-compliant browsers */
overflow:scroll;
}
Overflow:auto would most likely work in most cases for have any extra spill over.
ethyreal
2008-11-18 03:48:09
There is a problem with this... it generates a warning on IE6 about active content.
mcherm
2009-03-30 20:04:19
should the expression keyword be understood by visual studio when editing a css file?
towps
2010-10-14 18:01:45
i have never used visual studio, but the css expression() is unique to internet explorer so i would hope their dev tools would recognize it.
ethyreal
2010-10-21 21:10:55
A:
I found this solution from a post made in 2005 (Min-Height Fast hack). It's a hack but it's simple and pure CSS:
selector {
max-height:500px;
height:auto !important;
height:500px;
}
The example is for max-height, but it works for min-height, min-width and max-width. :)
*Note: You must use absolute values, percentages don't work.
All you need now is the "overflow:scroll;" to make this work with scroll bars
fudgey
2009-07-28 23:40:25
This only works in IE6 (the only browser that needs a hack at all anymore for this) for `min-width/height`, *not* `max-width/height`.
mercator
2009-08-14 12:53:20