footer固定在页面底部的实现方法总结
方法一:footer高度固定+绝对定位
HTML代码:
<body>
<header>头部</header>
<main>中间内容</main>
<footer>底部信息</footer>
</body>
CSS代码:
*{
margin:;
padding:;
}
html{
height:100%;
}
body{
min-height:100%;
margin:;
padding:;
position:relative;
}
header{
background: #000;
text-align: center;
height:50px;
line-height: 50px;
color:#fff;
}
main{ /* main的padding-bottom值要等于或大于footer的height值 */
padding-bottom:100px;
background:#ccc;
text-align: center;
}
footer{
position:absolute;
color:#fff;
bottom:;
width:100%;
height:100px;
line-height:100px;
text-align:center;
background-color: #000;
}
实现的效果:

- 首先,设置body的高度至少充满整个屏幕,并且body作为footer绝对定位的参考节点;
- 其次,设置main(footer前一个兄弟元素)的padding-bottom值大于等于footer的height值,以保证main的内容能够全部显示出来而不被footer遮盖;
- 最后,设置footer绝对定位,并
设置height为固定高度值。
优点:footer一直存在于底部。
缺点:中间区域main如果内容不够,不能撑满页面的中间区域。
方法二:footer高度固定+margin负值
HTML代码:
<body>
<div class="container">
<header>头部</header>
<main>中间内容</main>
</div>
<footer>底部信息</footer>
</body>
CSS代码:
*{
margin:;
padding:;
}
html,body{
height:100%;
}
.container{
min-height:100%;
}
header{
background: #000;
text-align: center;
height:50px;
line-height: 50px;
color:#fff;
}
main{
padding-bottom:100px;
background:#ccc;
text-align: center;
}
footer{
color:#fff;
height:100px;
line-height:100px;
margin-top:-100px;
text-align:center;
background-color: #000;
}
此方法把footer之前的元素放在一个容器里面,形成了container和footer并列的结构:
首先,设置.container的高度至少充满整个屏幕;
其次,设置main(.container最后一个子元素)的padding-bottom值大于等于footer的height值;
最后,设置footer的height值和margin-top负值。
展示效果跟第一种是一样的,缺点跟第一种也是一样的。
方法三:footer高度任意+js
HTML代码:
<header>头部</header>
<main>中间内容</main>
<footer>底部信息</footer>
CSS代码:
*{
margin:;
padding:;
}
html{
height:100%;
}
body{
min-height:100%;
margin:;
padding:;
position:relative;
}
header{
background: #000;
text-align: center;
height:50px;
line-height: 50px;
color:#fff;
}
main{ /* main的padding-bottom值要等于或大于footer的height值 */
background:#ccc;
text-align: center;
}
footer{
color:#fff;
width:100%;
height:100px;
line-height:100px;
text-align:center;
background-color: #000;
}
/* 动态为footer添加类fixed-bottom */
.fixed-bottom {
position: fixed;
bottom:;
width:100%;
}
JS(jquery)代码:
$(function(){
function footerPosition(){
$("footer").removeClass("fixed-bottom");
var contentHeight = document.body.scrollHeight,//网页正文全文高度
winHeight = window.innerHeight;//可视窗口高度,不包括浏览器顶部工具栏
if(!(contentHeight > winHeight)){
//当网页正文高度小于可视窗口高度时,为footer添加类fixed-bottom
$("footer").addClass("fixed-bottom");
}
}
footerPosition();
$(window).resize(footerPosition);
});
常用的纯css实现footer sticker
CSS代码:
html, body, #sticker {height: 100%;}
body > #sticker {height: auto; min-height: 100%;}
#stickerCon {padding-bottom: 40px;}
#footer {margin-top:-40px; height: 40px; width: 100%; text-align: center; line-height: 40px; color: #ABA498; font-size: 12px; background: #fafafa; border-top:1px solid #E7E7E7;}
HTML代码:
<div id="sticker">
<div id="stickerCon"></div>
</div>
<div id="footer">footer</div>
footer固定在页面底部的实现方法总结的更多相关文章
- 让footer固定在页面底部(CSS-Sticky-Footer)
让footer固定在页面底部(CSS-Sticky-Footer) 这是一个让网站footer固定在浏览器(页面内容小于浏览器高度时)/页面底部的技巧.由HTML和CSS实现,没有令人讨厌的h ...
- HTML中footer固定在页面底部的若干种方法
<div class="header"><div class="main"></div></div> <d ...
- 在不适用fixed的前提下,当内容较少时footer固定在页面底部
使用css,参考国外的一个解决方法: http://ryanfait.com/resources/footer-stick-to-bottom-of-page/ How to use the CSS ...
- Footer固定在页面底部(CSS)
<style type="text/css"> #wapper{ position: relative; /*重要!保证footer是相对于wapper位置绝对*/ h ...
- 利用CSS使footer固定在页面底部
1.HTML基本结构 <!DOCTYPEhtml> <htmlxmlns="http://www.w3.org/1999/xhtml"> <headr ...
- 将HTML页面页脚固定在页面底部(多种方法实现)
当一个HTML页面中含有较少的内容时,Web页面的footer部分随着飘上来,处在页面的半腰中间,给视觉效果带来极大的影响,接下来为大家介绍下如何将页脚固定在页面底部,感兴趣的朋友可以了解下 作为一个 ...
- 如何将页脚固定在页面底部,4中方法 转载自:W3CPLUS
原博客地址:http://www.w3cplus.com/css/css-sticky-foot-at-bottom-of-the-page 作为一个Web的前端攻城师,在制作页面效果时肯定有碰到下面 ...
- 让footer固定在页面(视口)底部(CSS-Sticky-Footer)
让footer固定在页面(视口)底部(CSS-Sticky-Footer) 这是一个让网站footer固定在浏览器(页面内容小于浏览器高度时)/页面底部的技巧.由HTML和CSS实现,没有令人讨厌的h ...
- 【转载自W3CPLUS】如何将页脚固定在页面底部
该文章转载自:W3CPLUS 大漠的文章 http://www.w3cplus.com/css/css-sticky-foot-at-bottom-of-the-page 以下为全文 作为一个Web的 ...
随机推荐
- redis的set类型!!!!
一.概述 在Redis中,我们可以将Set类型看作为没有排序的字符集合,和List类型一样,我们也可以在该类型的数据值上执行添加.删除或判断某一元素是否存在等操作.需要说明的是,这些操作的时间复杂度为 ...
- OpenResty 在又拍云容器平台中的应用
大家好,我是又拍云叶靖,今天主要分享 OpenResty 在又拍云容器平台中的应用.目前又拍云有很多产品,其中很多都使用了 OpenResty 技术,比如又拍云的 CDN .网关都是基于 OpenRe ...
- revit二次开发addin文件
command命令: <?xml version="1.0" encoding="utf-8"?> <RevitAddIns> < ...
- C# 设置Excel数据自适应行高、列宽的2种情况
Excel表格中,由于各种数据的复杂性,可能存在单元格中的数据字号大小.数据内容长度不一而出现,列宽过宽.过窄或者行高过大.过小的问题.常见的解决方法是调整行高.列宽.在Microsoft Excel ...
- a标签伪类选择器以及伪元素:hover的案例
1.通过我们的观察发现a标签存在一定的状态1.1默认状态, 从未被访问过1.2被访问过的状态1.3鼠标长按状态1.4鼠标悬停在a标签上状态 2.什么是a标签的伪类选择器?a标签的伪类选择器是专门用来修 ...
- MsSql去掉重复记录
假如有这样一张表news:字段:id,title,time,image,author,现在表中有1万多条记录,其中title重复的有上千条.如何才能一次性将title重复记录删除呢? ID Title ...
- Accesss数据库的DBhelper类(带分页)
首先配置web.config,使配置文件连接access数据库: <connectionStrings> <add name="DBConnection" con ...
- 3. VIM 系列 - 遇见你的第一个插件
目录 1. 插件管理利器 vim-plug 1.1 安装插件管理器 1.2 配置插件管理器 1.3 安装插件 1.4 更新插件 1.5 回滚插件 1.6 卸载插件 1. 插件管理利器 vim-plug ...
- 基于PT的ipv6 ripng配置
在Cisco路由器上配置RIPng 如图规划(本人学号后三位056) 路由器配置(以R1为例) R1(配置接口地址) Router(config)#int fastEthernet 0/0 Route ...
- vue的基本指令
1.创建vue对象 <script src="vue.js"></script> var vm = new Vue({ el:"#ap ...