Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CSS Max Height Property

Tags:

css

overflow

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 scrollbars?

like image 550
Ryan Smith Avatar asked Nov 18 '08 02:11

Ryan Smith


People also ask

Is there a max-height in CSS?

The max-height property in CSS is used to set the maximum height of a specified element. Authors may use any of the length values as long as they are a positive value. max-height overrides height, but min-height always overrides max-height .

Can I use height and max-height?

The max-height and min-height properties are used to set the maximum and minimum height of an element. This prevents the value of the height property from becoming larger than max-height or smaller than min-height. The value of the max-height and/or min-height property overrides height.

How does Max-height work?

The max-height CSS property sets the maximum height of an element. It prevents the used value of the height property from becoming larger than the value specified for max-height .


2 Answers

Sadly IE6 doesn't so you have to use an expression for IE6, then set the max-height for all other browsers:

 div{        _height: expression( this.scrollHeight > 332 ? "333px" : "auto" ); /* sets max-height for IE6 */        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.

like image 67
ethyreal Avatar answered Sep 29 '22 12:09

ethyreal


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

like image 33
Mottie Avatar answered Sep 29 '22 13:09

Mottie