CSS

css三列布局的实现

圣杯布局,双飞翼布局

Posted by AzirKxs on 2022-11-05
Estimated Reading Time 6 Minutes
Words 1.3k In Total
Viewed Times

CSS三列布局

css经典的三列布局,左右两列固定宽度,中间元素自适应的各种实现方式

定位实现

定位的缺点:当出现滚动条时,内容区在滚动条后边显示,而且内容区仍旧被压缩(不推荐使用)

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
36
37
38
39
40
41
42
43
<!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>定位实现</title>
<style>
*{
margin:0;
padding: 0;
}
body{
min-width: 600px;
}
.left,.right{
width: 200px;
height: 200px;
background: pink;
}
.middle{
height: 200px;
background-color: greenyellow;
margin: 0 200px;
}
.left{
position: absolute;
left: 0;
top: 0;
}
.right{
position: absolute;
right: 0;
top: 0;
}
</style>
</head>
<body>
<div class="left"></div>
<div class="middle"></div>
<div class="right"></div>
</body>
</html>

浮动实现

因为浮动脱离了文档流,所以middle一定要放在三列元素的最后面;

缺点:如果有文字出现,布局就会错乱,导致扩展性不好。

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
36
37
38
39
<!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>浮动实现</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
min-width: 600px;
}
.left{
float: left;
}
.right{
float: right;
}
.left,.right{
width: 200px;
height: 200px;
background-color: pink;
}
.middle{
height: 200px;
background-color: greenyellow;
}
</style>
</head>
<body>

<div class="left"></div>
<div class="right"></div>
<div class="middle"></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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<!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>圣杯布局</title>
<style>
*{
margin: 0;
padding: 0;
}
body{
min-width: 1440px;
}
.wrap{
margin: 0 auto;
width: 1040px;
padding: 0 200px;
}
.content{
width: 100%;
background-color: greenyellow;
}
.left,.right{
background-color: pink;
width: 200px;
}
.content,.left,.right{
float: left;
height: 200px;
}
.left{
position: relative;
left: -200px;
margin-left: -100%;
}
.right{
position: relative;
left: 200px;
margin-left: -200px;
}
</style>
</head>

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

</html>

双飞翼实现

圣杯布局的两侧首先是让外层包裹的容器padding腾出位置,然后通过设置外边距和自身的left定位到两侧,而双飞翼布局采用了在内容区域在设置一个容易通过设置该容器的margin腾出两侧的位置,两侧区域只需要设置外边距即可

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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<!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>双飞翼布局</title>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 1440px;
margin: 0 auto;
/* padding: 0 200px; */
}
.content,.left,.right{
float: left;
height: 200px;
}
.content{
width: 100%;
background-color: greenyellow;
}
.left,.right{
width: 200px;
background-color: rgb(255, 192, 203);
}
.left{
margin-left: -100%;
}
.right{
margin-left: -200px;
}
.inner{
margin: 0 200px;
height: 200px;
}
</style>
</head>
<body>
<div class="wrap">
<div class="content">
<div class="inner">content</div>
</div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

flex实现

有了flex布局,三列布局的实现再也不要那么繁琐了,两边定宽,中间内容自适应,然后用order调整顺序,既能保证自适应,又可以保证中间区域先加载

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
36
37
38
39
40
41
42
43
44
<!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>
<style>
*{
margin: 0;
padding: 0;
}
.wrap{
width: 1440px;
display: flex;
margin: 0 auto;
}
.left,.right{
height: 200px;
width: 200px;
background-color: pink;
}
.content{
height: 200px;
flex: 1;
background-color: greenyellow;
order: 1;
}
.left{
order: 0;
}
.right{
order:2;
}
</style>
</head>
<body>
<div class="wrap">
<div class="content"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

如果这篇文章对你有帮助,可以bilibili关注一波 ~ !此外,如果你觉得本人的文章侵犯了你的著作权,请联系我删除~谢谢!