页面布局
布局方式有哪几种
- 文档流布局
- 定位布局
- flex布局
- table布局
- 网格布局
水平居中
- margin: 0 auto
- 将子元素设置为行内元素,然后父元素设置
text-align: center
。 - 定位布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17.container{
width: 300px;
height: 200px;
background: pink;
position: relative;
}
.inner{
width: 100px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
margin-top: -25px;
margin-left: -50px;
background: #fff;
text-align: center;
}
1 | .container{ |
- flex布局
1
2
3
4
5
6
7
8
9
10
11
12
13
14.container{
width: 250px;
height: 200px;
background: pink;
display: flex;
justify-content: center;
padding: 20px;
}
.inner{
background: #fff;
width: 50px;
height: 150px;
margin-left: 10px;
}
垂直居中
- 单行行内元素居中,只需要将子元素的行高等于高度就可以了
- table布局
- flex布局
1
2
3
4.box {
display: flex;
align-items: center;
}