今天开发网页,发现一个问题,如果页面内容大于浏览器高度,底部的div就会位置偏移,要解决这个问题,有多种实现方法,现在我们使用flex布局。
页面结构:
<body>
<div class="content"></div>
<footer class="footer"></footer>
</body>
样式代码:
html {
height: 100%;
}
body {
height: 100%;
display: flex;
display: -webkit-flex;
flex-direction: column;
-webkit-flex-direction: column;
}
.content {
flex: 1;
-webkit-flex: 1;
width: 100%;
height: 500px; /*高度可由内容撑开*/
background-color: #ccc;
}
.footer {
width: 100%;
height: 50px;
background-color: #666;
}