登录
首页 >  文章 >  前端

居中 - CSS 挑战

来源:dev.to

时间:2024-11-29 16:42:41 121浏览 收藏

编程并不是一个机械性的工作,而是需要有思考,有创新的工作,语法是固定的,但解决问题的思路则是依靠人的思维,这就需要我们坚持学习和更新自己的知识。今天golang学习网就整理分享《居中 - CSS 挑战》,文章讲解的知识点主要包括,如果你对文章方面的知识点感兴趣,就不要错过golang学习网,在这可以对大家的知识积累有所帮助,助力开发能力的提升。

居中 - CSS 挑战

您可以在 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 挑战》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:dev.to 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>