CSS 常用经典布局 - 三栏布局

效果展示

原始效果

最终效果

原始代码

html 部分

1
2
3
4
5
<div class="container">
<div class="left">left</div>
<div class="center">center</div>
<div class="right">right</div>
</div>

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
body {
min-width: 630px;
margin: 0;
}

.left,
.center,
.right {
height: 150px;
}

.left {
width: 100px;
background-color: #ffb5bf;
}

.center {
background-color: #94e8ff;
}

.right {
width: 200px;
background-color: #8990d5;
}

实现方法

flex 布局

1
2
3
4
5
6
7
.container {
display: flex;
}

.center {
flex: 1;
}

grid 布局

1
2
3
4
.container {
display: grid;
grid-template-columns: 100px 1fr 200px;
}

table 布局

1
2
3
4
5
6
7
8
9
10
.container {
width: 100%;
display: table;
}

.left,
.center,
.right {
display: table-cell;
}

定位布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
.container {
position: relative;
}

.left {
position: absolute;
left: 0;
}

.center {
position: absolute;
left: 100px;
right: 200px;
}

.right {
position: absolute;
right: 0;
}

浮动布局

1
2
3
4
5
<div class="container">
<div class="left">left</div>
<div class="right">right</div>
<div class="center">center</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
.container {
overflow: hidden;
}

.left {
float: left;
}

.right {
float: right;
}

圣杯布局

1
2
3
4
5
<div class="container">
<div class="center">center</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
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
.container {
overflow: hidden;
padding-left: 100px;
padding-right: 200px;
}

.center,
.left,
.right {
float: left;
}

.center {
width: 100%;
}

.left {
position: relative;
left: -100px;
margin-left: -100%;
}

.right {
position: relative;
right: -200px;
margin-left: -200px;
}

双飞翼布局

1
2
3
4
5
6
7
<div class="container">
<div class="center-container">
<div class="center">center</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
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
.container {
overflow: hidden;
}

.center-container,
.left,
.right {
float: left;
}

.center-container {
width: 100%;
}

.center-container .center {
margin-left: 100px;
margin-right: 200px;
}

.left {
margin-left: -100%;
}

.right {
margin-left: -200px;
}

CSS 常用经典布局 - 三栏布局
https://blog.xukaiyyds.cn/posts/1db87a14/
作者
xukaiyyds
发布于
2023年2月11日
许可协议