我們可以同時給元素設置top值和bottom值,這樣的話就能撐開盒子給盒子設置高度值,並且該高度是自適應的。
如下:
<!DOCTYPE html>
<html lang="zh-cn">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style>
body {
margin: 0;
padding: 0;
}
div {
margin: 0;
padding: 0;
width:100%;
position: absolute;
top: 0;
bottom: 20px;
background-color: red;
}
</style>
</head>
<body>
<div></div>
</body>
</html>