Three-column Layout

I. 流体布局

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
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.left{
float: left;
height: 200px;
width: 100px;
background-color: red;
}
.right {
float: right;
height: 200px;
width: 100px;
background-color: blue;
}
.main {
margin: 50px 120px;
height: 200px;
background-color: #000;
}
/* 流体布局 */
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="right"></div>
<div class="main"></div>
</div>
</body>
</html>

缺点 : 主要内容无法最先加载,会影响用户体验。

II. BFC三栏布局

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
.left {
float: left;
width: 100px;
height: 200px;
margin-right: 20px;
background-color: red;
}

.right {
width: 200px;
height: 200px;
float: right;
margin-left: 20px;
background: blue;
}

.main {
height: 200px;
overflow: hidden;
background-color:green;
}
/* BFC布局,BFC区域,不会与浮动元素重叠 */

缺点同上。

III. 双飞翼布局

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>
<style>
.content {
float: left;
width: 100%;
}

.main {
height: 200px;
margin-left: 200px;
margin-right: 220px;
background-color: green;
}

.left {
float: left;
height: 200px;
width: 190px;
margin-left: -100%;
background-color: red;
}

.right {
width: 200px;
height: 200px;
float: right;
margin-left: -200px;
background-color: blue;
}

/* 双飞翼布局,主体内容可以优先加载,html代码稍复杂 */

</style>
</head>
<body>
<div class="content">
<div class="main"></div>
</div>
<div class="left"></div>
<div class="right"></div>
</body>
</html>

通过对margin-left赋负值,使得.left能够前于.main显示,.right能够后于.main显示。占用.content的空间。

IV. 圣杯布局

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>
<style>
.container {
margin-left: 200px;
margin-right: 220px;
}
.main {
float: left;
background-color: red;
height: 200px;
width: 100%;
}
.left {
float: left;
background-color: blue;
width: 180px;
margin-left: -100%;
height: 200px;
position: relative;
left: -200px;
}
.right {
float: right;
background-color: green;
width: 200px;
height: 200px;
position: relative;
right: -220px;
margin-left: -200px;
}

</style>
</head>
<body>
<div class="container">
<div class="main"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>
<!-- 圣杯布局 -->

V. 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>
<style>
.container {
display: flex;
}
.main {
flex-grow: 1;
height: 300px;
background-color: red;
}
.left {
order: -1;
flex: 0 1 200px;
margin-right: 20px;
height: 300px;
background-color:blue;
}
.right {
flex: 0 1 100px;
margin-left: 20px;
height: 300px;
background-color: green;
}
</style>
</head>
<body>
<div class="container">
<div class="main"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>

VI. Table布局

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>
<style>
.container {
display: table;
width: 100%;
}
.left, .main, .right {
display: table-cell;
}
.left {
width: 200px;
height: 300px;
background-color: red;
}
.main {
background-color: blue;
}
.right {
width: 100px;
height: 300px;
background-color: green;
}
</style>
</head>
<body>
<div class="container">
<div class="left"></div>
<div class="main"></div>
<div class="right"></div>
</div>
</body>
</html>
<!-- 无法设置栏间距 -->

VII. 绝对定位布局

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
<!DOCTYPE html>
<html lang="en">
<head>
<style>
.container {
position: relative;
}
.main {
height: 400px;
margin: 0 120px;
background-color: green;
}
.left {
position: absolute;
width: 100px;
height: 300px;
left: 0;
top: 0;
background-color: red;
}
.right {
position: absolute;
width: 100px;
height: 300px;
background-color: blue;
right: 0;
top: 0;
}
</style>
</head>
<body>
<div class="container">
<div class="main"></div>
<div class="left"></div>
<div class="right"></div>
</div>
</body>
</html>