我有以下页面(deadlink: http://www.workingstorage.com/Sample.htm),有一个脚注,我不能坐在页面的底部。
我想让页脚
当页面较短且屏幕未被填充时,坚持在窗口底部 当有超过一个屏幕的内容时,保持在文档末尾,并像往常一样向下移动(而不是重叠内容)。
CSS是继承的,让我困惑。我似乎不能正确地改变它,把一个最小高度的内容或使页脚到底部。
我有以下页面(deadlink: http://www.workingstorage.com/Sample.htm),有一个脚注,我不能坐在页面的底部。
我想让页脚
当页面较短且屏幕未被填充时,坚持在窗口底部 当有超过一个屏幕的内容时,保持在文档末尾,并像往常一样向下移动(而不是重叠内容)。
CSS是继承的,让我困惑。我似乎不能正确地改变它,把一个最小高度的内容或使页脚到底部。
当前回答
下面是我的4种不同的方法:
在每个示例中,文本都是可自由编辑的,以说明内容在不同场景中的呈现方式。
1) 弹性框
车身{min-height: 100vh;保证金:0;} 标题{最低高度:50 px;背景:浅蓝色;} 页脚{最低高度:50 px;背景:番木瓜;} /*文章把页眉和页脚之间的空格都填满了*/ 身体{显示:flex;flex-direction:列;} 文章{flex: 1;} <身体> <头contentEditable >标题头> < / <文章contentEditable > < / >文章内容 <页脚contentEditable >页脚> < /页脚 < /身体>
2)网格
身体{ 最小高度:100 vh; 保证金:0; 显示:网格; Grid-template-rows: auto 1fr auto; } 标题{ 最小高度:50 px; 背景:浅蓝色; } 页脚{ 最小高度:50 px; 背景:番木瓜; } <身体> <头contentEditable >标题头> < / <文章contentEditable > < / >文章内容 <页脚contentEditable >页脚> < /页脚 < /身体>
下面的方法使用了一个“技巧”,在body上放置一个::after伪元素,并将其设置为与footer相同的高度,因此它将占据与footer相同的空间,因此当footer绝对定位在它上面时,它将看起来像footer真的占据了空间,并消除了它的绝对定位的负面影响(例如,越过body的内容)
3)位置:绝对(无动态页脚高度)
body{ min-height:100vh; margin:0; position:relative; } header{ min-height:50px; background:lightcyan; } footer{ background:PapayaWhip; } /* Trick: */ body { position: relative; } body::after { content: ''; display: block; height: 50px; /* Set same as footer's height */ } footer { position: absolute; bottom: 0; width: 100%; height: 50px; } <body> <header contentEditable>Header</header> <article contentEditable>Content</article> <footer contentEditable>Footer</footer> </body>
4)表布局
html{高度:100%;} Body {min-height:100%;保证金:0;} 标题{ 高度:50 px; 背景:浅蓝色; } {条 高度:1%; } 页脚{ 高度:50 px; 背景:番木瓜; } /****技巧:****/ 身体{ 显示:表; 宽度:100%; } Body > footer { 显示:作用是; } <身体> <头contentEditable >标题头> < / <文章contentEditable > < / >文章内容 <页脚contentEditable >页脚> < /页脚 < /身体>
其他回答
只需将html、正文和除页脚之外的其他行设置为100%。 如
<body>
<header></header>
<content></content>
<footer></footer>
the CSS becomes
html, body, header, content{
height:100%;
}
下面是我的4种不同的方法:
在每个示例中,文本都是可自由编辑的,以说明内容在不同场景中的呈现方式。
1) 弹性框
车身{min-height: 100vh;保证金:0;} 标题{最低高度:50 px;背景:浅蓝色;} 页脚{最低高度:50 px;背景:番木瓜;} /*文章把页眉和页脚之间的空格都填满了*/ 身体{显示:flex;flex-direction:列;} 文章{flex: 1;} <身体> <头contentEditable >标题头> < / <文章contentEditable > < / >文章内容 <页脚contentEditable >页脚> < /页脚 < /身体>
2)网格
身体{ 最小高度:100 vh; 保证金:0; 显示:网格; Grid-template-rows: auto 1fr auto; } 标题{ 最小高度:50 px; 背景:浅蓝色; } 页脚{ 最小高度:50 px; 背景:番木瓜; } <身体> <头contentEditable >标题头> < / <文章contentEditable > < / >文章内容 <页脚contentEditable >页脚> < /页脚 < /身体>
下面的方法使用了一个“技巧”,在body上放置一个::after伪元素,并将其设置为与footer相同的高度,因此它将占据与footer相同的空间,因此当footer绝对定位在它上面时,它将看起来像footer真的占据了空间,并消除了它的绝对定位的负面影响(例如,越过body的内容)
3)位置:绝对(无动态页脚高度)
body{ min-height:100vh; margin:0; position:relative; } header{ min-height:50px; background:lightcyan; } footer{ background:PapayaWhip; } /* Trick: */ body { position: relative; } body::after { content: ''; display: block; height: 50px; /* Set same as footer's height */ } footer { position: absolute; bottom: 0; width: 100%; height: 50px; } <body> <header contentEditable>Header</header> <article contentEditable>Content</article> <footer contentEditable>Footer</footer> </body>
4)表布局
html{高度:100%;} Body {min-height:100%;保证金:0;} 标题{ 高度:50 px; 背景:浅蓝色; } {条 高度:1%; } 页脚{ 高度:50 px; 背景:番木瓜; } /****技巧:****/ 身体{ 显示:表; 宽度:100%; } Body > footer { 显示:作用是; } <身体> <头contentEditable >标题头> < / <文章contentEditable > < / >文章内容 <页脚contentEditable >页脚> < /页脚 < /身体>
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" type="text/css" href="main.css" />
</head>
<body>
<div id="page-container">
<div id="content-wrap">
<!-- all other page content -->
</div>
<footer id="footer"></footer>
</div>
</body>
</html>
#page-container {
position: relative;
min-height: 100vh;
}
#content-wrap {
padding-bottom: 2.5rem; /* Footer height */
}
#footer {
position: absolute;
bottom: 0;
width: 100%;
height: 2.5rem; /* Footer height */
}
你需要使用position: absolute;这很重要,下面是0
.footer {
position: absolute;
bottom: 0;
left: 0;
right: 0;
}
我使用的一个简单的解决方案,从IE8+工作
在html上给min-height:100%,这样如果内容较少,那么页面仍然采用完整的视图高度,页脚贴在页面底部。当内容增加时,页脚随着内容向下移动,并保持贴在底部。
JS小提琴工作演示:http://jsfiddle.net/3L3h64qo/2/
Css
html{
position:relative;
min-height: 100%;
}
/*Normalize html and body elements,this style is just good to have*/
html,body{
margin:0;
padding:0;
}
.pageContentWrapper{
margin-bottom:100px;/* Height of footer*/
}
.footer{
position: absolute;
bottom: 0;
left: 0;
right: 0;
height:100px;
background:#ccc;
}
Html
<html>
<body>
<div class="pageContentWrapper">
<!-- All the page content goes here-->
</div>
<div class="footer">
</div>
</body>
</html>