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

Centering - CSS challenges

Linda Hamilton
Release: 2024-11-05 16:20:02
Original
343 people have browsed it

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>
Copy after login
.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);
}
Copy after login

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>
Copy after login
.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);
}
Copy after login

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

source:dev.to
Statement of this Website
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
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template