用css布局页脚,固定页脚始终在最下面
2016-05-26 22:32
309 查看
有时候,我们用CSS创建一个高度自适应布局,如何保证页脚(footer)在内容不超过一屏的情况下始终保持在布局最下方是一个比较头疼的事。我看过一些利用绝对定位的例子,但总感觉不是那么完美,经过一下午的研究总结出一个利用负值外补丁的方法来实现这个效果的方法,兼容IE5.0+,Opera 8.5+,Firefox 1.5+。下面我们看步骤:
1、为了让浏览器识别高度100%我们需要先给 html 和 body 加上一个高度值,同时清除所有元素的 margin 和 padding。顺便提一下,经过我的测试,html 和 body 的 height: 100%; 等于整个浏览器窗口的总高度,无论内容是否超过一屏。而它们下一级子元素 height: 100%; 则等于第一屏的高度。如何,是不是有点不好理解?
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
2、因为上面提到的问题,所以为了让布局自适应高度,我们要加上 min-height: 100%;,虽然IE不支持这个属性但是IE的 height: 100%; 有同样的作用:
#wrapper {
min-height: 100%;
}
* html #wrapper {
height: 100%;
}
这样,一个最简单的最小高度满一屏的自适应布局就做好了。为了便于查看,我加了一些宽度和背景色修饰,如下:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
text-align: center;
font: 12px/1.4 Verdana, sans-serif;
background: #f00;
}
#wrapper {
width: 770px;
min-height: 100%;
background: #ccc;
margin: auto;
text-align: left;
}
* html #wrapper {
height: 100%;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jzxue.com-建站学</title>
<style type="text/css">
/*<![CDATA[*/
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
text-align: center;
font: 12px/1.4 Verdana, sans-serif;
background: #F00;
}
#wrapper {
width: 770px;
min-height: 100%;
background: #ccc;
margin: auto;
text-align: left;
}
* html #wrapper {
height: 100%;
}
#header {
background: Green;
height: 40px;
}
#sidebar {
float: left;
width: 200px;
background: Gray;
}
#content-box {
float: right;
width: 570px;
background: Olive;
}
#footer {
height: 50px;
background: Background;
width:770px;
margin: auto;
}
/*]]>*/
</style>
</head>
<body>
<div id="wrapper">
<div id="header">此处显示 id "header" 的内容</div>
<div id="content-box">此处显示 id "content-box" 的内容</div>
<div id="sidebar">此处显示 id "sidebar" 的内容</div>
</div>
<div id="footer">此处显示 id "footer" 的内容</div>
</body>
</html>
1、为了让浏览器识别高度100%我们需要先给 html 和 body 加上一个高度值,同时清除所有元素的 margin 和 padding。顺便提一下,经过我的测试,html 和 body 的 height: 100%; 等于整个浏览器窗口的总高度,无论内容是否超过一屏。而它们下一级子元素 height: 100%; 则等于第一屏的高度。如何,是不是有点不好理解?
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
}
2、因为上面提到的问题,所以为了让布局自适应高度,我们要加上 min-height: 100%;,虽然IE不支持这个属性但是IE的 height: 100%; 有同样的作用:
#wrapper {
min-height: 100%;
}
* html #wrapper {
height: 100%;
}
这样,一个最简单的最小高度满一屏的自适应布局就做好了。为了便于查看,我加了一些宽度和背景色修饰,如下:
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
text-align: center;
font: 12px/1.4 Verdana, sans-serif;
background: #f00;
}
#wrapper {
width: 770px;
min-height: 100%;
background: #ccc;
margin: auto;
text-align: left;
}
* html #wrapper {
height: 100%;
}
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>jzxue.com-建站学</title>
<style type="text/css">
/*<![CDATA[*/
* {
margin: 0;
padding: 0;
}
html, body {
height: 100%;
text-align: center;
font: 12px/1.4 Verdana, sans-serif;
background: #F00;
}
#wrapper {
width: 770px;
min-height: 100%;
background: #ccc;
margin: auto;
text-align: left;
}
* html #wrapper {
height: 100%;
}
#header {
background: Green;
height: 40px;
}
#sidebar {
float: left;
width: 200px;
background: Gray;
}
#content-box {
float: right;
width: 570px;
background: Olive;
}
#footer {
height: 50px;
background: Background;
width:770px;
margin: auto;
}
/*]]>*/
</style>
</head>
<body>
<div id="wrapper">
<div id="header">此处显示 id "header" 的内容</div>
<div id="content-box">此处显示 id "content-box" 的内容</div>
<div id="sidebar">此处显示 id "sidebar" 的内容</div>
</div>
<div id="footer">此处显示 id "footer" 的内容</div>
</body>
</html>
相关文章推荐
- 浏览器预览PHP文件时顶部出现空白影响布局分析原因及解决办法
- css布局(以两栏布局为例)
- CSS布局--使用margin-left负值布局
- css网页布局
- 响应式布局
- (转)理解块级格式化上下文——BFC
- 伸缩布局 — 打开布局天堂之门?
- 元素定位
- 【CSS】左右布局,自适应宽高
- CSS艺术之---负margin之美
- CSS float和absolute
- 父层与子层的 position 属性在不同组合下的情况
- css三列布局,最右侧div固定,其他两列自适应
- css整体缩放布局
- CSS中经典的双飞翼布局(
- 阿里巴巴_一道备受关注的前端开发笔试题
- CSS技巧荟萃:了解CSS页面布局和加载流程
- CSS中:before和:after选择器的用法
- CSS定位与布局
- 子div自适应父级元素剩余空间