我正在开发一个Web应用程序,我希望内容能够填满整个屏幕的高度.
该页面有一个标题,其中包含徽标和帐户信息.这可以是任意高度.我希望内容div将页面的其余部分填充到底部.
我有标题div
和内容div
.目前我正在使用表格进行布局,如下所示:
CSS和HTML
#page {
height: 100%; width: 100%
}
#tdcontent {
height: 100%;
}
#content {
overflow: auto; /* or overflow: hidden; */
}
...
...
页面的整个高度都已填满,无需滚动.
对于内容div中的任何内容,设置top: 0;
将把它放在标题下面.有时内容将是一个真实的桌子,其高度设置为100%.放在header
里面content
不会允许这个工作.
有没有办法在不使用的情况下达到相同的效果table
?
更新:
内容中的元素div
也将高度设置为百分比.因此,100%内部的东西div
将填充到底部.两个元素也是50%.
更新2:
例如,如果标题占据屏幕高度的20%,则内部指定为50%的表#content
将占用屏幕空间的40%.到目前为止,将整个事物包装在表中是唯一有效的方法.
还有两个简单提及flexbox的答案; 然而,这是两年多以前的事了,他们没有提供任何例子.flexbox的规范现在已经确定.
注意:尽管CSS Flexible Boxes Layout规范处于候选推荐阶段,但并非所有浏览器都实现了它.WebKit实现必须以-webkit-为前缀; Internet Explorer实现了旧版本的规范,前缀为-ms-; Opera 12.10实现了最新版本的规范,没有前缀.有关最新的兼容性状态,请参阅每个属性的兼容性表.
(取自https://developer.mozilla.org/en-US/docs/Web/Guide/CSS/Flexible_boxes)
所有主流浏览器和IE11 +都支持Flexbox.对于IE 10或更早版本,您可以使用FlexieJS垫片.
要查看当前支持,您还可以在此处查看:http: //caniuse.com/#feat=flexbox
使用flexbox,您可以轻松地在具有固定尺寸,内容大小尺寸或剩余空间尺寸的任何行或列之间切换.在我的示例中,我已将标题设置为与其内容对齐(根据OPs问题),我添加了一个页脚以显示如何添加固定高度区域,然后设置内容区域以填充剩余空间.
html,
body {
height: 100%;
margin: 0
}
.box {
display: flex;
flex-flow: column;
height: 100%;
}
.box .row {
border: 1px dotted grey;
}
.box .row.header {
flex: 0 1 auto;
/* The above is shorthand for:
flex-grow: 0,
flex-shrink: 1,
flex-basis: auto
*/
}
.box .row.content {
flex: 1 1 auto;
}
.box .row.footer {
flex: 0 1 40px;
}
header
(sized to content)
content
(fills remaining space)
footer (fixed height)
在CSS中确实没有一种完善的跨浏览器方式.假设您的布局很复杂,您需要使用JavaScript来设置元素的高度.您需要做的事情的本质是:
Element Height = Viewport height - element.offset.top - desired bottom margin
一旦你可以获得这个值并设置元素的高度,你需要将事件处理程序附加到窗口onload和onresize,以便你可以激活你的resize函数.
此外,假设您的内容可能大于视口,则需要设置overflow-y进行滚动.
原帖是3年多以前的.我想很多像我这样来这篇文章的人都在寻找类似应用程序的布局解决方案,比如以某种方式固定页眉,页脚和全高内容占据了剩下的屏幕.如果是这样,这篇文章可能会有所帮助,它适用于IE7 +等.
http://blog.stevensanderson.com/2011/10/05/full-height-app-layouts-a-css-trick-to-make-it-easier/
以下是该帖子的一些片段:
@media screen {
/* start of screen rules. */
/* Generic pane rules */
body { margin: 0 }
.row, .col { overflow: hidden; position: absolute; }
.row { left: 0; right: 0; }
.col { top: 0; bottom: 0; }
.scroll-x { overflow-x: auto; }
.scroll-y { overflow-y: auto; }
.header.row { height: 75px; top: 0; }
.body.row { top: 75px; bottom: 50px; }
.footer.row { height: 50px; bottom: 0; }
/* end of screen rules. */
}
My header
The body
My footer
您可以使用CSS表,而不是使用标记中的表.
hellothere
body { display:table; width:100%; } div { display:table-row; } div+ div { height:100%; }
FIDDLE1和 FIDDLE2
这种方法的一些优点是:
1)减少标记
2)标记比表更具语义,因为这不是表格数据.
3)浏览器支持非常好:IE8 +,所有现代浏览器和移动设备(caniuse)
table { display: table } tr { display: table-row } thead { display: table-header-group } tbody { display: table-row-group } tfoot { display: table-footer-group } col { display: table-column } colgroup { display: table-column-group } td, th { display: table-cell } caption { display: table-caption }
如果希望中间元素垂直跨越整个页面,可以使用calc()
CSS3中引入的内容.
假设我们有一个固定的高度 header
和footer
元素,我们希望section
标签采取整个可用的垂直高度...
演示
假设标记
100px Expand me for remaining space
所以你的CSS应该是
html, body { height: 100%; } header { height: 100px; background: grey; } section { height: calc(100% - (100px + 150px)); /* Adding 100px of header and 150px of footer */ background: tomato; } footer { height: 150px; background-color: blue; }
所以在这里,正在做的是,将元素的高度相加,而不是从100%
使用calc()
函数中扣除.
只需确保您使用height: 100%;
父元素.
用过的:
height: calc(100vh - 110px);
码:
.header { height: 60px; top: 0; background-color: green}
.body {
height: calc(100vh - 110px); /*50+60*/
background-color: gray;
}
.footer { height: 50px; bottom: 0; }
My header
The body
My footer
一个简单的解决方案,使用flexbox:
html,
body {
height: 100%;
}
body {
display: flex;
flex-direction: column;
}
.content {
flex-grow: 1;
}
header
Codepen样本
另一种解决方案,div在内容div中居中
怎么样,你只需使用vh
它代表view height
在CSS ...
查看我在下面为您创建的代码段并运行它:
body {
padding: 0;
margin: 0;
}
.full-height {
width: 100px;
height: 100vh;
background: red;
}
CSS3简单方法
height: calc(100% - 10px); // 10px is height of your first div...
如今所有主流浏览器都支持它,所以如果您不需要支持老式浏览器,请继续.
它可以纯粹通过CSS
使用vh
:
#page {
display:block;
width:100%;
height:95vh !important;
overflow:hidden;
}
#tdcontent {
float:left;
width:100%;
display:block;
}
#content {
float:left;
width:100%;
height:100%;
display:block;
overflow:scroll;
}
和 HTML
我查了一下它,它可以在所有主要的浏览器:Chrome
,IE
,和FireFox
当内容太高时需要底部div滚动时,没有任何解决方案发布.这是一个适用于这种情况的解决方案:
HTML:
This is the header whose height is unknownThis is the header whose height is unknownThis is the header whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknownThis is the scrollable content whose height is unknown
CSS:
.table { display: table; } .table-row { display: table-row; } .table-cell { display: table-cell; } .container { width: 400px; height: 300px; } .header { background: cyan; } .body { background: yellow; height: 100%; } .body-content-outer-wrapper { height: 100%; } .body-content-inner-wrapper { height: 100%; position: relative; overflow: auto; } .body-content { position: absolute; top: 0; bottom: 0; left: 0; right: 0; }
原始来源:在处理CSS溢出时填充容器的剩余高度
JSFiddle实时预览
我也一直在寻找答案.如果您有幸能够以IE8及更高版本为目标,则可以使用display:table
相关值来获取包含div的块级元素的表的呈现规则.
如果您更幸运并且您的用户使用的是顶级浏览器(例如,如果这是您控制的计算机上的Intranet应用程序,就像我的最新项目一样),您可以在CSS3中使用新的Flexible Box布局!
对我有用的(在另一个div中使用div而我在其他所有情况下都假设)是将底部填充设置为100%.也就是说,将其添加到您的css/stylesheet:
padding-bottom: 100%;
免责声明:接受的答案给出了解决方案的想法,但我发现它有点臃肿与不必要的包装和css规则.以下是一个css规则很少的解决方案.
HTML 5
Header with an arbitrary height This container will grow so as to take the remaining height
CSS
body { display: flex; flex-direction: column; min-height: 100vh; /* body takes whole viewport's height */ } main { flex: 1; /* this will make the container take the free space */ }
上面的解决方案使用视口单元和flexbox,因此是IE10 +,您可以使用IE10的旧语法.
使用Codepen:链接到codepen
或者这个,对于那些需要主容器在内容溢出的情况下可滚动的人:链接到codepen
现在有很多答案,但我发现使用height: 100vh;
div元素需要填满整个可用的垂直空间.
通过这种方式,我不需要玩显示或定位.这在使用Bootstrap制作仪表板时派上用场,其中我有侧边栏和主边栏.我希望主要拉伸并填充整个垂直空间,以便我可以应用背景颜色.
div { height: 100vh; }
支持IE9及以上版本:点击查看链接
Test HeaderContentHeader stuff
Content stuff
在所有理智的浏览器中,您可以将"header"div放在内容之前,作为兄弟,并且相同的CSS将起作用.但是,如果在这种情况下浮点数是100%,则IE7-不能正确解释高度,因此标题需要在内容中,如上所述.溢出:auto会导致IE上的双滚动条(它始终具有视口滚动条可见但已禁用),但如果没有它,内容将在其溢出时剪辑.
我暂时对此感到满意,结果如下:
由于很容易使内容DIV与父级相同,但显然难以使父级高度减去标题高度我决定使内容div达到全高,但绝对位于左上角然后定义填充对于具有标题高度的顶部.这样,内容整齐地显示在标题下并填充整个剩余空间:
body {
padding: 0;
margin: 0;
height: 100%;
overflow: hidden;
}
#header {
position: absolute;
top: 0;
left: 0;
height: 50px;
}
#content {
position: absolute;
top: 0;
left: 0;
padding-top: 50px;
height: 100%;
}
如果您可以处理不支持旧浏览器(即MSIE 9或更早版本),则可以使用已经是W3C CR的Flexible Box Layout Module来完成此操作.该模块也允许其他好的技巧,例如重新排序内容.
不幸的是,MSIE 9或更低版本不支持这一点,你必须为除Firefox之外的每个浏览器使用CSS属性的供应商前缀.希望其他供应商也很快放弃前缀.
另一种选择是CSS网格布局,但是稳定版本的浏览器支持更少.实际上,只有MSIE 10支持这一点.
为什么不这样呢?
html, body {
height: 100%;
}
#containerInput {
background-image: url('../img/edit_bg.jpg');
height: 40%;
}
#containerControl {
background-image: url('../img/control_bg.jpg');
height: 60%;
}
给你html和body(按此顺序)一个高度,然后给你的元素一个高度?
适合我
CSS网格解决方案
只需定义body
with display:grid
和grid-template-rows
using auto
以及fr
value属性.
* {
margin: 0;
padding: 0;
}
html {
height: 100%;
}
body {
min-height: 100%;
display: grid;
grid-template-rows: auto 1fr auto;
}
header {
padding: 1em;
background: pink;
}
main {
padding: 1em;
background: lightblue;
}
footer {
padding: 2em;
background: lightgreen;
}
main:hover {
height: 2000px;
/* demos expansion of center element */
}
HEADER
MAIN
为我解决了这个问题.在我的情况下,我将此应用于所需的div
实际上,您可以使用display: table
将区域拆分为两个元素(标题和内容),其中标题的高度可能不同,内容会填充剩余的空间.这适用于整个页面,以及当面积仅仅是定位与其他元素的含量position
设置为relative
,absolute
或fixed
.只要父元素的高度为非零,它就会起作用.
看到这个小提琴,以及下面的代码:
CSS:
body, html {
height: 100%;
margin: 0;
padding: 0;
}
p {
margin: 0;
padding: 0;
}
.additional-padding {
height: 50px;
background-color: #DE9;
}
.as-table {
display: table;
height: 100%;
width: 100%;
}
.as-table-row {
display: table-row;
height: 100%;
}
#content {
width: 100%;
height: 100%;
background-color: #33DD44;
}
HTML:
This header can vary in height, it also doesn't have to be displayed as table-row. It will simply take the necessary space and the rest below will be taken by the second div which is displayed as table-row. Now adding some copy to artificially expand the header.
This is the actual content that takes the rest of the available space.
文森特,我会再次使用您的新要求回答.由于您不关心隐藏的内容(如果内容太长),您不需要浮动标题.只需将溢出隐藏在html和body标签上,并将#content
高度设置为100%.内容将始终比视口的高度长,但它将被隐藏,不会导致滚动条.
Test HeaderHeader stuff
ContentContent stuff
Positioned Content