2013-04-28 169 views
0

我需要使元素的边距扩大以使其居中在页面上。使元素填充可用高度

元素1和3应分别粘贴到顶部和底部。
元素2有一个固定的高度,应该在页面居中。

我不知道如果我解释这还不够清楚,所以我画了一幅画:

enter image description here

+0

它看起来像1和3固定的高度,而不是2 – 2013-04-28 22:20:38

+0

好,我认为它是元素2中的余量。 – 2013-04-28 22:31:13

回答

3

修复元素1和元素3分别使用绝对定位到页面的顶部和底部。然后,绝对定位元素2的顶部50%并设置其高度的一半的负上边距:

#top, #middle, #bottom { position: absolute; left: 0; right: 0; } 
#top { top: 0; } 
#middle { top: 50%; margin-top: -25px; height: 50px; } 
#bottom { bottom: 0; } 

Try it.

+0

这种方法的优点是它可以在IE7中工作。 – 2013-04-28 22:34:09

+0

谢谢,这似乎是一个很好的解决方案! – 2013-04-28 22:38:40

0

或者,你可以做这样的事情:Example

body, html { 
    height: 100%; 
    display: table; 
    width: 100%; 
    margin: 0px; 
    padding: 0px; 
} 
header, div, footer { 
    width: 100%; 
    display: table-row; 
} 
header { 
    height: 50px; 
} 
footer { 
    height: 50px; 
}