下推相对div内的绝对溢出div
我有一个 CSS 布局,其中内容 div 具有绝对定位,以便能够在溢出时滚动内容。该内容 div 位于相对 div 内,因为上面的元素的高度可能不同(由其内容决定)。
但是,我的解决方案仅适用于 Chrome 和 IE9,内容 div 在 Firefox 中不显示滚动条。不幸的是,我的解决方案也不适用于旧版浏览器。您知道更好的布局来完成上述要求吗?
编辑:
布局应填充视口 100% 宽度和 100% 高度,这就是为什么我可能需要使用表格(如果我错了,请纠正我)。另外,我更喜欢没有 javascript 的解决方案,因为我有其他可以操纵内容的 javascript。
我当前的代码:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test</title>
<style type="text/css">
html, body, form { width:100%; height:100%; margin:0; overflow:hidden; }
</style>
</head>
<body>
<table style="width: 100%; height: 100%;">
<tr>
<td style="height: 120px; border: 1px solid #000;">
Top: always same height
</td>
</tr>
<tr style="height: 20px; background-color: Red;">
<td>
Variable height: could push the next row down
</td>
</tr>
<tr style="position: relative; overflow: auto;">
<td style="position: relative; background-color: White; vertical-align: top; overflow: scroll;">
<div style="position: relative;">
<div style="position: absolute; bottom: 0; top: 0; left: 0; right: 0; ">
Content with variable height: needs overflow auto/scroll
</div>
</div>
</td>
</tr>
</table>
</body>
</html>
I have a CSS layout in which a content div has absolute positioning to be able to scroll the content in case of overflow. This content div is positioned within a relative div, since elements above may differ in height (which is decided by their content).
However, my solution only works in Chrome and IE9, the content div doesn't show a scrollbar in Firefox. And unfortunately my solution also doesn't work in older browsers. Do you know a better layout to accomplish the above requirements?
Edit:
The layout should fill the viewport 100% width and 100% height, that's why I probably need to use tables (please correct me if I'm wrong). Also, I prefer a solution without javascript, since I have other javascripts which could manipulate the contents.
My current code:
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head><title>Test</title>
<style type="text/css">
html, body, form { width:100%; height:100%; margin:0; overflow:hidden; }
</style>
</head>
<body>
<table style="width: 100%; height: 100%;">
<tr>
<td style="height: 120px; border: 1px solid #000;">
Top: always same height
</td>
</tr>
<tr style="height: 20px; background-color: Red;">
<td>
Variable height: could push the next row down
</td>
</tr>
<tr style="position: relative; overflow: auto;">
<td style="position: relative; background-color: White; vertical-align: top; overflow: scroll;">
<div style="position: relative;">
<div style="position: absolute; bottom: 0; top: 0; left: 0; right: 0; ">
Content with variable height: needs overflow auto/scroll
</div>
</div>
</td>
</tr>
</table>
</body>
</html>
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我无法弄清楚如何仅使用 css 来做到这一点,但我可以使其与 javascript 一起工作。
HTML(正文内部):
CSS:
JavaScript(放置在头部):
getDocHeight()
函数来自此处:http://james.padolsey.com/javascript/get-document-height-cross-browser/I can't figure out how to do it with only css, but I can make it work with javascript.
HTML (inside body):
CSS:
JavaScript (to be placed in head):
The
getDocHeight()
function is from here: http://james.padolsey.com/javascript/get-document-height-cross-browser/