😄この投稿は、codingeverybody.jpのコンテンツをもとに作成しています。
CSSによる要素の水平方向の中央揃え
# 要素自体を中央揃えにする方法:ブロックレベル要素をmargin-left: autoとmargin-right: autoで中央揃えにする
以下は、代表的なブロックレベル要素である<div>要素を中央揃えにする方法です。<div>要素のdisplayプロパティの初期値はblockです。
---- HTML ----
<div>ブロックレベル要素</div>
---- CSS ----
div {
width: 150px;
height: 150px;
background-color: pink;
margin-left: auto;
margin-right: auto;
}
上記の例のように、ブロックレベル要素はmargin-left: auto;とmargin-right: auto;を使って中央揃えにすることができます。
以下は、インラインレベル要素のdisplayプロパティを変更してブロックレベル要素にし、要素自体を中央揃えにした例です。
---- HTML ----
<span>インラインレベル要素</span>
---- CSS ----
span {
display: block; /* span要素のdisplayの初期値はinlineであり、
インラインレベル要素をブロックレベル要素に変更しています */
width: 150px;
height: 150px;
background-color: pink;
margin-left: auto;
margin-right: auto;
}
上記の例のように、インラインレベル要素をブロックレベル要素に変更した後、margin-left: autoとmargin-right: autoを使って中央揃えにすることができます。
# 左右に余白がある親要素で中央揃えする方法:text-align: centerを使ってインラインレベルの子要素を中央揃えにする
---- HTML ----
<div class="container">
<span>インラインレベルの子要素</span>
</div>
---- CSS ----
.container {
background-color: orange;
text-align: center; /* 左右に余白がある親要素の横幅を基準に、
子要素のテキストや画像などのインラインレベルコンテンツを
中央揃えにします */
}
.container span {
background-color: pink;
}
# 左右に余白がある親要素で中央揃えする方法:display: flexとjustify-content: center、align-items: centerを使って、ブロックレベル要素やインラインレベルの子要素を中央揃えにする
---- HTML ----
<div class="container">
<div>ブロックレベルの子要素</div>
</div>
---- CSS ----
.container {
background-color: orange;
display: flex;
justify-content: center; /* 子要素(ブロックレベル要素、インラインレベル要素)を
横(水平)方向の中央に揃えます */
}
.container div {
width: 170px;
height: 170px;
background-color: pink;
}
CSSで要素を縦(垂直)方向に中央揃えにする方法
# 親要素にdisplay: flexとalign-items: centerを指定する
---- HTML ----
<div class="container">
<div>ブロックレベルの子要素</div>
</div>
---- CSS ----
.container {
background-color: orange;
height: 300px;
display: flex;
align-items: center; /* 子要素(ブロックレベル要素、インラインレベル要素)
を縦(垂直)方向の中央に揃えます */
}
.container div {
width: 170px;
height: 170px;
background-color: pink;
}
# 親要素と子要素にpositionとtransformプロパティを使用する方法
この方法は、ブロックレベル要素およびインラインレベル要素の両方に適用可能です。以下の例では、ブロックレベル要素である<div>要素を例にしています。
---- HTML ----
<div class="container">
<div>ブロックレベルの子要素</div>
</div>
---- CSS ----
.container {
background-color: orange;
height: 300px;
position: relative;
}
.container div {
width: 170px;
height: 170px;
background-color: pink;
position: absolute;
top: 50%;
transform: translateY(-50%);
}
CSSで要素を横(水平)および縦(垂直)方向に中央揃えにする方法
# 左右に余白がある親要素にdisplay: flex、justify-content: center、align-items: centerを指定する
この方法は、ブロックレベル要素およびインラインレベル要素の両方に適用可能です。以下の例では、ブロックレベル要素である<div>要素を例にしています。
---- HTML ----
<div class="container">
<div>ブロックレベルの子要素</div>
</div>
---- CSS ----
.container {
background-color: orange;
height: 300px;
display: flex;
justify-content: center; /* 子要素を横(水平)方向の中央に揃えます */
align-items: center; /* 子要素を縦(垂直)方向の中央に揃えます */
}
.container div {
width: 170px;
height: 170px;
background-color: pink;
}
# 親要素と子要素にpositionとtransformプロパティを使用する方法
---- HTML ----
<div class="container">
<div>ブロックレベルの子要素</div>
</div>
---- CSS ----
.container {
background-color: orange;
height: 300px;
position: relative;
}
.container div {
width: 170px;
height: 170px;
background-color: pink;
position: absolute;
top: 50%;
left: 50%;
transform: translateX(-50%) translateY(-50%);
}
参考資料