一段内容显示在web页面的时候,想对所要显示的区域做下限制:内容较多时,超出预定宽高时自动隐藏,内容不够指定的区域宽高时,容器大小随内容变化而变化。
若用程序去做,动态创建容器,根据内容长短判断设置容器大小。这种方法可以,但不便于拓展。应该用CSS去处理。代码示例:
1 div{
2 height:expression( this.scrollHeight > 46 ? "46px" : "auto" );
3 max-height:46px;
4 overflow:hidden;
5 }
2 height:expression( this.scrollHeight > 46 ? "46px" : "auto" );
3 max-height:46px;
4 overflow:hidden;
5 }
这段css代码可完整兼容ie/ff/opera/safari浏览器。在IE7+、ff、opera等浏览器,支持了max/min-width、max/min-height。expression表达式只在ie6及以下有效,ie7以后,取消了对expression 的支持。
ps:http://perishablepress.com/press/2007/01/16/maximum-and-minimum-height-and-width-in-internet-explorer/