在下面所示的标记中,我试图让内容div一直延伸到页面的底部,但只有在有内容要显示时才会拉伸。我想这样做的原因是,即使没有任何内容显示,垂直边界仍然出现在页面下方。

下面是我的演示:

body { font-family: Trebuchet MS, Verdana, MS Sans Serif; font-size:0.9em; margin:0; padding:0; } div#header { width: 100%; height: 100px; } #header a { background-position: 100px 30px; background: transparent url(site-style-images/sitelogo.jpg) no-repeat fixed 100px 30px; height: 80px; display: block; } #header, #menuwrapper { background-repeat: repeat; background-image: url(site-style-images/darkblue_background_color.jpg); } #menu #menuwrapper { height:25px; } div#menuwrapper { width:100% } #menu, #content { width:1024px; margin: 0 auto; } div#menu { height: 25px; background-color:#50657a; } <form id="form1"> <div id="header"> <a title="Home" href="index.html" /> </div> <div id="menuwrapper"> <div id="menu"> </div> </div> <div id="content"> </div> </form>


当前回答

您可以使用“vh”长度单位作为元素本身及其父元素的min-height属性。IE9开始支持:

<body class="full-height">
    <form id="form1">
    <div id="header">
        <a title="Home" href="index.html" />
    </div>

    <div id="menuwrapper">
        <div id="menu">
        </div>
    </div>

    <div id="content" class="full-height">
    </div>
</body>

CSS:

.full-height {
    min-height: 100vh;
    box-sizing: border-box;
}

其他回答

试试Ryan Fait的“Sticky Footer”解决方案,

http://ryanfait.com/sticky-footer/ http://ryanfait.com/resources/footer-stick-to-bottom-of-page/

适用于IE, Firefox, Chrome, Safari和Opera,但还没有测试。这是一个很好的解决方案。非常容易和可靠的实现。

我将尝试在标题中直接回答这个问题,而不是拼命在页面底部添加页脚。

如果没有足够的内容来填充可用的垂直浏览器视口,让div扩展到页面底部:

演示地址:http://jsfiddle.net/NN7ky(拖动框架手柄查看效果) (优点:干净、简单。缺点:需要flexbox - http://caniuse.com/flexbox)

HTML:

<body>
  
  <div class=div1>
    div1<br>
    div1<br>
    div1<br>
  </div>
  
  <div class=div2>
    div2<br>
    div2<br>
    div2<br>
  </div>
  
</body>

CSS:

* { padding: 0; margin: 0; }

html, body {
  height: 100%;
  
  display: flex;
  flex-direction: column;
}

body > * {
  flex-shrink: 0;
}

.div1 { background-color: yellow; }

.div2 {
  background-color: orange;
  flex-grow: 1;
}

哒哒,或者我只是太困了

试试下面的css规则:

#content {
    min-height: 600px;
    height: auto !important;
    height: 600px;
}

改变高度以适应你的页面。为了跨浏览器兼容性,高度提到了两次。

你可能还会喜欢这个:http://matthewjamestaylor.com/blog/ultimate-2-column-left-menu-pixels.htm

这不是你所要求的,但也可能适合你的需要。

根据布局的工作方式,你可能会在<html>元素上设置背景,它总是至少是视口的高度。