首页 > web前端 > css教程 > 居中 - CSS 挑战

居中 - CSS 挑战

Linda Hamilton
发布: 2024-11-05 16:20:02
原创
358 人浏览过

Centering - CSS challenges

您可以在仓库 Github 上找到本文中的所有代码。

您可以在此处查看垂直中心 - CodeSandbox 和水平中心的视觉效果。


通过 CSS 居中


垂直居中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Centering</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <p class="center-by-absolute-margin">Centering</p>

    <p class="center-by-pseudo-class">Centering</p>

    <p class="center-by-line-height">Centering</p>

    <p class="center-by-table">Centering</p>

    <div class="center-by-flex">
      <p>Centering</p>
    </div>

    <div class="center-by-grid">
      <p>Centering</p>
    </div>

    <div class="center-by-absolute-parent">
      <p class="center-by-absolute-child">Centering</p>
    </div>

    <p class="center-by-calc">Centering</p>
  </body>
</html>
登录后复制
.center-by-absolute-margin {
  position: absolute;
  top: 0;
  bottom: 0;
  margin: auto 0;
}

.center-by-pseudo-class::before {
  content: "";
  display: inline-block;
  height: 100%;
  vertical-align: middle;
}

.center-by-line-height {
  height: 200px;
  line-height: 200;
}

.center-by-table {
  display: table-cell;
  vertical-align: middle;
}

.center-by-flex {
  display: flex;
  align-items: center;
}

.center-by-grid {
  display: grid;
  align-items: center;
}

.center-by-absolute-parent {
  position: relative;
}

.center-by-absolute-child {
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
}

.center-by-calc {
  height: 70px;
  position: relative;
  top: calc(50% - 35px);
}
登录后复制

水平居中

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta http-equiv="X-UA-Compatible" content="ie=edge" />
    <title>Centering</title>
    <link rel="stylesheet" href="styles.css" />
  </head>
  <body>
    <p class="center-by-fixed-width">Centering</p>

    <p class="center-by-text-align">Centering</p>

    <div class="center-by-unfixed-width">
      <span>Centering</span>
    </div>

    <p class="center-by-table">Centering</p>

    <div class="center-by-flex">
      <p>Centering</p>
    </div>

    <div class="center-by-grid">
      <p>Centering</p>
    </div>

    <div class="center-by-absolute-parent">
      <p class="center-by-absolute-child">Centering</p>
    </div>

    <p class="center-by-calc">Centering</p>
  </body>
</html>
登录后复制
.center-by-fixed-width {
  width: 70px;
  margin: 0 auto;
}

.center-by-text-align {
  text-align: center;
}

.center-by-unfixed-width {
  text-align: center;
}

.center-by-table {
  display: table;
  margin: 0 auto;
}

.center-by-flex {
  display: flex;
  justify-content: center;
}

.center-by-grid {
  display: grid;
  justify-content: center;
}

.center-by-absolute-parent {
  position: relative;
}

.center-by-absolute-child {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
}

.center-by-calc {
  width: 70px;
  margin-left: calc(50% - 35px);
}
登录后复制

以上是居中 - CSS 挑战的详细内容。更多信息请关注PHP中文网其他相关文章!

来源:dev.to
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
作者最新文章
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板