常见元素居中的五种方法

块元素,且宽和高已知

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    html, body, .father {
      width: 100%;
      height: 100%;
    }
    .father {
      position: relative;
    }
    .child {
         /* 使用绝对定位 */
      position: absolute;
      left: 50%;
      top: 50%;
      /* 盒子宽和高的一半 */
      margin-left: -150px;
      margin-top: -150px;
      width: 300px;
      height: 300px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="child"></div>
  </div>
</body>
</html>

块元素,且宽和高未知

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    html, body, .father {
      width: 100%;
      height: 100%;
    }
    .father {
      position: relative;
    }
    .child {
      position: absolute;
      left: 50%;
      top: 50%;
      /* 偏移50% */
      transform: translateX(-50%) translateY(-50%);
      width: 300px;
      height: 300px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="child"></div>
  </div>
</body>
</html>

flex布局

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    html, body, .father {
      width: 100%;
      height: 100%;
    }
    .father {
      display: flex;
      justify-content: center;
      align-items: center;
    }
    .child {
      width: 300px;
      height: 300px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="child"></div>
  </div>
</body>
</html>

margin:auto

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    html, body, .father {
      width: 100%;
      height: 100%;
    }
    .father {
     position: relative;
    }
    .child {
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      left: 0;
      margin: auto;
      width: 300px;
      height: 300px;
      background-color: lightblue;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="child"></div>
  </div>
</body>
</html>

行内元素居中

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
  <style>
    * {
      margin: 0;
      padding: 0;
    }
    .father {
      width: 500px;
      height: 500px;
      background-color: #cccccc;
    }
    .father {
      text-align: center;
      line-height: 500px;
    }
    .child {
      font-size: 26px;
      color: #f40;
      display: inline-block;
    }
  </style>
</head>
<body>
  <div class="father">
    <div class="child">一朵菊花,一个小鬼</div>
  </div>
</body>
</html>

————————————————
版权声明:本文为CSDN博主「亦是木子也是雨」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_44162474/article/details/106626269

posted @ 2021-06-01 15:10  珂珂keo  阅读(193)  评论(0编辑  收藏  举报