2008-11-18 125 views
46

是否有一個很好的跨瀏覽器的方式來設置一個DIV的最大高度屬性,當該DIV超出最大高度,它變成溢出與滾動條?CSS最大高度屬性

+2

難道你不能只是將高度設置爲你的最大高度並設置`overflow:scroll;`? – Jason 2009-07-28 23:43:14

+1

@Jason使用靜態高度200像素。但是,如果您的身高是一個百分比例如100%,那麼溢出的內容將覆蓋指定的高度,只是拉伸元素,而不是剪裁或引入滾動條。 – 2010-04-23 15:44:33

回答

1

你可以有一個高度設置爲高度和溢出的包裝div:滾動。然後內部div沒有高度集,隨着它的增長,它將填充然後使用第一個div的滾動條?

+0

好的想法,但它仍然需要設置一個基準高度,以超過他想要的塊級別元素。否則,他只會在他真正關心的元素上設置這個高度。 – 2008-11-18 02:35:27

35

可悲的是IE6不那麼你必須使用對IE6的表達式,然後將所有其他瀏覽器的最大高度:

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; 
} 

溢出:汽車會在大多數情況下,有什麼最有可能的工作額外溢出。

+0

這有一個問題......它會在IE6上產生關於活動內容的警告。 – mcherm 2009-03-30 20:04:19

+0

我從來沒有使用過visual studio,但是css表達式()對於Internet Explorer來說是獨一無二的,所以我希望他們的開發工具能夠識別它。 – ethyreal 2010-10-21 21:10:55

16

我從2005年發佈的帖子中找到了這個解決方案(Min-Height Fast hack)。這是一個黑客,但它的簡單和純粹的CSS:

selector { 
    max-height:500px; 
    height:auto !important; 
    height:500px; 
} 

的例子是最大高度,但它適用於最小高度,最小寬度和最大寬度。 :)

*注意:您必須使用絕對值,百分比不起作用。

現在您只需要「溢出:滾動」;使這個滾動條的工作

7
selector 
{ 
    max-height:900px; 
    _height:expression(this.scrollHeight>899?"900px":"auto"); 
    overflow:auto; 
    overflow-x:hidden; 
} 
-1

我發現這從http://www.tutorialspoint.com/css/css_scrollbars.htm和修改了一下。它似乎工作都IE9和FF19

<style type="text/css"> 
.scroll{ 
    display:block; 
    border: 1px solid red; 
    padding:5px; 
    margin-top:5px; 
    width:300px; 

    max-height:100px; 
    overflow:scroll; 
} 
.auto{ 
    display:block; 
    border: 1px solid red; 
    padding:5px; 
    margin-top:5px; 
    width:300px; 
    height: 100px !important; 
    max-height:110px; 
    overflow:hidden; 
    overflow-y:auto; 
} 
</style> 
<p>Example of scroll value:</p> 

<div class="scroll"> 
    I am going to keep lot of content here just to show 
    you how scrollbars works if there is an overflow in 
    an element box. This provides your horizontal as well 
    as vertical scrollbars.<br/> 
    I am going to keep lot of content here just to show 
    you how scrollbars works if there is an overflow in 
    an element box. This provides your horizontal as well 
    as vertical scrollbars.<br/> 
    I am going to keep lot of content here just to show 
    you how scrollbars works if there is an overflow in 
    an element box. This provides your horizontal as well 
    as vertical scrollbars.<br/> 
    I am going to keep lot of content here just to show 
    you how scrollbars works if there is an overflow in 
    an element box. This provides your horizontal as well 
    as vertical scrollbars.<br/>   
    </div> 

<br /> 
<p>Example of auto value:</p> 

<div class="auto"> 
    I am going to keep lot of content here just to show 
    you how scrollbars works if there is an overflow in 
    an element box. This provides your horizontal as well 
    as vertical scrollbars.<br/> 
    </div> 
0

主要的黑客(RedWolves式):

.divMax{width:550px;height:200px;overflow-Y:auto;position:absolute;} 
.divInner{border:1px solid navy;background-color:white;} 

我是從最大高度,沒有得到愛的屬性,所以我有這個alreadyand成功與這些2類。但它在尋找更好的命中這個問題是醜陋的。具有position:absolute的divMax可讓內容顯示下來,但將divInner的最終高度控制爲200px。