← マニュアル一覧に戻る

CSSで実装する上下左右中央表示

📅 2025年12月22日

パターン① Flexbox(王道)

✔ 特徴

.box {
  display: flex; /* “並べ方をFlexで管理します”という宣言 */
  justify-content: center; /* 横方向の中央 */
  align-items: center; /* 縦方向の中央 */
}

<div class="box flex-demo">
  <div class="item">中央</div>
</div>

パターン② position + transform(少々古い)

✔ 特徴

.box {
  position: relative;
}
.item {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
}

<div class="box flex-demo">
  <div class="item">中央</div>
</div>