css之左邊定寬右邊自適應
###html程式碼:<!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>Document</title>
</head>
<body>
<div class="left">111</div>
<div class="right">222</div>
</body>
</html>
複製程式碼
1.通過浮動實現
.left{
width: 300px;
height: 500px;
background: red;
float: left;
}
.right{
height: 500px;
background: blue;
}
複製程式碼
2. 通過定位實現
.left{
width: 300px;
height: 500px;
background: red;
position: absolute; // 固定定位也可以
}
.right{
height: 500px;
margin-left: 300px;
background: blue;
}
複製程式碼
3.通過calc實現
這種方法看起來比較笨重,但是它能夠保證右側的div是真正的寬頻,而不是獨佔一行。
.left{
width: 300px;
height: 500px;
background: red;
float: left;
}
.right{
height: 500px;
float: right; // 也可以inline-block
width: calc(100% - 300px);
background: blue;
}
複製程式碼
4.通過flex佈局實現
body {
display: flex;
}
.left {
height: 500px;
flex: 0 0 300px;
background: red;
}
.right {
height: 500px;
flex: 1;
background: blue;
}
複製程式碼
5.通過grid網格佈局實現
body {
display: grid;
grid-template-columns: 200px auto;
}
複製程式碼