我有以下页面(deadlink: http://www.workingstorage.com/Sample.htm),有一个脚注,我不能坐在页面的底部。
我想让页脚
当页面较短且屏幕未被填充时,坚持在窗口底部 当有超过一个屏幕的内容时,保持在文档末尾,并像往常一样向下移动(而不是重叠内容)。
CSS是继承的,让我困惑。我似乎不能正确地改变它,把一个最小高度的内容或使页脚到底部。
我有以下页面(deadlink: http://www.workingstorage.com/Sample.htm),有一个脚注,我不能坐在页面的底部。
我想让页脚
当页面较短且屏幕未被填充时,坚持在窗口底部 当有超过一个屏幕的内容时,保持在文档末尾,并像往常一样向下移动(而不是重叠内容)。
CSS是继承的,让我困惑。我似乎不能正确地改变它,把一个最小高度的内容或使页脚到底部。
当前回答
一个非常简单的跨浏览器工作的方法是:
http://matthewjamestaylor.com/blog/keeping-footers-at-the-bottom-of-the-page
html, body { margin:0; padding:0; height:100%; } #container { min-height:100%; position:relative; } #header { background:#ff0; padding:10px; } #body { padding:10px; padding-bottom:60px; /* Height of the footer */ } #footer { position:absolute; bottom:0; width:100%; height:60px; /* Height of the footer */ background:#6cf; } <div id="container"> <div id="header">header</div> <div id="body">body</div> <div id="footer">footer</div> </div>
其他回答
一个非常简单的弹性解决方案,不需要固定的高度或改变元素的位置。
HTML
<div class="container">
<header></header>
<main></main>
<footer></footer>
</div>
CSS
.container {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
}
浏览器支持
所有主流浏览器,除了IE11及以下版本。
确保使用Autoprefixer适当的前缀。
.container { display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-orient: vertical; -webkit-box-direction: normal; -ms-flex-direction: column; flex-direction: column; min-height: 100vh; } main { -webkit-box-flex: 1; -ms-flex: 1; flex: 1; } ///////////////////////////////////////////// body, html { margin: 0; padding: 0; } header, main, footer { margin: 0; display: block; } header, footer { min-height: 80px; } header { background-color: #ccc; } main { background-color: #f4f4f4; } footer { background-color: orange; } <div class="container"> <header></header> <main></main> <footer></footer> </div>
我认为你可以将主要内容设置为视口高度,所以如果内容超过,主要内容的高度将定义页脚的位置
* { 保证金:0; 填充:0; 宽度:100%; } 身体{ 显示:flex; flex-direction:列; } 标题{ 高度:50 px; 背景:红色; } 主要{ 背景:蓝色; /*这是最重要的部分 身高:100 vh; } 页脚{ 背景:黑色; 高度:50 px; 底部:0; } <标题> < /头> 主要主要< > < / > <页脚> < /页脚>
其实很简单。这个解决方案不需要知道页脚高度。
<body>
<div class="app">
Website
</div>
<div class="footer">
Footer
</div>
</body>
.app {
min-height: 100vh;
}
位置固定,底部,左边和右边设置为0最适合我:
.footer {
position: fixed;
background : #d1ccc0;
bottom : 0;
left : 0;
right : 0;
height : 50px;
}
绝对位置不会粘在底部,但固定位置会。
我刚才在这里回答了类似的问题
在页眉固定的页面底部放置页脚
我在网页开发方面是新手,我知道这个问题已经有了答案,但这是我发现的最简单的解决方法,我认为在某种程度上是不同的。我想要一些灵活的东西,因为我的网页应用程序的页脚有一个动态高度,我最终使用了FlexBox和一个间隔。
首先设置html和body的高度
html, body {
height: 100%;
display: flex;
flex-direction: column;
margin: 0px;
}
我假设我们的应用程序的列行为,在这种情况下,你需要添加一个标题,英雄或任何垂直对齐的内容。
创建spacer类
.spacer {
flex: 1;
}
之后你的HTML可能是这样的
<html>
<body>
<header> Header </header>
Some content...
<div class='spacer'></div>
<footer> Footer </footer>
</body>
</html>
你可以在这里玩 https://codepen.io/anon/pen/xmGZQL