CSS盒子垂直居中的六种方法:flex、定位、margin:auto、位移、计算、display: table-cell

way1:

flex布局:主轴垂直居中 + 侧轴垂直居中

  <style>

    * {

      padding: 0;

      margin: 0;

      box-sizing: border-box;

    }

    .bigBox {

      width: 300px;

      height: 300px;

      background-color: plum;

      margin: 100px auto 0;

      display: flex;

      justify-content: center;

      align-items: center;

    }

    .smallBox {

      width: 200px;

      height: 200px;

      background-color: yellowgreen;

    }

  </style>

<body>

  <div class="bigBox">

    <div class="smallBox"></div>

  </div>

</body>

way2:

父级:相对定位

子级:绝对定位 上下左右0 + margin:auto

  <style>

    * {

      padding: 0;

      margin: 0;

      box-sizing: border-box;

    }

   

.bigBox {

      width: 300px;

      height: 300px;

      background-color: plum;

      margin: 100px auto 0;

      position: relative;

    }

    .smallBox {

      width: 200px;

      height: 200px;

      background-color: yellowgreen;

      position: absolute;

      margin: auto;

      top: 0;

      bottom: 0;

      right: 0;

      left: 0;

    }

  </style>

way3: 

子级:上、左margin = ( 父盒子宽 / 高 - 盒子宽 / 高 ) 的50%(需要计算)

 <style>

    * {

      padding: 0;

      margin: 0;

      box-sizing: border-box;

    }

    .bigBox {

      width: 300px;

      height: 300px;

      background-color: plum;

      margin: 100px auto 0;

      overflow: hidden;

      /* padding-top: 50px; */

    }

    .smallBox {

      width: 200px;

      height: 200px;

      background-color: yellowgreen;

      margin-top: 50px;

      margin-left: 50px;

    }

  </style>

way4: 

父级:相对定位

子级:绝对定位+距离父盒子上左各50%,再往回移动自己宽高的50%(需要计算)

<style>

    * {

      padding: 0;

      margin: 0;

      box-sizing: border-box;

    }

    .bigBox {

      width: 300px;

      height: 300px;

      background-color: plum;

      margin: 100px auto 0;

      position: relative;

    }

    .smallBox {

      width: 200px;

      height: 200px;

      background-color: yellowgreen;

      position: absolute;

      top: 50%;

      left: 50%;

      margin-top: -100px;

      margin-left: -100px;

    }

  </style>

way5: 

父级:相对定位

子级:绝对定位+距离父盒子上左各50%,再x、y轴各移动自己的50%

(不需要计算,直接使用位移)

 <style>

    * {

      padding: 0;

      margin: 0;

      box-sizing: border-box;

    }

    .bigBox {

      width: 300px;

      height: 300px;

      background-color: plum;

      margin: 100px auto 0;

      position: relative;

    }

    .smallBox {

      width: 200px;

      height: 200px;

      background-color: yellowgreen;

      position: absolute;

      top: 50%;

      left: 50%;

      transform: translate(-50%, -50%);

    }

  </style>

way6: 

父级:display: table-cell + 基线对齐 + 文字居中对齐

子级:转行内块

注意:display:table-cell的属性很类似于td标签,并且对margin值是没有任何意义的

 <style>

    * {

      padding: 0;

      margin: 0;

      box-sizing: border-box;

    }

    .bigBox {

      width: 300px;

      height: 300px;

      background-color: plum;

      /* margin: 100px auto 0; */

      display: table-cell;

      vertical-align: middle;

      text-align: center;

    }

    .smallBox {

      width: 200px;

      height: 200px;

      background-color: yellowgreen;

      display: inline-block;

    }

  </style>

猜你喜欢

转载自blog.csdn.net/weixin_48082900/article/details/128953906