CSS 居中

transform

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    #parent {
      width: 500px;
      height: 500px;
      background: lightgray;
      position: relative;
  }
    #child {
      background: white;
      width: 250px;
      height: 250px;
      position: absolute;
      top: 50%;
      transform: translate(50%, -50%);
    }
  </style>
</head>
<body>
  <div id="parent">
      <div id="child"></div>
  </div>
</body>
</html>

felx

<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title></title>
  <style>
    #parent {
      width: 500px;
      height: 500px;
      background: lightgray;
      display: flex;
      align-items: center;
      justify-content: center;
    }
    #child {
      background: white;
      width: 250px;
      height: 250px;
    }
  </style>
</head>
<body>
  <div id="parent">
      <div id="child"></div>
  </div>
</body>
</html>
    原文作者:TaoWu
    原文地址: https://segmentfault.com/a/1190000018861115
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞