<div class="container">
<div class="left"></div>
<div class="center">float解决方案,DOM顺序与视觉顺序不符</div>
<div class="right"></div>
</div>
1
2
3
4
5
2
3
4
5
# 方法1 浮动布局(float+margin/padding)
.left {
width: 300px;
float: left;
height: 500px;
background: #FF79BC;
}
.center {
height: 500px;
font-size: 30px;
color: #fff;
background: #666;
margin: 0 300px;
}
.right {
width: 300px;
height: 500px;
float: right;
background: #CA8EFF;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 方法2 浮动布局(float+calc)
.left {
height: 500px;
width: 300px;
float: left;
background: cadetblue;
}
.center {
width: calc(100% - 600px);
height: 500px;
display: inline-block;
/* 将display: inline-block;改为float布局也可以 */
background: cornflowerblue;
}
.right {
height: 500px;
width: 300px;
float: right;
background: chocolate;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 方法3 绝对定位布局(absolute+margin)
.container {
height: 100%;
position: relative;
}
.left {
position: absolute;
top: 0;
left: 0;
width: 300px;
height: 100%;
background: cadetblue;
}
.right {
position: absolute;
top: 0;
right: 0;
width: 300px;
background: cornflowerblue;
height: 100%;
}
.center {
margin: 0 300px;
background: cornsilk;
height: 100%;
}
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
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
# 方法4 table布局
.container {
display: table;
width: 100%;
background: gray
}
.left,.center,.right {
display: table-cell;
height: 500px;
}
.left{
width:300px;
background: darkblue
}
.right{
width:300px;
background: darkgreen
}
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
# 方法5 flex布局
.container {
display: flex;
}
.left {
flex: 0 0 300px;
background: cornflowerblue;
}
.center {
flex: 1;
background: cadetblue;
}
.right {
flex: 0 0 300px;
background: cornsilk;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 方法6 grid布局
.container {
display: grid;
grid-template-columns: 300px auto 300px;
grid-template-rows: 500px;
}
.left {
background: cadetblue;
}
.right {
background: chocolate;
}
.center {
background: cornsilk;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
# 方法7 圣杯布局
圣杯布局时要注意中间和左边元素的位置要换一下。
<div class="container">
<div class="center">float解决方案,DOM顺序与视觉顺序不符</div>
<div class="left"></div>
<div class="right"></div>
</div>
1
2
3
4
5
2
3
4
5
.container {
padding: 0 300px;
overflow: hidden;
}
.left,
.right,
.center {
position: relative;
float: left;
min-height: 100px;
height: 500px;
}
.left {
right: 300px;
margin-left: -100%;
width: 300px;
background: red;
}
.center {
width: 100%;
background: #bfefff;
}
.right {
left: 300px;
margin-left: -300px;
width: 300px;
background: blue;
}
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
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