dan*_*ial 55 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>
Run Code Online (Sandbox Code Playgroud)
基本上我希望标题在顶部可见,页脚始终在底部可见,并且在中心有一个可滚动的内容,占据了重新生成的高度.
页眉,页脚和中心div的高度都是未知的(没有设置px或%,即可变字体大小或填充).纯CSS可以吗?
Dan*_*scu 81
2014更新:解决此布局问题的现代方法是使用flexboxCSS模型.它受到所有主流浏览器和IE11 +的支持.
2012:单独使用CSS的正确方法是使用display: table和display: table-row.从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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)
而已.div正如你所期望的那样包裹起来.
源自Dan Dascalescu的跨浏览器解决方案回答:
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;
}Run Code Online (Sandbox Code Playgroud)
<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>Run Code Online (Sandbox Code Playgroud)