jQuery 调整 div 的大小以适应最大高度的内容
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/10398069/
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
Resizing a div to fit content with maximum height
提问by qudoz
I have a div with dynamic content. When the content loads in the div I want the div to resize to fit the content. But I want this resizing to have a maximum height. If the content needs more than this maximum height I'd like there to be a scrollbar.
我有一个带有动态内容的 div。当内容加载到 div 中时,我希望 div 调整大小以适合内容。但我希望这种调整大小具有最大高度。如果内容需要超过这个最大高度,我希望有一个滚动条。
I've searched through the questions here but haven't been able to find what I'm looking for (apart from one where the question-asker replied saying he'd figured it out himself). I've read I can do it with jquery but I'm pretty new to that, but I did go through the demos of resizing a div, and changing classes and stuff like that but I'm really at a loss on how to implement what I'm looking for. I havetried to find a solution to this, so please be gentle.
我已经搜索了这里的问题,但没有找到我要找的东西(除了提问者回答说他自己想出来的问题)。我读过我可以用 jquery 做到这一点,但我对此很陌生,但我确实经历了调整 div 大小和更改类和类似内容的演示,但我真的不知道如何实现我在找什么。我也试图找到一个解决这个,所以请温柔。
回答by meWantToLearn
div {height:auto;overflow:scroll;max-height:200px;}
回答by Nick
Use:
用:
<div style="max-height: 400px; overflow-y:scroll; overflow-x: hidden">
<p>Some dynamic content loads inside the div</p>
</div>
Then add: <div style="clear:both"></div>
before closing the "div":
然后<div style="clear:both"></div>
在关闭“div”之前添加:
<div style="max-height: 400px">
<p>Some dynamic content loads inside the div</p>
<div style="clear:both"></div>
</div>
回答by alexg
You could do this with css, e.g.: max-height: 100px; overflow-y:scroll;
你可以用 css 来做到这一点,例如: max-height: 100px; overflow-y:scroll;