在写页面是经常会遇到页面由于内容高度不够撑满而导致底部div(footer)无法稳定在底部,解决的方法:
- footer固定高度+绝对定位
思路:将footer的父级div的最小高度(min-height)设为100%;然后将footer设为绝对定位(position:abusolute);同时父级div要空出footer的高度(padding-bottom);
html 代码结构:
<div class="container">
<header class="header"></div>
<section class="section ">主体内容</section >
<footer class="footer ">底部导航</footer >
</div>
css代码结构:
*{
margin: 0;
padding: 0;
}
html,body{
height: 100%;
}
.container{
/*保证footer是根据container来进行绝对定位*/
position:relative;
width:100%;
min-height:100%;
/*该处的值为底部div的值,如果低于底部div的高度,将会遮盖住主体内容*/
padding-bottom:100px;
box-sizing:border-box;
}
header{
width: 100%;
height: 200px;
background: #999;
}
.main{
width: 100%;
height: 200px;
background: orange;
}
footer{
position:absolute;
width:100%;
height:100px;
bottom:0px;
}