首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用CSS将div设置为剩余高度,上面和下面的div高度未知

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

Stack Overflow用户
提问于 2011-12-19 07:55:19
回答 3查看 64.7K关注 0票数 56

有没有可能让包装器填充窗口高度(没有滚动),并且中间的div可滚动,而不会影响像素和javascript?

代码语言: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?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-11-17 00:46:03

Dan Dascalescu派生的跨浏览器解决方案答案:

http://jsfiddle.net/Uc9E2

代码语言:javascript
复制
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;
}
代码语言:javascript
复制
<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>

票数 5
EN

Stack Overflow用户

发布于 2011-12-19 08:01:20

使用overflow:auto可以让你做到这一点。

demo

票数 -2
EN

Stack Overflow用户

发布于 2011-12-19 09:45:33

所以你正在谈论的是一个粘性的脚注。我去做了更多的研究,这是我为你准备的。

代码语言:javascript
复制
<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>

这将给你一个粘性的页脚。关键字是position:fixed和bottom:0px;不幸的是,这意味着它也会悬停在滚动视图中的任何内容之上。到目前为止,似乎只有Javascript能解决这个问题,但我会继续寻找。

票数 -2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/8555820

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档