# 两栏布局
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
color: #fff;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<div class="wrap">
<div class="left">left</div>
<div class="right">right</div>
</div>
1
2
3
4
2
3
4
# float + overflow:hidden
左侧栏左浮动,右侧内容是一个 BFC,BFC 不和浮动元素重叠;右侧内容宽度自适应
.wrap {
width: 90%;
height: 500px;
margin: 100px auto;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.left {
width: 300px;
height: 100%;
background-color: red;
float: left;
}
.right {
background-color: blue;
height: 100%;
overflow: hidden;
}
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
# 绝对定位 + margin 外边距 (浮动+margin 外边距)
.wrap {
width: 90%;
height: 500px;
position: relative;
margin: 100px auto;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.left {
width: 300px;
height: 100%;
position: absolute;
background-color: red;
}
.right {
margin-left: 300px;
background-color: blue;
height: 100%;
}
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
# flex 弹性布局
.wrap {
width: 90%;
height: 500px;
display: flex;
margin: 100px auto;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.left {
width: 300px;
height: 100%;
background-color: red;
}
.right {
display: flex;
flex: 1;
height: 100%;
background-color: blue;
}
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
# 三栏布局
<div class="wrap">
<div class="left">left</div>
<main>main</main>
<div class="right">right</div>
</div>
1
2
3
4
5
2
3
4
5
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
color: #fff;
}
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
# float
.wrap {
width: 90%;
height: 500px;
margin: 100px auto;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.left,
.right {
width: 300px;
height: 100%;
}
.left {
float: left;
background-color: red;
}
.right {
float: right;
background-color: blue;
}
main {
width: calc(100% - 600px);
height: 100%;
display: inline-block; /* 设置inline-block */
background-color: cornflowerblue;
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
# 定位 + margin 外边距
.wrap {
width: 90%;
height: 500px;
position: relative;
margin: 100px auto;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.left,
.right {
width: 300px;
height: 100%;
position: absolute;
top: 0;
}
.left {
left: 0;
background-color: red;
}
.right {
right: 0;
background-color: blue;
}
main {
height: 100%;
margin: 0 300px;
background-color: cornflowerblue;
}
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
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
# flex (代码省略)
更加优化的三栏布局是在结构上将内容区提高,优先显示
圣杯布局(利用父级的 padding) 双飞翼布局(利用 middle 的 margin)
# 圣杯布局
<div class="wrap">
<div class="middle">middle</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
1
2
3
4
5
2
3
4
5
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
color: #fff;
}
.wrap {
width: 90%;
height: 500px;
margin: 100px auto;
padding: 0 300px;
box-sizing: border-box;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.wrap > div {
float: left;
height: 100%;
position: relative;
}
.left,
.right {
width: 300px;
}
.left {
margin-left: -100%;
left: -300px;
background-color: red;
}
.right {
margin-left: -300px;
right: -300px;
background-color: blue;
}
.middle {
width: 100%;
background-color: cyan;
}
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
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
# 双飞翼布局
<div class="wrap">
<div class="middle-wrap">
<div class="middle">middle</div>
</div>
<div class="left">left</div>
<div class="right">right</div>
</div>
1
2
3
4
5
6
7
2
3
4
5
6
7
* {
margin: 0;
padding: 0;
}
html,
body {
height: 100%;
color: #fff;
}
.wrap {
width: 90%;
height: 500px;
margin: 100px auto;
border: 1px solid rgba(0, 0, 0, 0.5);
}
.wrap > div {
float: left;
height: 100%;
}
.left,
.right {
width: 300px;
}
.left {
margin-left: -100%;
background-color: red;
}
.right {
margin-left: -300px;
background-color: blue;
}
.middle-wrap {
width: 100%;
}
.middle {
height: 100%;
margin: 0 300px;
background-color: cyan;
}
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
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