html页脚紧贴页面底部,CSS:页脚紧贴底部

偏执的太偏执、 2022-10-06 04:00 308阅读 0赞

我的练习来源于《CSS揭秘》这本书第7章-41紧贴底部的页脚这部分内容以及书中提到的链接。

方案一

描述:以下方案简单、干净、现代并且没有hack,适用于IE8+, Chrome, Firefox, Opera等浏览器;不需要使用任何标签包裹,因为它是基于可变的body高度;这个解决方案诞生于2012年初,今天也许应该使用更好的方案比如flexbox。

css紧贴底部的页脚

/*footer紧贴底部的主要代码*/

html{

position: relative;

min-height: 100%;

}

body{

margin: 0 0 100px;

}

footer{

position: absolute;

left: 0;

bottom: 0;

height: 50px;

width: 100%;

background-color: green;/*设置颜色方便查看footer位置*/

}

/*调整内容的高度以查看footer紧贴底部的效果*/

.content{

height: 100px;

}

方案二

描述:使用display的flex属性

css紧贴底部的页脚

/*footer紧贴底部的主要代码*/

body{

margin: 0;

display: flex;

min-height: 100vh;

flex-direction: column;

}

footer{

height: 50px;

width: 100%;

background-color: green;/*设置颜色方便查看footer位置*/

}

.content{

flex: 1;

}

方案三

描述:使用 calc()方法

css紧贴底部的页脚

/*footer紧贴底部的主要代码*/

body{

margin: 0;

}

footer{

height: 50px;

width: 100%;

background-color: green;/*设置颜色方便查看footer位置*/

}

.content{

min-height: calc(100vh - 50px);

}

这个div用来撑高度的

(未完待续……)

参考:

http://mystrd.at/modern-clean-css-sticky-footer/【Modern Clean CSS “Sticky Footer”】

https://philipwalton.github.io/solved-by-flexbox/demos/sticky-footer/【Sticky Footer】

http://blog.sina.com.cn/s/blog\_4c1e6a010102v6jp.html【vh是相对视口的高度】

发表评论

表情:
评论列表 (有 0 条评论,308人围观)

还没有评论,来说两句吧...

相关阅读

    相关 HTML 课程 | 建筑

    因此,我们已经完成了网站除页脚之外的所有部分的构建。那么,让我们看看最终的页脚是什么样子的: ![在这里插入图片描述][69cacc00630646a980bfeb235d

    相关 HTML始终固定在底部

    页脚固定在底部分两种情况考虑,一种是有滑动栏的情况,一种是没有的。后者比较简单,直接加上定位属性就是了,但是在页面内容是扩充整个页面以至于可以下滑浏览时,这时候单纯加上定位属