首頁  >  文章  >  web前端  >  css中有哪些方法可以實現垂直居中

css中有哪些方法可以實現垂直居中

王林
王林轉載
2020-03-23 10:31:132630瀏覽

css中有哪些方法可以實現垂直居中

css實作垂直居中的方法如下:

1、利用line-height實作居中,這種方法適合純文字類別的;

<!-- css -->
<style>
.parents {
  height: 400px;
  line-height: 400px;
  width: 400px;
  border: 1px solid red;
  text-align: center;
}

.child {
  background-color: blue;
  color: #fff;
}
 </style>
</head>

<body>
<!-- html -->
<div class="parents">
   <span class="child">css布局,实现垂直居中</span>
</div>
</body>

效果:

css中有哪些方法可以實現垂直居中

(推薦教學:CSS教學

2、透過設定父容器相對定位,子級設定絕對定位,標籤透過margin實現自適應居中;

<!-- css -->
<style>
.parents {
  height: 400px;
  width: 400px;
  border: 1px solid red;
  position: relative;
}

.child {
  width: 200px;
  height: 100px;
  line-height: 100px;
  text-align: center;
  color: #fff;
  background-color: blue;
  /* 四个方向设置为0, 然后通过margin为auto自适应居中 */
  position: absolute;
  top: 0;
  right: 0;
  bottom: 0;
  left: 0;
  margin: auto;
}
 </style>
</head>

<body>
<!-- html -->
<div class="parents">
  <span class="child">css布局,实现垂直居中</span>
</div>
</body>

效果:

css中有哪些方法可以實現垂直居中

#3、彈性佈局flex 父級設定display: flex; 子級設定margin為auto實現自適應居中;

  <!-- css -->
  <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: flex;
    }

    .child {
      width: 200px;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #333;
      background-color: yellow;
      margin: auto;
  }
 </style>
</head>

<body>
 <!-- html -->
  <div class="parents">
    <span class="child">css布局,实现垂直居中</span>
  </div>
</body>

效果:

css中有哪些方法可以實現垂直居中

4、父級設定相對定位,子級設定絕對定位,並且透過位移transform實現;

  <!-- css -->
  <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      position: relative;
    }

    .child {
      width: 200px;
      height: 100px;
      line-height: 100px;
      text-align: center;
      color: #fff;
      background-color: green;
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child">css布局,实现垂直居中</span>
  </div>
</body>

效果:

css中有哪些方法可以實現垂直居中

5、父級設定彈性盒子,並設定彈性盒子相關屬性;

 <!-- css -->
 <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: flex;
      justify-content: center; /* 水平 */
      align-items: center; /* 垂直 */
    }

    .child {
      width: 200px;
      height: 100px;
      color: black;
      background-color: orange;
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child"></span>
  </div>
</body>

效果:

css中有哪些方法可以實現垂直居中

6、網格佈局,父級透過轉換成表格形式,然後子級設定行內或行內區塊實作。 (要注意的是:vertical-align: middle所使用的前提條件是內嵌元素以及display值為table-cell的元素)。

效果:

css中有哪些方法可以實現垂直居中

 <!-- css -->
 <style>
    .parents {
      height: 400px;
      width: 400px;
      border: 1px solid red;
      display: table-cell;
      text-align: center;
      vertical-align: middle;
    }

    .child {
      width: 200px;
      height: 100px;
      color: #fff;
      background-color: blue;
      display: inline-block; /* 子元素设置行内或行内块 */
    }
  </style>
</head>

<body>
  <!-- html -->
  <div class="parents">
    <span class="child"></span>
  </div>
</body>

相關影片教學推薦:css影片教學

以上是css中有哪些方法可以實現垂直居中的詳細內容。更多資訊請關注PHP中文網其他相關文章!

陳述:
本文轉載於:jb51.net。如有侵權,請聯絡admin@php.cn刪除