如果文本溢出 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

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

Force div to scroll if the text overflows HTML/CSS?

csshtmlscrollbar

提问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-xand overflow-yproperties.

如果您希望它们仅向一个方向滚动,则可以使用overflow-xoverflow-y属性。

回答by Emil

Add widthand heightCSS properties to the div, as well as overflow:auto

添加widthheightCSS 属性到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:nowrapto the element inside of the div.

如果只想水平滚动,请添加white-space:nowrapdiv.