实现垂直居中

113 阅读4分钟

HTML 代码如下

<div class="parent">
      <div class="child">
        一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </div>
    </div>

如果 .parent 的 height 不写

只需要 padding: 50px 0; 就能将 .child 垂直居中

.parent {
  border: 5px solid red;
}
.child {
  border: 5px solid yellow;
  padding: 50px 0;
}

image.png

如果 .parent 的 height 写死了

image.png

1、利用 flex 布局

.parent {
  border: 5px solid red;
  // 三行代码足矣
  display: flex;
  justify-content: center;
  align-items: center;
}
.child {
  border: 5px solid yellow;
}

2、absolute margin auto

绝对定位、定宽定高、四个方向都是0、margin:auto

.parent {
  border: 5px solid red;
  height: 800px;
  position: relative;
}
.child {
  border: 5px solid yellow;
  position: absolute;
  width: 100px;
  height: 200px;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

3、margin-top -50%

绝对定位、定宽定高、top left 50%、margin-top margin-left 设置为定宽定高的一半

.parent {
  border: 5px solid red;
  height: 800px;
  position: relative;
}
.child {
  border: 5px solid yellow;
  position: absolute;
  width: 100px;
  height: 200px;
  top: 50%;
  left: 50%;
  margin-top: -100px;
  margin-left: -50px;
}

4、translate -50%

绝对定位、top left 50%、 transform: translate(-50%, -50%)

.parent {
  border: 5px solid red;
  height: 800px;
  position: relative;
}
.child {
  border: 5px solid yellow;
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
} 

5、table自带功能

  <table class="parent">
    <tr>
      <td class="child">
      一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </td>
    </tr>
  </table>
.parent{
  border: 1px solid red;
  height: 600px;
}

.child{
  border: 1px solid green;
}

6、div 装成 table

  <div class="table">
      <div class="td">
        <div class="child">
          一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
        </div>
    </div>
  </div>
div.table{
  display: table;
  border: 1px solid red;
  height: 600px;
}

div.tr{
  display: table-row;
  border: 1px solid green;
}

div.td{
  display: table-cell;
  border: 1px solid blue;
  vertical-align: middle;
}
.child{
  border: 10px solid black;
}

7、100% 高度的 afrer before 加上 inline block

  <div class="parent">
    <span class=before></span><div class="child">
      一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
    </div><span class=after></span>
  </div>
.parent{
  border: 3px solid red;
  height: 600px;
  text-align: center;
}

.child{
  border: 3px solid black;
  display: inline-block;
  width: 300px;
  vertical-align: middle;
}

.parent .before{
  outline: 3px solid red;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}
.parent .after{
  outline: 3px solid red;
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

忠告:能不写 height 就千万别写 height。