css各种布局
# css各种布局
# 圣杯布局
# 单列布局
# 1.1 水平居中
父元素 text-align:center; 子元素:inline-block;
- 优点:兼容性好;
- 不足:需要同时设置子元素和父元素
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 200px;
text-align: center;
border:1px solid #ccc;
background-color: #fff;
}
.child {
display: inline-block;
width: 300px;
height: 100px;
background-color: red;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 子元素 margin:0 auto;
- 优点:兼容性好
- 缺点:需要指定宽度
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 400px;
background: red;
}
.child {
margin: 0 auto;
width: 300px;
height: 100px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
# 父元素:relative;子元素:absolute;left:50%;margin-left:-宽度的一半
- 优点:兼容性好
- 缺点:需要知道子元素的宽度
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
position: relative;
top: 0;
left: 0;
width: 500px;
height: 400px;
background: red;
}
.child {
position: absolute;
top: 0;
left: 50%;
margin-left: -150px;
width: 300px;
height: 100px;
background: blue;
}
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
# 父元素:relative;子元素:absolute;left:50%;transform:translate(-50%,0);
- 优点:不需要知道子元素的宽度
- 缺点:兼容性差 (新时代的你们,现在新的浏览器支持了,放心用了)
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
position: relative;
top: 0;
left: 0;
width: 500px;
height: 400px;
background: red;
}
.child {
position: absolute;
top: 0;
left: 50%;
transform: translate(-50%, 0);
width: 300px;
height: 100px;
background: blue;
}
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
# 弹性盒子:父元素:display:flex;justify-content:center;
- 优点:简单
- 缺点:兼容性差 (新时代的你们,现在新的浏览器支持了,放心用了)
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
display: flex;
justify-content: center;
width: 500px;
height: 400px;
background: red;
}
.child {
width: 300px;
height: 100px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 1.2 垂直居中
# vertical-align:center;
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 400px;
background: red;
display: table-cell;
vertical-align: middle;
}
.child {
width: 300px;
height: 100px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 父元素:position:relative;子元素:positon:absolute;top:50%;transform:translate(0,-50%);
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
position: relative;
top: 0;
left: 0;
width: 500px;
height: 400px;
background: red;
}
.child {
position: absolute;
top: 50%;
left: 0;
transform: translate(0, -50%);
width: 300px;
height: 100px;
background: blue;
}
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
# 父元素:position:relative;子元素:positon:absolute;top:50%;margin-top:-子元素高度的一半;
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
position: relative;
top: 0;
left: 0;
width: 500px;
height: 400px;
background: red;
}
.child {
position: absolute;
top: 50%;
left: 0;
margin-top: -50px;
width: 300px;
height: 100px;
background: blue;
}
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
# 父元素:display:flex;align-items:center;
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 400px;
background: red;
display: flex;
align-items: center;
}
.child {
width: 300px;
height: 100px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 1.3 水平垂直居中
# 父元素:display:table-cell;vertical-align:middle;text-align:center;子元素;display:inline-block;
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 400px;
background: red;
display: table-cell;
vertical-align: middle;
text-align: center;
}
.child {
width: 300px;
height: 100px;
background: blue;
display: inline-block;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
2
3
4
5
6
7
8
9
10
11
12
13
14
# 父元素:position:relative;子元素:position:absolute?50%;left:50%;margin-left:宽度的一半;margin-top:高度的一半;或者 transform:translate(-50%,-50%);
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 400px;
background: red;
position: relative;
left: 0;
right: 0;
}
.child {
width: 300px;
height: 100px;
background: blue;
position: absolute;
left: 50%;
top: 50%;
transform: translate(-50%, -50%);
}
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
# 父元素{display:flex;justify-content:center;align-items:center;}
<div class="parent">
<div class="child"></div>
</div>
1
2
3
2
3
.parent {
width: 500px;
height: 400px;
background: red;
display: flex;
justify-content: center;
align-items: center;
}
.child {
width: 300px;
height: 100px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
2
3
4
5
6
7
8
9
10
11
12
13
# 2.多列布局
# 2.1 左侧定宽,右侧自适应 第一种
left{float:left;width:100px;} .right{margin-left:100px;}
<div class="left">left</div>
<div class="right">right</div>
1
2
2
* {
margin: 0;
padding: 0;
}
.left {
height: 400px;
width: 300px;
background: red;
float: left;
}
.right {
height: 400px;
margin-left: 300px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 2.1 左侧定宽,右侧自适应 第二种
<div class="parent">
<div class="left">
left
</div>
<div class="right-fix">
<div class="right">
right
</div>
</div>
</div>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
* {
margin: 0;
padding: 0;
}
.left {
width: 300px;
height: 400px;
float: left;
background: red;
}
.right-fix {
width: 100%;
margin-left: -300px;
float: right;
}
.right {
margin-left: 300px;
height: 400px;
background: blue;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
.left{width:宽度值;float:left;} .right{overflow:hidden;}
<div class="parent">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
/*设置overflow:hidden;创建BFC。根据BFC特性,BFC不会与float box 重叠。*/
* {
margin: 0;
padding: 0;
}
.left {
width: 300px;
height: 400px;
float: left;
background: red;
}
.right {
height: 400px;
background: blue;
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
table 实现
<div class="parent">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
* {
margin: 0;
padding: 0;
}
.parent {
display: table;
table-layout: fixed;
width: 100%;
}
.left {
width: 300px;
height: 400px;
background: red;
display: table-cell;
}
.right {
height: 400px;
background: blue;
display: table-cell;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
flex 实现
<div class="parent">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
* {
margin: 0;
padding: 0;
}
.parent {
display: flex;
width: 100%;
}
.left {
width: 300px;
height: 400px;
background: red;
}
.right {
height: 400px;
background: blue;
flex: 1;
}
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
# 2.2 右侧定宽左侧自适应
float margin 实现
<div class="parent">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
* {
margin: 0;
padding: 0;
}
.left {
width: 100%;
height: 400px;
background: red;
float: left;
margin-right: -300px;
}
.right {
height: 400px;
background: blue;
width: 300px;
float: right;
}
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
table 实现
<div class="parent">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
* {
margin: 0;
padding: 0;
}
.parent {
width: 100%;
display: table;
table-layout: fixed;
}
.left {
width: 100%;
height: 400px;
background: red;
display: table-cell;
}
.right {
height: 400px;
background: blue;
width: 300px;
display: table-cell;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
flex 实现
<div class="parent">
<div class="left">
left
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
* {
margin: 0;
padding: 0;
}
.parent {
width: 100%;
display: flex;
}
.left {
flex: 1;
background: red;
}
.right {
height: 400px;
background: blue;
width: 300px;
}
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
# 2.3 左边两列定宽,右侧自适应
float margin 实现
<div class="parent">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
* {
margin: 0;
padding: 0;
}
.parent {
width: 100%;
}
.left,
.center {
background: red;
float: left;
width: 300px;
height: 400px;
}
.center {
background: yellow;
}
.right {
height: 400px;
background: blue;
margin-left: 600px;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
float overflow 实现
<div class="parent">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
* {
margin: 0;
padding: 0;
}
.parent {
width: 100%;
}
.left,
.center {
background: red;
float: left;
width: 300px;
height: 400px;
}
.center {
background: yellow;
}
.right {
height: 400px;
background: blue;
overflow: hidden;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
table 实现
<div class="parent">
<div class="left">
left
</div>
<div class="center">
center
</div>
<div class="right">
right
</div>
</div>
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
* {
margin: 0;
padding: 0;
}
.parent {
width: 100%;
display: table;
table-layout: fixed;
}
.left,
.center {
background: red;
display: table-cell;
width: 300px;
height: 400px;
}
.center {
background: yellow;
}
.right {
height: 400px;
background: blue;
display: table-cell;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
编辑 (opens new window)