上下左右中央に配置する CSS に関する記録

4つ発見した。書きなぐりです。信頼性は低い。

display: table-cell なやつ

<div class="outer">
  <div class="inner">Hi, world.</div>
</div>

.outer {
  display: table;
}

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

transform なやつ

<div class="outer">
  <div class="inner">Hi, world.</div>
</div>

.outer {
  position: relative;
}

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

flexbox なやつ

<div class="outer">
  <div class="inner">Hi, world.</div>
</div>

.outer {
  display: flex;
  display: -webkit-flex;
  align-items: center;
  -webkit-align-items: center;
  justify-content: center;
  -webkit-justify-content: center;
}

.inner {
}

top: 0; bottom: 0; left: 0; right: 0; なやつ

<div class="outer">
  <div class="inner">Hi, world.</div>
</div>

.outer {
  width: XXXpx;
  height: XXXpx;
  position: relative;
}

.inner {
  width: XXXpx;
  height: XXXpx;
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
}

See the Pen dPrjZV by furu (@furu) on CodePen.