如何实现垂直居中?

52 阅读4分钟

使用<table>

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

image.png

用100%高度的after 和before 加上inline-block

    <div class="parent">
      <div class="after"></div>   //这里的after与before可以用伪元素代替
      <div class="child">
        一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </div>
      <div class="before"></div>
    </div>
.parent {
  border: 1px solid red;
  height: 600px;
  text-align: center;
}

.child {
  border: 1px solid green;
  display: inline-block;
  width: 300px;
  vertical-align: middle;
}
.after {
  height: 100%;
  display: inline-block;
  outline: 3px solid red;
  vertical-align: middle;
}
.before {
  height: 100%;
  display: inline-block;
  outline: 3px solid red;
  vertical-align: middle;
}

image.png

改变display,将div装成table

    <div class="table">
      <div class="tr">
        <div class="td">
          一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
        </div>
      </div>
    </div>
.table {
  display: table;
  border: 1px solid red;
  height: 600px;
}
.tr {
  display: table-row;
  border: 1px solid red;
}
.td {
  display: table-cell;
  border: 1px solid red;
  vertical-align: middle;
}

image.png

使用position和负margin

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

.child {
  border: 1px solid green;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 300px;
  height: 100px;
  margin-top: -50px;
  margin-left: -150px;
}

image.png

使用positiontranslate

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

.child {
  border: 1px solid green;
  position: absolute;
  top: 50%;
  left: 50%;
  width: 300px;
  height: 100px;
  transform: translate(-50%, -50%);
}

image.png

margin auto

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

.child {
  border: 1px solid green;
  position: absolute;
  width: 300px;
  height: 200px;
  margin: auto;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
}

image.png

使用flex

    <div class="parent">
      <div class="child">
        一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字一串文字
      </div>
    </div>
.parent {
  border: 1px solid red;
  display: flex;
  height: 600px;
  align-items: center;
  justify-content: center;
}

.child {
  border: 1px solid green;
  width: 300px;
}

image.png