Html 当内容太大时使 div 滚动

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

make div scroll when contents too big

csshtmlscroll

提问by Kelly Larsen

enter image description here

在此处输入图片说明

I have this setup where the results are wrapped in span tags and are 'moved' using jquery into other divs. at the moment the search just returns 30 values max and it's all good but I don't feel that is a fair limitation. what I want to be able to do is scroll the div when there are more results than can be displayed, like a list box. I've tried a number of different methods but none of them seem to work very well. I'd like it to keep a fixed height too

我有这样的设置,其中结果被包装在 span 标签中,并使用 jquery '移动'到其他 div 中。目前,搜索最多只返回 30 个值,一切都很好,但我认为这不是一个公平的限制。我希望能够做的是当结果多于可以显示时滚动 div,例如列表框。我尝试了许多不同的方法,但似乎没有一个效果很好。我也希望它保持固定的高度

回答by Mr. Alien

You haven't shared any markup here but if you are willing to have a fixed height just use this

你还没有在这里分享任何标记,但如果你愿意有一个固定的高度就使用这个

.container {
   /* Optional - You can set a  min-height : whatever px; for reserving some space*/
   height: 200px; /* Fix a height here */
   overflow: auto; /* Optionally you can also use overflow: scroll; */
}

回答by Pavel Griza

In addition to @mr-alien answer: you can use max-heightto limit the size of a container and in the same time to make it fit the content:

除了@mr-alien 答案:您可以使用max-height限制容器的大小,同时使其适合内容:

.container {
   /* Optional - You can set a  min-height : whatever px; for reserving some space*/
   max-height: 200px; /* Fix a max-height here */
   overflow: auto; /* Optionally you can also use overflow: scroll; */
}