如果文本溢出 HTML/CSS,强制 div 滚动?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6752237/
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
Force div to scroll if the text overflows HTML/CSS?
提问by Alper
So I have a div tag to sort of draw boxes around various sections, and of course make actual sections.
所以我有一个 div 标签来在各个部分周围绘制框,当然还有实际的部分。
In one of the sections, there is more text than can be held in the div tag, so I want to for the text within the div tag to have a scroll bar to make it so the text doesn't overflow outside of the box.
在其中一个部分中,文本超出了 div 标签所能容纳的范围,因此我希望 div 标签内的文本有一个滚动条,以便文本不会溢出框外。
How would I do that?
我该怎么做?
回答by fletom
Use the following:
使用以下内容:
div {
overflow: scroll;
}
If you want them to scroll only in one direction, you can use the overflow-x
and overflow-y
properties.
如果您希望它们仅向一个方向滚动,则可以使用overflow-x
和overflow-y
属性。
回答by Emil
Add width
and height
CSS properties to the div
, as well as overflow:auto
添加width
和height
CSS 属性到div
,以及overflow:auto
If you add overlow:scroll
, the scroll bars will be always visible.
如果添加overlow:scroll
,滚动条将始终可见。
If you want to have only horizontal scroll, add white-space:nowrap
to the element inside of the div
.
如果只想水平滚动,请添加white-space:nowrap
到div
.