<!-- margin+positionで上下左右中央寄せ -->
<div class="box absolute">
  <div class="box--item">
    positionで上下左右中央
  </div>
</div>

<!-- margin+flexで上下左右中央寄せ -->
<div class="box flex">
  <div class="box--item">
    flexで上下左右中央
  </div>
</div>
/* ===============
/* margin+positionで上下中央寄せ
==================*/
.absolute {
  position: relative;
}
.absolute .box--item {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  margin: auto;
  width: fit-content; /* widthの指定は必須 */
  height: fit-content; /* heightの指定は必須 */
}

/* ===============
margin+flexで上下中央寄せ
==================*/
.flex {
  display: flex;
}
.flex .box--item {
  margin: auto;
}


/* ===============
装飾のためのCSS
==================*/
.box {
  background-color: #efefef;
  height: 200px;
}
.box + .box {
  margin-top: 40px;
}
.box--item {
  background-color: #2eb7cf;
  color: #fff;
  font-weight: bold;
  padding: 20px;
}

External CSS

This Pen doesn't use any external CSS resources.

External JavaScript

This Pen doesn't use any external JavaScript resources.