简体   繁体   English

在页面中间水平和垂直居中div,页眉和页脚粘在页面的顶部和底部

[英]Horizontally and vertically center div in the middle of page with header and footer stuck to top and bottom of page

I'm trying to make a page where I have a fixed height header and footer. 我正在尝试制作一个页面,我有一个固定的高度页眉和页脚。 The header is at the top of the screen (100% width) and the footer is at the bottom (100% width). 标题位于屏幕顶部(100%宽度),页脚位于底部(100%宽度)。 I want to center a div with variable height content in the space between the header and footer. 我想在页眉和页脚之间的空间中居中具有可变高度内容的div。 In the below jsfiddle, it works if the content is shorter than the space, but if the content gets too long, it goes past the footer, and over the header. 在下面的jsfiddle中,如果内容比空格短,它就可以工作,但是如果内容太长,它会越过页脚和标题。 It also doesn't work at all in IE (surprise, surprise). 它在IE中根本不起作用(惊喜,惊喜)。

Example: http://jsfiddle.net/VrfAU/4/ 示例: http//jsfiddle.net/VrfAU/4/

Edit: I've made some images to try and make this more clear. 编辑:我已经制作了一些图像来尝试使其更清晰。

Small content 内容小

内容框水平和垂直居中。

Large Content 大内容

内容框比页眉和页脚之间的间隙长,因此它会拉伸页面,使页脚保持在底部。

I ended up starting over and trying a different approach. 我最终重新开始尝试不同的方法。 The working solution is found in the new jsfiddle below. 工作解决方案可以在下面的新jsfiddle中找到。 The idea was to separate the header and footer from the content area so that they would sit on top and bottom. 我们的想法是将页眉和页脚与内容区域分开,以便它们位于顶部和底部。 Then it became much easier to center the content area in the space between those (with some hacks for older versions of IE). 然后,将内容区域集中在那些之间的空间变得更加容易(对于旧版本的IE,有一些黑客)。

http://jsfiddle.net/UYpnC/5/ http://jsfiddle.net/UYpnC/5/

Try something like this: 尝试这样的事情:

.main { min-height: 500px }

http://jsfiddle.net/VrfAU/8/ http://jsfiddle.net/VrfAU/8/

I used the css property z-index, which controls the stack order to fix this: I also used position: fixed to fix the header and footer: 我使用了css属性z-index,它控制堆栈顺序来解决这个问题:我还使用position:fixed来修复页眉和页脚:

I put 我放

        #header {
background: black;
width: 100%;
height: 66px;
position:fixed;
overflow: hidden;
z-index: 20;}


.main_wrap {
    display: table;
    width: 100%;
    height: 100%;
    margin-top: -88px;
    vertical-align: middle;
    position: relative;
    z-index: -1;
}
#footer {
background: black;
width: 100%;
position: relative;
font-size: 85%;
color: #d0d6e0;
margin-top: -22px;
position: fixed;}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM