CSS - 让 div 占用所有可用空间
所有,
我有一个页面应该只占用浏览器中的可用屏幕空间。
我有一个“顶栏”和一个“底栏”,它们都固定位于页面的顶部和底部。我想要一个 div 来消耗(占用)上面提到的两个条之间的剩余空间。
中间的 div 不与顶部和底部栏重叠是至关重要的。这可以用 CSS 实现吗?还是我需要使用 js?
另外,如果我确实使用js,考虑到浏览器在js代码之前先加载CSS,那么使用js进行居中定位的上述效果如何?
非常感谢,
All,
I have a page which is suppose to take up only the available screen space in the browser.
I have a 'top bar' and a 'bottom bar', both of which are fixed positioned at the top and bottom of the page. I want to have a div which will consume (take up) the remaining of the space inbetween the two bars mentioned above.
Its crucial that the middle div is not overlapped by the top and bottom bars. Is this at all possible with CSS or do I need to make use of js.
Also, if I do go with js, considering the browser loads up the CSS first before the js code, how is the above work out using js for centre positioning?
Many thanks,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用
相对
和绝对
位置。这里有一个例子:css
html
演示: http://jsfiddle.net/jz4rb/4
You can use
relative
andabsolute
positions. Here an example:css
html
Demo: http://jsfiddle.net/jz4rb/4
此演示适用于 Chrome12,但 YMMV 取决于您需要支持的浏览器。例如,
position:fixed
在 IE6 中无法正常工作。This demo works for me in Chrome12 but YMMV depending on which browsers you need to support. For example
position:fixed
does not work correctly in IE6.在
body
标记上使用绝对定位。顶部和底部为零的position:absolute
会将主体“拉伸”到与浏览器窗口的大小相同。或者,设置height: 100%
也可以,但我记得它对于某些旧浏览器来说效果很奇怪。然后在中心 div 上使用绝对定位,并具有足够的顶部/底部偏移量以避免页眉和页脚栏。标题栏使用
top
绝对定位,fotter 使用bottom
绝对定位。注意:这不适用于移动浏览器。您需要使用 JS 获取窗口的高度并手动设置中心 div 的高度。
Use absolute positioning on the
body
tag.position:absolute
with zero top and bottom will "stretch" body to be the same size as the browser window. Alternatively, settingheight: 100%
also works but I remember it works wierd for certain old browsers.Then use absolute positioning on the center div, with enough top/bottom offsets to avoid your header and footer bars. The header bar is absolutely positioned with
top
and the fotter is absolutely positioned withbottom
.Note: This won't work on mobile browsers. You'll need to use JS to get the window's height and manually set the center div's height.
Sotiris 给出了很好的答案(https://stackoverflow.com/a/5776719/1925112)。
将“overflow:auto”添加到中间,在调整窗口大小时它会表现得更加完美。
Great answer by Sotiris (https://stackoverflow.com/a/5776719/1925112).
Add "overflow:auto" to middle, and it will behave more than perfectly when resizing the window.