100%最小高度CSS布局

时间:2008-08-24 17:59:04

标签: html css xhtml

在各种浏览器中制作100%最小高度元素的最佳方法是什么?特别是如果你有一个页眉和页脚固定高度的布局,你如何使中间内容部分填充100%的空间,页脚固定在底部?

14 个答案:

答案 0 :(得分:110)

我使用以下内容:CSS Layout - 100 % height

  

<强>最小高度

     

此页面的#container元素的最小高度为100%。那   如果内容需要比视口提供的更高的高度,   #content强制#container的高度也变长。   然后可以使用背景显示#content中可能的列   #container上的图像; div不是表格单元格,你不需要(或   想要)物理元素来创造这样的视觉效果。如果你是   尚未说服;想想摇摆不定的线条和渐变而不是   直线和简单的配色方案。

     

相对定位

     

因为#container具有相对位置,所以#footer将始终保留   在它的底部;因为上面提到的最小高度并没有阻止   #container来自缩放,即使(或者更确切地说是)#content强制#container变得更长,这也会起作用。

     

<强>填充底

     

由于它不再处于正常流程中,因此填充了#content的底部   现在为绝对#footer提供了空间。这个填充是   默认包含在滚动高度中,以便页脚将   从不重叠上述内容。

     

稍微缩放文本大小或调整浏览器窗口大小以测试它   布局。

html,body {
    margin:0;
    padding:0;
    height:100%; /* needed for container min-height */
    background:gray;

    font-family:arial,sans-serif;
    font-size:small;
    color:#666;
}

h1 { 
    font:1.5em georgia,serif; 
    margin:0.5em 0;
}

h2 {
    font:1.25em georgia,serif; 
    margin:0 0 0.5em;
}
    h1, h2, a {
        color:orange;
    }

p { 
    line-height:1.5; 
    margin:0 0 1em;
}

div#container {
    position:relative; /* needed for footer positioning*/
    margin:0 auto; /* center, not in IE5 */
    width:750px;
    background:#f0f0f0;

    height:auto !important; /* real browsers */
    height:100%; /* IE6: treaded as min-height*/

    min-height:100%; /* real browsers */
}

div#header {
    padding:1em;
    background:#ddd url("../csslayout.gif") 98% 10px no-repeat;
    border-bottom:6px double gray;
}
    div#header p {
        font-style:italic;
        font-size:1.1em;
        margin:0;
    }

div#content {
    padding:1em 1em 5em; /* bottom padding for footer */
}
    div#content p {
        text-align:justify;
        padding:0 1em;
    }

div#footer {
    position:absolute;
    width:100%;
    bottom:0; /* stick to bottom */
    background:#ddd;
    border-top:6px double gray;
}
div#footer p {
    padding:1em;
    margin:0;
}

对我来说很好。

答案 1 :(得分:30)

设置锁定到某个位置的自定义高度:

body, html {
  height: 100%;
}
#outerbox {
  width: 100%;
  position: absolute; /* to place it somewhere on the screen */
  top: 130px;         /* free space at top */
  bottom: 0;          /* makes it lock to the bottom */
}
#innerbox {
  width: 100%;
  position: absolute;				
  min-height: 100% !important; /* browser fill */
  height: auto;                /*content fill */
}
<div id="outerbox">
  <div id="innerbox"></div>
</div>

答案 2 :(得分:22)

以下是基于vhviewpoint height的其他解决方案,详情请访问CSS units。它基于此solution,它使用flex代替。

&#13;
&#13;
* {
    /* personal preference */
    margin: 0;
    padding: 0;
}
html {
    /* make sure we use up the whole viewport */
    width: 100%;
    min-height: 100vh;
    /* for debugging, a red background lets us see any seams */
    background-color: red;
}
body {
    /* make sure we use the full width but allow for more height */
    width: 100%;
    min-height: 100vh; /* this helps with the sticky footer */
}
main {
    /* for debugging, a blue background lets us see the content */
    background-color: skyblue;
	min-height: calc(100vh - 2.5em); /* this leaves space for the sticky footer */
}
footer {
    /* for debugging, a gray background lets us see the footer */
    background-color: gray;
	min-height:2.5em;
}
&#13;
<main>
    <p>This is the content. Resize the viewport vertically to see how the footer behaves.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
    <p>This is the content.</p>
</main>
<footer>
    <p>This is the footer. Resize the viewport horizontally to see how the height behaves when text wraps.</p>
    <p>This is the footer.</p>
</footer>
&#13;
&#13;
&#13;

  

单位是vw,vh,vmax,vmin。基本上,每个单位等于视口大小的1%。因此,随着视口的更改,浏览器会计算该值并相应地进行调整。

您可以找到更多信息here:

  

具体做法是:

1vw (viewport width) = 1% of viewport width
1vh (viewport height) = 1% of viewport height
1vmin (viewport minimum) = 1vw or 1vh, whatever is smallest
1vmax (viewport minimum) = 1vw or 1vh, whatever is largest

答案 3 :(得分:15)

kleolb02的回答看起来很不错。另一种方式是sticky footermin-height hack

的组合

答案 4 :(得分:6)

CSS解决方案(#content { min-height: 100%; })可以在很多情况下使用,但不适用于所有情况 - 尤其是IE6和IE7。

不幸的是,您需要使用JavaScript解决方案才能获得所需的行为。 这可以通过计算内容<div>的所需高度并将其设置为函数中的CSS属性来完成:

function resizeContent() {
  var contentDiv = document.getElementById('content');
  var headerDiv = document.getElementById('header');
  // This may need to be done differently on IE than FF, but you get the idea.
  var viewPortHeight = window.innerHeight - headerDiv.clientHeight;
  contentDiv.style.height = 
    Math.max(viewportHeight, contentDiv.clientHeight) + 'px';
}

然后,您可以将此功能设置为onLoadonResize事件的处理程序:

<body onload="resizeContent()" onresize="resizeContent()">
  . . .
</body>

答案 5 :(得分:6)

要让min-height正确使用百分比,在继承它的父节点min-height时,诀窍是将父节点高度设置为1px,然后将子节点设置为min-height将正常工作。

Demo page

答案 6 :(得分:3)

我同意Levik,因为如果您有侧边栏并且希望它们填充空间以满足您不能将它们设置为100%的页脚,因为它们将是父级高度的100%,因此父容器设置为100%这意味着当使用清除功能时,页脚最终会被推倒。

如果您的标题高度为50px且页脚高度为50px并且内容只是自动调整到剩余空间(例如100px)并且页面容器是此值的100%,其高度将为200px 。然后,当您将侧边栏高度设置为100%时,它就是200px,即使它应该适合在页眉和页脚之间。相反,它最终为50px + 200px + 50px,因此页面现在为300px,因为侧边栏设置为与页面容器相同的高度。页面内容会有一个很大的空白区域。

我使用的是Internet Explorer 9,这就是我使用这种100%方法时的效果。我没有在其他浏览器中尝试过它,我认为它可能适用于其他一些选项。但它不会普及。

答案 7 :(得分:3)

首先,您应该在div div之后创建id='footer' content,然后执行此操作。

您的HTML应如下所示:

<html>
    <body>
        <div id="content">
            ...
        </div>
        <div id="footer"></div>
    </body>
</html>

CSS:

​html, body {
    height: 100%;   
}
#content {
    height: 100%;
}
#footer {
    clear: both;        
}

答案 8 :(得分:2)

试试这个:

body{ height: 100%; }
#content { 
    min-height: 500px;
    height: 100%;
}
#footer {
    height: 100px;
    clear: both !important;
}

内容div下方的div元素必须为clear:both

答案 9 :(得分:2)

可能是最短的解决方案(仅适用于现代浏览器)

这一小块CSS使"the middle content part fill 100% of the space in between with the footer fixed to the bottom"

html, body { height: 100%; }
your_container { min-height: calc(100% - height_of_your_footer); }

唯一的要求是你需要一个固定的高度页脚。

例如,对于此布局:

<html><head></head><body>
  <main> your main content </main>
  </footer> your footer content </footer>
</body></html>

你需要这个CSS:

html, body { height: 100%; }
main { min-height: calc(100% - 2em); }
footer { height: 2em; }

答案 10 :(得分:1)

你可以试试这个:http://www.monkey-business.biz/88/horizontal-zentriertes-100-hohe-css-layout/ 这是100%的高度和水平中心。

答案 11 :(得分:0)

只是分享我已经使用过的东西,并且效果很好

#content{
        height: auto;
        min-height:350px;
}

答案 12 :(得分:0)

正如Afshin Mehrabani的回答中所提到的,你应该将body和html高度设置为100%,但要获取页脚,请计算包装器的高度:

#pagewrapper{
/* Firefox */
height: -moz-calc(100% - 100px); /*assume i.e. your header above the wrapper is 80 and the footer bellow is 20*/
/* WebKit */
height: -webkit-calc(100% - 100px);
/* Opera */
height: -o-calc(100% - 100px);
/* Standard */
height: calc(100% - 100px);
}

答案 13 :(得分:0)

如指定的 the height property in MDN docs 它不是继承的。所以你需要将它设置为 100%。众所周知,任何网页都以 html 开头,然后将正文作为其子页面,您需要在两者中设置 heigth: 100%

html,
   body {
     height: 100%;
   }

<html>
  <body>

  </body>
</html>

任何标有身高 100% 的孩子都将是父母身高的 100 的一部分。在上面的示例样式中,我们将 html 标签设置为 100%,它将是可用屏幕高度的全高。那么 body 是 100%,所以它也将是完整的高度,因为它的父元素是 html。

相关问题