参考资料

  1. CSS3 阴影
  2. 有没有推荐的 CSS 过渡动画库?
  3. 边框属性详细说明以及案例
  4. CSS 阴影边框设置
  5. 动态伪类选择器详细说明以及案例
  6. flex-direction: row/column示例
  7. 动画属性详细说明以及案例
  8. 类选择器详细说明以及案例

水平居中

  1. 行内元素

    .parent {
      text-align: center;
    }
  2. 块级元素(固定宽度)

    .child {
      width: 200px;
      margin: 0 auto;
    }
  3. Flexbox

    .parent {
      display: flex;
      justify-content: center;
    }
  4. Grid

    .parent {
      display: grid;
      place-items: center;
    }

垂直居中

  1. 单行文本

    .parent {
      height: 100px;
      line-height: 100px;
    }
  2. Flexbox

    .parent {
      display: flex;
      align-items: center;
    }
  3. Grid

    .parent {
      display: grid;
      align-items: center;
    }
  4. 绝对定位(已知高度)

    .child {
      position: absolute;
      top: 50%;
      height: 50px;
      margin-top: -25px;
    }

水平垂直居中

  1. Flexbox

    .parent {
      display: flex;
      justify-content: center;
      align-items: center;
    }
  2. Grid

    .parent {
      display: grid;
      place-items: center;
    }
  3. 绝对定位 + Transform

    .child {
      position: absolute;
      top: 50%;
      left: 50%;
      transform: translate(-50%, -50%);
    }

完整示例

<!DOCTYPE html>
<html>
<head>
  <style>
    .flex-center {
      display: flex;
      justify-content: center;
      align-items: center;
      height: 200px;
      border: 1px solid #000;
    }
    .grid-center {
      display: grid;
      place-items: center;
      height: 200px;
      border: 1px solid #000;
    }
  </style>
</head>
<body>
  <div class="flex-center">
    <div>Flexbox 居中内容</div>
  </div>
  
  <div class="grid-center">
    <div>Grid 居中内容</div>
  </div>
</body>
</html>

声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。