原文:CSS + DIV 让页脚始终底部

一  前言

 经常设计页面时用到三层DIV,头DIV与脚DIV一般固定高度,而中间层DIV根据内容的多少,高度不定,我们经常希望但内容很少时,脚DIV保持在底部,当内容很多时,脚DIV被中间内容挤到下面,但仍然始终保持在底部。

二  position

  position有四个参数:static  | relative | absolute | fixed

  position:static,意味元素没有被定位,元素会出现在文档本该出现位置,是页面元素默认的定位的方式,一般无需指定,除非想要覆盖之前设置的定位。

  position:relative,很明白,相对元素本该位置的偏移量  

#nav{position:relative;top:15px;left:20px;
}    

  position:absolute,这时候元素已经脱离了文档,文档中已经没有自己的本该的位置了,但我们可以通过left、bottom、left和right来规定其在文档中位置。

#nav{postion:absolute;botton:0px;
}

  我们知道万物都是相对的,元素进行上面设置了后就保证nav元素始终保持在底部了呢?nav元素离botton为0px,是哪个为参照物呢,是父级元素还还是浏览器呢,其实这里分为两种情况:

  如果父级元素(父级元素的父级、父级的父级的父级......)设置postion时,则子元素此时相对的是父级的,所以当内容过多时,脚DIV不能被挤到底部去。

  如果父级元素(父级元素的父级、父级的父级的父级......)没有设置postion时,则子元素此时相对的是浏览器的,所以当内容过少时,脚DIV不能被调到底部去。

二  完整代码

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" /><title>CSS + DIV 让页脚始终底部</title><meta name="generator" content="" />
<style type="text/css">body{margin:0;padding:0;font:12px/1.5 tahoma,arial,'Hiragino Sans GB',\5b8b\4f53,sans-serif;position:absolute;width:100%;min-height:100%; }.content{padding-bottom: 54px;}a:link,a:visited,a:active{color:#00749E;text-decoration:none;}
a:hover{color:#000;text-decoration:underline;}#header{width:980px;height:69px;margin:0 auto;padding:0;}
#header h1.blogtitle{font-family:Arial, Helvetica, sans-serif;margin:0;padding:10px 0 0;}
#header p.desc{font-family:Verdana, Arial, Helvetica, sans-serif;font-size:11px;margin:0;padding:0;}
#top{background-color:#fff;background-image:url(images/topbck.gif);background-repeat:repeat-x;height:34px;}#ddnav{background-color:#312e2e;height:35px;margin:0;padding:0;}
#nav{height:25px;width:980px;font-weight:700;margin:0 auto;padding:10px 0 0;}
ul.nav{height:25px;line-height:25px;float:left;list-style:none;font-size:11px;text-transform:inherit;margin:0;padding:0;}
ul.nav li{float:left !important;list-style-type:none;border-right:2px solid #312e2e;margin:0;padding:0;}
ul.nav li a,ul.nav li a:link,ul.nav li a:visited{background:url(images/navsilver.gif) repeat-x;color:#818181;float:left;display:block;text-decoration:none;padding:0 15px;}
ul.nav li a:hover,ul.nav li a:active {background:url(images/navblue.gif) repeat-x;color:#205387;text-decoration:none;}
ul.nav li.current_page_item a{text-decoration:none;background:url(images/navblue.gif) repeat-x;color:#fff;}
ul.nav li ul{float:left;margin:0;padding:0;}#footer{background-image:url(images/footerbck.gif);background-repeat:repeat-x;clear:both;height:24px;margin:0;padding:0;position:absolute;bottom:30px;width:100%;}
#footerbox{color:#fff;background-color:#312e2e;height:30px;line-height:30px !important;margin:0;padding:0;clear:both;position:absolute;bottom:0px;width:100%;}
.footer{width:980px;color:#fff;height:30px;line-height:30px !important;margin:0 auto;padding:0;}
.footer a:hover{text-decoration:underline;color:#fff;border:none;}
.footer a:link,.footer a:active,.footer a:visited{text-decoration:underline;color:#25aacd;border:none;}#top{background-color:#fff;background-image:url(images/topbck.gif);background-repeat:repeat-x;height:34px;}</style><script type="text/javascript">
function aboutFunc(){window.document.getElementById("about").style.display="";
}function indexFunc(){window.document.getElementById("about").style.display="none";
}</script>
</head>
<body><div id="header"><h1 class="blogtitle"><a href="http://www.cnblogs.com/chenyuming507950417/">助你软件工作室</a></h1><p class="desc">二十八年,我用青春修行</p>
</div><div id="ddnav"><div id="nav"><ul class="nav"><li><a href="#" οnclick="indexFunc();">主页</a></li></ul><ul class="nav"><li><a href="#" οnclick="aboutFunc();">关于</a></li></ul></div>
</div><div id="top"></div><div class="content"><div id="post-e0a6ac53-c204-4919-9fda-2021397c40b8"><p><p class="MsoNormal" style="margin-bottom: 7.5pt; text-align: center; mso-pagination: widow-orphan;" align="center"><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">麦子,你是怎样长进我的眼睛</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">你站立的土地一直站着我的双脚</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">我们同根而生,是伙伴</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">只是我比你在泥土上住得更久</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">我一辈子是生活的奴隶,为了生计</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">一年一次,我用锋利的镰刀收割</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">你的麦芒对着天,从没刺伤我</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">为让我拿镰的手有更多的力气</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">你在一盘磨里走很长的路</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">把自己碾的粉身碎骨</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">我是在饥饿时学会了感恩</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">端起一碗饭就想你熟了的肤色</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">想起在泥土上来回行走的季节</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">麦子,你年年回来吧,我年年爱你</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"></span></strong></p></p></div><div id="about" style="display:none;"><p><p class="MsoNormal" style="margin-bottom: 7.5pt; text-align: center; mso-pagination: widow-orphan;" align="center"><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">麦子,你是怎样长进我的眼睛</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">你站立的土地一直站着我的双脚</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">我们同根而生,是伙伴</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">只是我比你在泥土上住得更久</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">我一辈子是生活的奴隶,为了生计</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">一年一次,我用锋利的镰刀收割</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">你的麦芒对着天,从没刺伤我</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">为让我拿镰的手有更多的力气</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">你在一盘磨里走很长的路</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">把自己碾的粉身碎骨</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">我是在饥饿时学会了感恩</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">端起一碗饭就想你熟了的肤色</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">想起在泥土上来回行走的季节</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"><br /> </span></strong><strong><span style="font-size: 14pt; font-family: 宋体;" lang="ZH">麦子,你年年回来吧,我年年爱你</span></strong><strong><span style="font-size: 14pt; font-family: Arial;" lang="EN-US"></span></strong></p></p></div></div><div id="footer">
</div>
<div id="footerbox"><div class="footer"><a href="http://www.cnblogs.com/chenyuming507950417/">助你软件工作室</a>&nbsp;&nbsp;&nbsp; 二十八年,我用青春修行&nbsp;&nbsp;&nbsp; &copy; Copyright2014</div>
</div><body>
</html>

二  参考文章

  (1)http://blog.davidqiu.com/post/2013-06-17/40051753968

  (2)http://www.cnblogs.com/ahjesus/archive/2011/11/26/2263986.html

CSS + DIV 让页脚始终底部相关推荐

  1. CSS + DIV 让页脚始终保持在页面底部

    来源:David's Blog     http://www.DavidQiu.com/ 文章链接:http://blog.davidqiu.com/post/2013-06-17/400517539 ...

  2. css设置h5页脚固定底部

    思路:将页面拆分为主体内容和页脚两大部分,给内容区域设置css样式:min-height: calc(100vh - 120px); <template><div class=&qu ...

  3. php架在底部页面,页脚始终保持在页面底部的网页布局方法

    页脚始终保持在页面底部的网页布局方法 导语:用CSS创建一个高度自适应布局,如何保证页脚(footer)在内容不超过一屏的情况下始终保持在布局最下方是一个比较头疼的事.下面就由百分网小编为大家介绍一下 ...

  4. 如何确保页脚始终位于页面底部?

    在尝试创建精美的网站时,防止页脚在页面上浮动非常重要.内容较短的页面可能会遇到此问题,但只需几行 CSS 即可轻松修复.假设您的 HTML 看起来像下面的代码片段,这里有两种现代方法可以确保页脚始终位 ...

  5. html页脚显示不出来,CSS实例:让页脚保持在未满屏页面的底部

    CSS实例:让页脚保持在未满屏页面的底部 互联网   发布时间:2008-10-17 19:20:27   作者:佚名   我要评论 在内容不超过一屏的情况下,当浏览器窗口变小那行页脚文字会跟着向上浮 ...

  6. CSS实现页脚始终在页面底部

    说明 最近在布局自己的博客系统,我是想练练手把时下比较流行的前后端技术串起来.同时,我会把设计和编码过程中遇到的问题或值得分享的技术点.实现方式做下总结,记录下来.本篇就是第一篇,个人能力有限,不足之 ...

  7. 在html用div怎样写页脚,使用div+CSS将页脚始终控制在页面最下方的方法

    tml和body的高度并不一定相同,在内容少的时候,body的高度要小于html,当然这只会出现在body中的内容所占的空间高度小于浏览器的视口高度的时候,此时html的高度大于body的高度.网页中 ...

  8. CSS:页脚紧贴底部

    2019独角兽企业重金招聘Python工程师标准>>> 我的练习来源于<CSS揭秘>这本书第7章-41紧贴底部的页脚这部分内容以及书中提到的链接. 方案一 描述:以下方案 ...

  9. css页脚怎么这只,纯css实现内容不足时页脚在底部

    当页面的内容较少时,你会发现页脚并没有在页面的底部.那么如何在内容不足时让页面始终在底部呢?你可能会说用fixed定位,只要设置bottom:0就会在底部了,那么此时当页面内容增多出现滚动条的时候,页 ...

最新文章

  1. 教你轻松恢复Linux口令
  2. 剑指offer十一:二进制中1的个数
  3. [objective-c] 04 - 消息机制 回调 目标-动作回调
  4. python数据处理框架_python 最快 web 框架 Sanci 快速入门
  5. 软件开发质量的双保险 — 3.应用设计验证与应用用例
  6. 【系统架构】如何解决热点数据更新问题
  7. 挑战iOS!小米全新OS发布:22款机型首发
  8. Oracle 11G R2(11.0.2.4.0) RAC打PSU(11.2.0.4.11.2.0.4.171017)
  9. 你就是北上广深的树先生
  10. Spring AOP动态代理的实现方式
  11. HttpServletResponse
  12. 京东商城开通英文网站 开拓西方市场
  13. ThinkPHP 微信支付及退款
  14. 电脑与树莓派与stm32f4串口通信
  15. C语言用数组编程矩阵,二维数组—矩阵求和(C语言)
  16. nvidia驱动版本查询
  17. 学习笔记(01):Web前端与HTML5移动开发系列一:HTML篇-06,HTML基本构成和语法
  18. mysql server 安装及配置_mysql server的安装和配置
  19. fluent p1模型_Fluent辐射传热模型理论以及相关设置(一)
  20. 提问的艺术!(转载)

热门文章

  1. MQTT——控制报文格式
  2. js中获取时间new date()的用法
  3. xhtml和css概述
  4. 进击的UI--------------------GETPOST
  5. C++设计模式 之 “组件协作”模式:Template Method、Strategy、Observer
  6. 动态绑定 dgvlist 列
  7. MemberShip驗證
  8. kali linux 安装 Mysql Can‘t read from messagefile 报错解决方案
  9. easy-mock本地部署成功,访问报错:EADDRNOTAVAIL 0.0.0.0:7300 解决方案
  10. Python学习之解决python下载第三方依赖速度慢的问题