方法 1
左边左浮动,右边设置 margin-left: 200px;
<style>
.left {
height: 400px;
width: 200px;
float: left;
background: red;
}
.right {
height: 400px;
margin-left: 200px;
background: yellow;
}
</style>
</head>
<body>
<div class="left">左</div>
<div class="right">右</div>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
方法 2
左边绝对定位,右边设置 margin-left: 200px;
<style>
.left {
height: 400px;
width: 200px;
position: absolute;
background: red;
}
.right {
height: 400px;
margin-left: 200px;
background: yellow;
}
</style>
</head>
<body>
<div class="left">左</div>
<div class="right">右</div>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
方法 3
左边绝对定位,右边设置绝对定位,left: 200px,width: calc(100% - 200px);
<style>
.left {
height: 400px;
width: 200px;
position: absolute;
background: red;
}
.right {
height: 400px;
left: 200px;
width: calc(100% - 200px);
position: absolute;
background: yellow;
}
</style>
</head>
<body>
<div class="left">左</div>
<div class="right">右</div>
</body>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
方法 4(flex 实现)
<!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>flex实现左定宽右自适应</title>
<style>
.container {
display: flex;
}
.left {
background: red;
// 0 0表示该元素不随着页面大小变化而变化
flex: 0 0 200px;
}
.right {
background: blue;
// 1 1表示该元素随着页面大小变化而变化
flex: 1 1 auto;
}
</style>
</head>
<body>
<div class="container">
<div class="left">左边</div>
<div class="right">右边</div>
</div>
</body>
</html>
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
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