页面布局

页面布局

布局方式有哪几种

  1. 文档流布局
  2. 定位布局
  3. flex布局
  4. table布局
  5. 网格布局

水平居中

  1. margin: 0 auto
  2. 将子元素设置为行内元素,然后父元素设置 text-align: center
  3. 定位布局
    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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
.container{
width: 300px;
height: 200px;
background: pink;
position: relative;
}
.inner{
width: 100px;
height: 50px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
background: #fff;
text-align: center;
}
  1. 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;
    }

垂直居中

  1. 单行行内元素居中,只需要将子元素的行高等于高度就可以了
  2. table布局
  3. flex布局
    1
    2
    3
    4
    .box {
    display: flex;
    align-items: center;
    }