不固定高度的滚动div
我需要构建一个动态调整大小的滚动 div。
div 应动态调整大小以适合屏幕。但如果内容不适合屏幕,它应该显示一个滚动条。因此浏览器自己的滚动条永远不需要激活。
我可以通过在 div 中放置另一个 div 并使用 overflow: auto
来让滚动条出现在该 div 中。
<div id="gridcontainer" style="overflow:auto;height:300px; width:100px;" >
<div id="gridcontent" style="height:100%">
<!--Put loads of text in here-->
</div>
</div>
问题是这仅在第一个 div 具有固定高度时才有效。我本来希望我可以将第一个 div 设置为 height:100%
,但遗憾的是不是 - 该属性似乎被忽略,并且滚动条没有出现。
我尝试将 div 放入 height:100%
的表格中,并将第一个 div 设置为 height:auto
,希望它可以从其父级获取高度。但 div 似乎仍然忽略了 height 属性。
所以我的问题是:可以仅使用 html 来完成此操作,还是使用 javascript 来完成?
I need to build a dynamically-resizing scrolling div.
The div should dynamically resize to fit the screen. But if the content doesn't fit on the screen, it should display a scrollbar. So the browser's own scrollbar should never need to become active.
I can get a scrollbar to appear in the div by placing another div inside it and using overflow: auto
.
<div id="gridcontainer" style="overflow:auto;height:300px; width:100px;" >
<div id="gridcontent" style="height:100%">
<!--Put loads of text in here-->
</div>
</div>
The trouble is that this only works when the first div has a fixed height. I had hoped I could just set the first div to height:100%
, but sadly not- this property appears to get ignored, and the scrollbar just doesn't appear.
I have tried putting the divs in a table with height:100%
, and setting the first div to height:auto
, hoping it might take its height from its parent. But the div still seems to ignore the height property.
So my question is: Can this be done using just html, or- failing that- javascript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以使用绝对定位拉伸 div。这样,它将始终采用浏览器窗口(或最近的定位祖先)的大小。
鉴于此 HTML:
CSS 应该类似于:
Live Demo
You could stretch the div using absolute positioning. This way it will always take the size of the browser window (or the closest, positioned ancestor).
Given this HTML:
the CSS should be something like:
Live Demo
从 IE9 开始,您可以使用视口单位。
假设容器的高度是动态的,除非其大小大于窗口高度。在这种情况下,我们停止扩展并停止扩展。激活卷轴。
Since IE9 you can use viewport units.
Let's say that the height of your container is dynamic, unless its size is greater than the window height. In that case we stop the expansion & activate the scroll.
如果您尝试使元素适合屏幕,则可以将元素的高度和宽度值设置为 100%。
您还需要设置 html 和 body 的高度
If you are trying to make element fit the screen then you can set the value of hight and width of the element to 100%.
You will also need to set the height of html and body