<!-- 上下左右中央寄せ -->
<div class="box poa_vh">
<div class="box--item">
positionとtransformで<br>上下左右中央
</div>
</div>
<!-- 上下中央寄せ -->
<div class="box poa_v">
<div class="box--item">
positionとtransformで<br>上下中央
</div>
</div>
<!-- 左右中央寄せ -->
<div class="box poa_h">
<div class="box--item">
positionとtransformで<br>左右中央
</div>
</div>
.box {
position: relative;
}
.box--item {
position: absolute;
width: fit-content; /* widthの指定は必須 */
height: fit-content; /* heightの指定は必須 */
}
/* ===============
上下左右中央寄せ
==================*/
.poa_vh .box--item {
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
}
/* ===============
上下中央寄せ
==================*/
.poa_v .box--item {
top: 50%;
transform: translateY(-50%);
}
/* ===============
左右中央寄せ
==================*/
.poa_h .box--item {
left: 50%;
transform: translateX(-50%);
}
/* ===============
装飾のためのCSS
==================*/
.box {
background-color: #efefef;
height: 200px;
}
.box + .box {
margin-top: 40px;
}
.box--item {
background-color: #2eb7cf;
color: #fff;
font-weight: bold;
padding: 20px;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.