<!-- 上下左右中央寄せ -->
<div class="box grid_vh">
<div class="box--item">
Gridで<br>上下左右中央
</div>
</div>
<!-- 子要素が複数ある場合 -->
<div class="box grid_v">
<div class="box--item -first">
要素1
</div>
<div class="box--item -second">
要素2
</div>
</div>
<!-- 子要素を横並びで左右中央寄せ -->
<div class="box grid_h">
<div class="box--item -first">
要素1
</div>
<div class="box--item -second">
要素2
</div>
</div>
.box {
display: grid;
}
/* ===============
上下左右中央寄せ
==================*/
.grid_vh {
place-content: center;
}
/* ===============
子要素が複数ある場合
==================*/
.grid_v {
place-content: center;
place-items: center;
/*
▼こっちでもOK▼
justify-content: center;
align-items: center;
*/
}
/* ===============
子要素を横並びで左右中央寄せ
==================*/
.grid_h {
place-content: center;
place-items: center;
grid-auto-flow: column;
}
/* ===============
装飾のためのCSS
==================*/
.box {
background-color: #efefef;
height: 200px;
}
.box + .box {
margin-top: 40px;
}
.box--item {
background-color: #2eb7cf;
color: #fff;
font-weight: bold;
padding: 20px;
}
.box--item.-second {
background-color: #95cf2e;
}
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.