很有意思的布局方式,很实用,总有需求遇到说footer固定在页面下方,内容过长时跟着向下推送。
而sticky footer布局就是解决这个问题。这个名词很有意思,想自己记一下,就像是黏在底部的footer,自己想象着玩的哈
html
<body>
<!-- 注意点: wrap和footer是平级的,并且wrap要加clearfix清除浮动 -->
<div class="wrap clearfix">
<!-- 注意点: content的padding-bottom和footer的高度一样高 -->
<div class="content">
<p>我是内容</p>
</div>
</div>
<!-- 注意点: footer的margin-top的高度是footer的高度取负值 -->
<footer>我是底部</footer>
</body>
css
<style type="text/css">
body,html {
height: 100%;
}
* {
margin: 0;
padding: 0;
}
.wrap {
min-height: 100%;
}
.clearfix {
display: inline-block;
zoom: 1;
}
.clearfix:after {
content: '';
display: block;
height: 0px;
clear: both;
visibility: hidden;
}
.content {
padding-bottom: 80px;
}
footer {
position: relative;
clear: both;
height: 80px;
margin-top: -80px; /* footer高度的负值 */
background: deeppink;
}
</style>
第二种方法
使用flex布局,这个使用起来代码量少,但是可能有兼容性,使用时应有取舍。
html
<div class="box">
<!-- 注意点: wrap和footer是平级的 -->
<div class="wrap clearfix">
<!-- 注意点: content的padding-bottom和footer的高度一样高 -->
<div class="content">
<p>我是内容</p>
</div>
</div>
<!-- 注意点: footer的margin-top的高度是footer的高度取负值 -->
<footer>我是底部</footer>
</div>
css
.box {
height: 100%;
display: flex;
flex-direction: column;
}
.wrap {
flex: 1;
}
footer {
height: 60px; /* 或者flex:0 */
}