方法一

  • 左边设置宽度,右边设为flex即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html lang="en">

<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>

<body>
<div class="left"></div>
<div class="right"></div>
</body>

</html>

<style>
body,
html {
height: 100%;
width: 100%;
}

.left {
width: 100px;
height: 100%;
background-color: red;
}

.right {
flex: 1; // 填满空间
background-color: blueviolet;
}
</style>

方法二

  • 左侧固定宽度左浮动,右侧设margin-left等于左侧宽度,右侧宽度通过calc来计算,或者直接设为auto
1
2
3
4
5
6
7
8
9
10
11
12
13
14
.left {
width: 100px;
height: 100%;
float: left;
background-color: red;
}

.right {
// width: auto;
width: calc(100% - 100px);
margin-left: 100px;
height: 100%;
background-color: blueviolet;
}

方法三

  • 利用浮动+BFC
  • 左边设为浮动,右边触发BFC,BFC区域不会与浮动元素重叠
1
2
3
4
5
6
7
8
9
10
11
12
.left {
width: 100px;
height: 100%;
float: left;
background-color: red;
}

.right {
overflow: hidden;
height: 100%;
background-color: blueviolet;
}

方法四

  • 利用绝对定位
  • 父元素设为相对定位
  • 子元素设为left为0,设置宽度,右边元素left设为左边元素的宽度,right设为0即可
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
body,
html {
height: 100%;
width: 100%;
position: relative;
}

.left {
position: absolute;
width: 100px;
height: 100%;
left: 0;
background-color: red;
}

.right {
position: absolute;
left: 100px;
right: 0;
width: auto;
height: 100%;
background-color: blueviolet;
}