如何设置div使用一定百分比的高度,同时使用overflow:auto放置滚动条?
我有以下代码:
div.leftnav {
overflow: auto ;
height: 50% ;
}
当我设置此类的大 div 时,它不起作用(它不显示滚动条,也不将其大小调整为 50%,但是如果我使用
height: 400px ;
或其他一些“绝对”, 它就可以完美工作“ 价值。
I have the following code:
div.leftnav {
overflow: auto ;
height: 50% ;
}
That, when I set a big div of this class, does not work (it doesn't show the scrollbar nor resize it to 50%, however it works perfectly if instead if I use
height: 400px ;
Or some other "absolute" value.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
仅当父元素也有高度指令时,高度才会起作用。
假设您有非常简单的标记:
那么以下 CSS 将为您工作:
不过,您始终需要有高度说明。如果父元素之一缺少高度指令,则 100% 对 div 没有任何意义。如果您无法访问树下的所有元素,那么您将需要一个具有固定高度的父元素:
然后您需要这个 css :
height will only work if the parent element has a height instruction as well.
So say you have really simple markup :
Then the following CSS will work for you :
You need to have height instructions all the way though. If there is a height instruction missing on one of the parent elements, then the 100% won't mean anything to the div. If you can't access all the elements down the tree, then you will need a parent element with a fixed height :
Then you need this css :