javascript 如何让div变成带有滚动条的容器?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4336129/
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-10-25 11:04:20  来源:igfitidea点击:

How to make the div become a container with scroll bar?

javascripthtmlcss

提问by Tattat

This is the simple code:

这是简单的代码:

<div id="container">            

    <div id = "information">
    </div>

</div>   

When I change the "information" to width 1000, and container width 100, the "information" become very long, but I would like to let the information div inside the div...I means, I want the container have a scroll bar, if the information's width is longer than the container. How can I do so? Thank you.

当我将“信息”更改为宽度1000,容器宽度为100时,“信息”变得很长,但我想让div内的信息div......我的意思是,我希望容器有一个滚动条, 如果信息的宽度比容器长。我该怎么做?谢谢你。

回答by Tatu Ulmanen

#container {
    overflow: auto;
}

回答by Robert Koritnik

This should do the trick:

这应该可以解决问题:

#container
{
    overflow: auto;
}

回答by Quentin

Set overflow: autoin the stylesheet.

overflow: auto在样式表中设置。

That said, it is almost always better to make use of the available space and not introduce small regions with their own scrollbars (which are harder to deal with with AT, and require scrolling more frequently to read)

也就是说,利用可用空间而不是引入带有自己的滚动条的小区域几乎总是更好(这更难处理 AT,并且需要更频繁地滚动才能阅读)

回答by elMarquis

Or use overflow-xand overflow-yto limit the scrolling to just vertical or horizontal.

或者使用overflow-xoverflow-y将滚动限制为仅垂直或水平滚动。

#container { overflow-x: auto; } /* Horizontal scrolling only */

or

或者

#container { overflow-y: auto; } /* Vertical scrolling only */