常見元素居中的五種方法

塊元素,且寬和高已知

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

在這裏插入圖片描述

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章