css3彈性盒子flex實現三欄佈局
如題:高度已知,左右欄寬度300px,中間自適應:
彈性盒子本身就是並排的,我們設定寬度即可。
用一個容器container包裹三欄,設定comtainer容器的display屬性為flex,左右欄設定寬度為300px,中間欄設定flex:1,這裡的1表示寬度比例,具體數值取決於其它盒子的flex值,由於這裡其它盒子寬度固定,所以中間欄會自動填充:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>三欄佈局</title>
</head>
<style type="text/css">
html*{
margin: 0;
padding: 0;
}
.container{
display: flex;
}
.left{
background-color: aqua;
width: 300px;
height: 100px;
}
.center{
height: 100px;
flex: 1;
background: #f296ff;
}
.right{
height: 100px;
background-color: #6ee28d;
width: 300px;
}
</style>
<body>
<!-- 已知高度,寫出三欄佈局,左右寬度300px,中間自適應-->
<div class="container">
<div class="left"></div>
<div class="center"></div>
<div class="right"></div>
</div>
</body>
</html>
效果如圖: