css几种垂直居中

不论是实际项目中,还是面试,经常被面试官问到,css垂直居中有那些方法?今天特意整理一下,亲测有效

css几种垂直居中

通过verticle-align:middle实现CSS垂直居中

通过vertical-align:middle实现CSS垂直居中是最常使用的方法,但是有一点需要格外注意,vertical生效的前提是元素的display:inline-block。

  .S1{
      background-color:#efefef;
      height: 800px;
      line-height: 800px;
      text-align: center;
  }
  .S2{
      display: inline-block;
      width: 50px;
      height: 50px;
      vertical-align: middle;
      background-color: #f00;
  }

通过display:flex实现CSS垂直居中

随着越来越多浏览器兼容CSS中的flexbox特性,所以现在通过“display:flex”实现CSS水平居中的方案也越来越受青睐。
通过display:flex实现CSS垂直居中的方法是给父元素display:flex;而子元素align-self:center;
这个跟CSS水平居中的原理是一样的,只是在flex-direction上有所差别,一个是row(默认值),另外一个是column。

 .S1{
      background-color:#efefef;
      height: 800px;
      display: flex;
  }
  .S2{
    width: 50px;
    height: 50px;
    background-color: #f00;
    margin: auto;
    align-self: center;
  }

通过display:table-cell实现CSS垂直居中

给父元素display:table-cell,子元素display:inline-table的方式实现CSS垂直居中。

  .S1{
      background-color:#efefef;
      height: 800px;
      display: table-cell;
      vertical-align: middle;
      text-align: center;
      width: 600px;
  }
  .S2{
    width: 50px;
    height: 50px;
    background-color: #f00;
    display: inline-block;
  }

通过隐藏节点实现CSS垂直居中

创建一个隐藏节点#hide,使得隐藏节点的height值为剩余高度的一半即可。这种方法也适用于CSS水平居中,原理一样。

已知父元素高度通过transform实现CSS垂直居中

给子元素的position:relative,再通过translateY即可定位到垂直居中的位置

  .S1{
      background-color:#efefef;
      height: 800px;
  }
  .S2{
    width: 50px;
    height: 50px;
    background-color: #f00;
    position: relative;
    top:50%;
    margin: auto;
    transform: translateY(-50%);
     
  }

未知父元素高度通过transform实现CSS垂直居中。

先给父元素position:relative,再给子元素position:absolute,通过translateY即可定位到垂直居中的位置。

 .S1{
      background-color:#efefef;
      height: 800px;
      position: relative;

  }
  .S2{
    width: 50px;
    height: 50px;
    background-color: #f00;

    top:50%;
    margin: auto;
    transform: translateY(-50%); */
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
  }

通过line-height实现CSS垂直居中

设置子元素的line-height值等于父元素的height,这种方法适用于子元素为单行文本的情况。

 <!DOCTYPE html>
<html>
<head>
<style>
  .S1{
      background-color:#efefef;
      height: 800px;

      /* verticle-align:middle */
      /* line-height: 800px;
      text-align: center; */

      /* display:flex */
      /* display: flex; */

      /* display:table-cell */
      /* display: table-cell;
      vertical-align: middle;
      text-align: center;
      width: 600px; */

      position: relative;

  }
  .S2{
    width: 50px;
    height: 50px;
    background-color: #f00;

    /* verticle-align:middle */
    /* display: inline-block;
    vertical-align: middle; */

    /* display:flex */
    /* margin: auto;
    align-self: center; */

    /* display:table-cell */
    /* display: inline-block; */

    /* transform */
    /* position: relative;
    top:50%;
    margin: auto;
    transform: translateY(-50%); */
    position: absolute;
    top:50%;
    left: 50%;
    transform: translate(-50%,-50%);
     
  }
  
</style>
</head>

<body>
  <div class="S1">
    <!-- 三者都可以 -->
    <div class="S2"></div>
    <!-- <img class="S2" src="http://ehome-eas.stnts.com/shr/personPhoto/eas/10/eba80b5c0cffe058155864d2f786e8a8.jpg"> -->
    <!-- <span class="S2"></span> -->
  </div>
</body>
</html>