使用上面和下面未知高度div的CSS将div设置为剩余高度

时间:2011-12-18 23:55:19

标签: html css height scrollable

是否可以使包装器填充窗口高度(无滚动)和中心div可滚动而不会弄乱像素和javascript?

<div id="wrapper">
  <h1>Header</h1>
  <div id="center">
    <div style="height:1000px">high content</div>
  </div>
  <div id="footer">Footer</div>
</div>

基本上我希望标题在顶部可见,页脚始终在底部可见,并且在中心有一个可滚动的内容,占据了重新高度。
页眉,页脚和中心div的高度都是未知的(没有设置px或%,即可变字体大小或填充)。用纯CSS可以吗?

4 个答案:

答案 0 :(得分:80)

2014年更新:解决此布局问题的现代方法是use the flexbox CSS model。它受到所有主流浏览器和IE11 +的支持。


2012:单独使用CSS执行此操作的正确方法是使用display: tabledisplay: table-row。这些是supported by all major browsers,从IE8开始。这是使用表格进行显示。你将使用div:

html, body {
    height: 100%;
    margin: 0;
}
.wrapper {
    display: table;
    height: 100%;
    width: 100%;
    background: yellow;  /* just to make sure nothing bleeds */
}
.header {
    display: table-row;
    background: gray;
}
.content {
    display: table-row;  /* height is dynamic, and will expand... */
    height: 100%;        /* ...as content is added (won't scroll) */
    background: turquoise;
}
.footer {
    display: table-row;
    background: lightgray;
}
<div class="wrapper">
    <div class="header">
        <h1>Header</h1>
        <p>Header of variable height</p>
    </div>
    <div class="content">
        <h2>Content that expands in height dynamically to adjust for new content</h2>
        Content height will initially be the remaining
        height in its container (<code>.wrapper</code>).
        <!-- p style="font-size: 4000%">Tall content</p -->
    </div>
    <div class="footer">
        <h3>Sticky footer</h3>
        <p>Footer of variable height</p>
    </div>
</div>

就是这样。 div正如你所期望的那样包裹起来。

答案 1 :(得分:5)

源自Dan Dascalescu答案的跨浏览器解决方案:

http://jsfiddle.net/Uc9E2

&#13;
&#13;
html, body {
    margin: 0;
    padding: 0;
    height: 100%;
}
.l-fit-height {
    display: table;
    height: 100%;
}
.l-fit-height-row {
    display: table-row;
    height: 1px;
}
.l-fit-height-row-content {
    /* Firefox requires this */
    display: table-cell;
}
.l-fit-height-row-expanded {
    height: 100%;
    display: table-row;
}
.l-fit-height-row-expanded > .l-fit-height-row-content {
    height: 100%;
    width: 100%;
}
@-moz-document url-prefix() {
    .l-scroll {
        /* Firefox requires this to do the absolute positioning correctly */
        display: inline-block;
    }
}
.l-scroll {
    overflow-y: auto;
    position: relative;
    height: 1000px;
}
.l-scroll-content {
    position: absolute;
    top: 0;
    bottom: 0;
    height: 1000px;
    min-height:100px;
}
&#13;
<div class="l-fit-height">
    <section class="l-fit-height-row">
        <div class="l-fit-height-row-content">
            <p>Header</p>
        </div>
    </section>
    <section class="l-fit-height-row-expanded">
        <div class="l-fit-height-row-content l-scroll">
            <div class="l-scroll-content">
                <p>Foo</p>
            </div>
        </div>
    </section>
    <section class="l-fit-height-row">
        <div class="l-fit-height-row-content">
            <p>Footer</p>
        </div>
    </section>
</div>
&#13;
&#13;
&#13;

答案 2 :(得分:-2)

所以你所说的是一个粘性的页脚。我去做了更多的研究,这就是我对你的看法。

<div id="wrapper" style="height:100%">
<div id="header" style="float:none;"><h1>Header</h1></div>

<div style="overflow:scroll;float:none;height:auto;">high content</div>

<div id="footer" style="clear:both;position:fixed;bottom:0px;"><h1>Footer</h1></div>
</div>

这会给你一个粘性页脚。关键是位置:固定和底部:0px; 不幸的是,这意味着它也会悬停在scrollview中的任何内容之上。到目前为止似乎只有Javascript来解决这个问题,但我会继续寻找。

答案 3 :(得分:-3)

使用overflow:auto可以执行此操作。

demo