在下面所示的标记中,我试图让内容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>


你可以用最小高度声明来搞定它

<div style="min-height: 100%">stuff</div>

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

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


试试下面的css规则:

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

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


不是所有浏览器都支持min-height属性。如果你需要你的#content在较长的页面上扩展它的高度,height属性会缩短它。

这是一个小技巧,但你可以在#content div中添加一个宽度为1px,高度为1000px的空div。这将迫使内容至少有1000px高,并且仍然允许更长的内容在需要时扩展高度


我没有代码,但我知道我这样做了一次使用高度:1000px和margin-bottom: -1000px的组合;试试。


虽然它不像纯CSS那样优雅,但一小部分javascript可以帮助实现这一点:

<html>
<head>
<style type='text/css'>
    div {
        border: 1px solid #000000;
    } 
</style>
<script type='text/javascript'>

    function expandToWindow(element) {
         var margin = 10; 

         if (element.style.height < window.innerHeight) { 
            element.style.height = window.innerHeight - (2 * margin) 
         }
    }


</script>
</head>
<body onload='expandToWindow(document.getElementById("content"));'>
<div id='content'>Hello World</div>
</body>
</html>

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


你的问题不是div没有100%的高度,而是它周围的容器没有。这将在我怀疑你正在使用的浏览器中有所帮助:

html,body { height:100%; }

你可能还需要调整填充和页边距,但这将使你达到90%的效果。如果你想让它在所有浏览器上都能正常工作,你就得花点心思。

这个网站有一些很好的例子:

http://www.brunildo.org/test/html_body_0.html http://www.brunildo.org/test/html_body_11b.html http://www.brunildo.org/test/index.html

我也推荐你去http://quirksmode.org/


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

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

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


Try:

html, body {
    height: 102%;
}
.wrapper {
    position: relative;
    height: 100%;
    width: 100%;
}
.div {
    position: absolute;
    top: 0;
    bottom: 0;
    width: 1000px;
    min-height: 100%;
}

还没测试过…


仅使用样式表(CSS)是不可能实现这一点的。有些浏览器不接受

height: 100%;

作为比浏览器窗口的视点更高的值。

Javascript是最简单的跨浏览器解决方案,尽管如上所述,它并不干净或漂亮。


我知道这不是最好的方法,但是如果不搞乱我的标题、菜单等位置,我就无法解决这个问题。所以…我用一张桌子放这两列。这是个权宜之计。不需要JS;)


试试http://mystrd.at/modern-clean-css-sticky-footer/

上面的链接坏了,但是这个链接https://stackoverflow.com/a/18066619/1944643是好的。: D

演示:

<!DOCTYPE html>
<head>
    <meta charset="UTF-8">
    <meta name="author" content="http://mystrd.at">
    <meta name="robots" content="noindex, nofollow">
    <title>James Dean CSS Sticky Footer</title>
    <style type="text/css">
        html {
            position: relative;
            min-height: 100%;
        }
        body {
            margin: 0 0 100px;
            /* bottom = footer height */
            padding: 25px;
        }
        footer {
            background-color: orange;
            position: absolute;
            left: 0;
            bottom: 0;
            height: 100px;
            width: 100%;
            overflow: hidden;
        }
    </style>
</head>
<body>
    <article>
        <!-- or <div class="container">, etc. -->
        <h1>James Dean CSS Sticky Footer</h1>

        <p>Blah blah blah blah</p>
        <p>More blah blah blah</p>
    </article>
    <footer>
        <h1>Footer Content</h1>
    </footer>
</body>

</html>

固定高度的粘脚:

HTML方案:

<body>
   <div id="wrap">
   </div>
   <div id="footer">
   </div>
</body>

CSS:

html, body {
    height: 100%;
}
#wrap {
    min-height: 100%;
    height: auto !important;
    height: 100%;
    margin: 0 auto -60px;
}
#footer {
    height: 60px;
}

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

如果没有足够的内容来填充可用的垂直浏览器视口,让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;
}

哒哒,或者我只是太困了


您可以使用“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;
}

我认为这个问题会被修复,只是让html填充100%, 可能是body 100%地填满了HTML但HTML并没有100%地填满屏幕。

试一试:

html, body {
      height: 100%;
}

#content {
    height: calc(100% - the amount of pixels the content div is away from the top);
}

所以如果你的div是从顶部200px,你需要的代码将是

#content {
    height: calc(100% - 200px);
}