开发者

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

开发者 https://www.devze.com 2023-03-30 03:42 出处:网络
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). I want to center a div with

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). I want to center a div with variable height content in the space between the header and footer. In the below jsfiddle, it works if the content is shorter than the space, but if the conten开发者_JAVA百科t gets too long, it goes past the footer, and over the header. It also doesn't work at all in IE (surprise, surprise).

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

Edit: I've made some images to try and make this more clear.

Small content

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

Large Content

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


I ended up starting over and trying a different approach. The working solution is found in the new jsfiddle below. 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).

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


Try something like this:

.main { min-height: 500px }

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:

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;}
0

精彩评论

暂无评论...
验证码 换一张
取 消