有没有一种良好的 跨浏览器 方法来设置 DIV 的 max-height
属性,当该 DIV 超过 max-height
时,它会变成带有滚动条的溢出状态?
不幸的是,IE6不支持这个属性,因此您需要使用一个针对IE6的表达式,然后为所有其他浏览器设置max-height:
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在大多数情况下都可以处理额外的溢出问题。
我从一个2005年的帖子中找到了这个解决方案(Min-Height Fast hack)。虽然它是一个hack,但它很简单,而且只涉及CSS:
selector {
max-height:500px;
height:auto !important;
height:500px;
}
这个示例是针对最大高度(max-height)的,但它同样适用于最小高度(min-height)、最小宽度(min-width)和最大宽度(max-width)。 :)
*注意:您必须使用绝对值,百分比无法生效。
现在,您所需要的就是添加“overflow:scroll;”就可以实现滚动条了。
min-width/height
,而不是max-width/height
。 - mercatorselector
{
max-height:900px;
_height:expression(this.scrollHeight>899?"900px":"auto");
overflow:auto;
overflow-x:hidden;
}
你可以使用一个高度设置为你所需高度并且带有overflow:scrolling属性的包装div。然后内部div没有设置高度,当它增长时,它将填充并使用第一个div的滚动条。
重大黑客攻击(RedWolves风格):
.divMax{width:550px;height:200px;overflow-Y:auto;position:absolute;}
.divInner{border:1px solid navy;background-color:white;}
我在使用max-height属性时遇到了问题,所以我已经使用了这两个类并成功解决了问题。但是这样做很丑陋,于是我在寻找更好的方法时发现了这个问题。divMax具有position:absolute
,可以让下面的内容显示出来,但控制divInner的最终高度为200px。
我从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>
overflow: scroll;
呢? - Jason