有时,页面内容太少,无法占满一屏的高度,底栏就会抬高到页面的中间。这时可以采用Flex布局,让底栏总是出现在页面的底部。这里有个“视口高度”的单位vh。“视口”是什么,其实也很好理解,在PC端,视口是指浏览器的可视区域,那视口高度的单位vh是怎么回事呢?其实也难理解。只要记住1vh=视口高度的1%。更简单的描述是,假如你的浏览器可视高度为1000px,那么1vh = 1000px / 100 = 10px,由此可见100vh就是你浏览器可视区域的最小高度啦。这里好有个细节,flex容器主轴的方向必须为垂直方向 flex-direction:column;,main元素自动占据视口高度剩余的空间,flex:1;。
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title>固定的底栏</title>
<style>
header{
width: 100%;
height: 50px;
background-color: honeydew;
}
#flex-container{
display: flex;
min-height: 100vh;
flex-direction: column;
}
#flex-container .site-content{
flex: 1;
}
footer{
width: 100%;
height: 100px;
background-color: black;
color: #fff;
text-align: center;
line-height: 100px;
}
</style>
</head>
<body>
<div id="flex-container">
<header>I am header</header>
<main class="site-content">
<h1>hello,I am main</h1>
</main>
<footer>粤ICP备案:238499</footer>
</div>
</body>
</html>