Html 有没有办法为div设置垂直滚动条?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6698741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 09:34:20  来源:igfitidea点击:

Is there a way to set vertical scrollbar for a div?

htmlcss

提问by Steven Zack

I put a empty content div into a td tag in a table. It shows as there is only one line in div. If I set the height of the div, when I input more content than it's height, it still keep the same size as it was. How to add a vertical scrollbar when content need to consume more space in the div? Thanks.

我将一个空的内容 div 放入表格中的 td 标签中。它显示为 div 中只有一行。如果我设置 div 的高度,当我输入的内容超过它的高度时,它仍然保持与原来相同的大小。当内容需要在div中消耗更多空间时如何添加垂直滚动条?谢谢。

回答by John Hartsock

Automatically show scroll bars when content is greater than height and width like this:

当内容大于高度和宽度时自动显示滚动条,如下所示:

<div style="overflow:auto;"></div>

OR

或者

Show scroll bars at all times like this:

像这样始终显示滚动条:

<div style="overflow:scroll;"></div>

Below is some reference for CSS overflow properties:

以下是 CSS 溢出属性的一些参考:

overflow

溢出

overflow-x

溢出-x

overflow-y

溢出-y

回答by Optillect Team

Use overflow, or overflow-y: auto| hidden| scroll| visible

使用overflow, 或overflow-y: auto| hidden| scroll|visible

<div style="max-height:100px; overflow-y:auto"> ... </div>

or

或者

<div style="max-height:100px; overflow-y:scroll"> ... </div>

NOTE:overflowsupported since CSS2, but overflow-y- CSS3, Opera 9.6, Safari 3.1

注意:overflow自 CSS2 起支持,但overflow-y- CSS3、Opera 9.6、Safari 3.1

回答by Chintan

If you want vertical scrollbar then

如果你想要垂直滚动条,那么

<div style="overflow-y:auto;overflow-x:hidden"></div>

回答by JAiro

only vertical scroll

只有垂直滚动

<div style="overflow-y:auto;"></div>

回答by calumbrodie

do this in css

在 css 中执行此操作

div {overflow: scroll}

edit:What's with all the inline styles guys :-(

编辑:所有内联样式的家伙是什么:-(