Six methods of vertically centering CSS boxes: flex, positioning, margin:auto, displacement, calculation, display: table-cell

way1:

flex layout: main axis vertically centered + side axis vertically centered

  <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:

Parent: relative positioning

Child level: Absolute positioning, top, bottom, left, and right 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: 

Child level: top and left margin = 50% of (parent box width/height - box width/height) (need to be calculated)

 <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: 

Parent: relative positioning

Child level: absolute positioning + 50% from the upper left of the parent box, and then move back 50% of its own width and height (need to calculate)

<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: 

Parent: relative positioning

Child level: Absolute positioning + 50% from the upper left of the parent box, and then move 50% of itself on the x and y axes

(No need to calculate, use displacement directly)

 <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: 

Parent: display: table-cell + baseline alignment + text center alignment

Child: Wrapped Inline Block

Note: The display:table-cell attribute is very similar to the td tag, and has no meaning for the margin value

 <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>

Guess you like

Origin blog.csdn.net/weixin_48082900/article/details/128953906