Home >Web Front-end >CSS Tutorial >Centering - CSS challenges

Centering - CSS challenges

Linda Hamilton
Linda HamiltonOriginal
2024-11-05 16:20:02380browse

Centering - CSS challenges

You can find all the code in this post at the repo Github.

You can check the visual here Vertical Center - CodeSandbox and Horizontal Center.


Centering via CSS


Vertical centering

<!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);
}

Horizontal centering

<!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);
}

The above is the detailed content of Centering - CSS challenges. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn