戻る

要素を中央揃え
要素を中央揃えにする設定方法について

display: flex; justify-content: center; align-items: center;


要素を中央揃えにするためのCSSの組み合わせです。
display: flex;
フレックスコンテナを作成し、子要素をフレックスアイテムとして扱えるようにします。
このプロパティは、要素をフレックスコンテナとして定義します。
フレックスコンテナとは、その子要素(フレックスアイテム)を柔軟に配置するためのコンテナのことです。
justify-content: center;
フレックスアイテムを水平方向の中央に配置します。
このプロパティは、フレックスコンテナ内のフレックスアイテムを水平方向(主軸方向)にどのように配置するかを定義します。
centerを指定すると、アイテムはコンテナの中央に配置されます。
align-items: center;
フレックスアイテムを垂直方向の中央に配置します。
このプロパティは、フレックスコンテナ内のフレックスアイテムを垂直方向(交差軸方向)にどのように配置するかを定義します。
centerを指定すると、アイテムはコンテナの中央に配置されます。

まとめ
この組み合わせは、要素をコンテナ内で綺麗に中央揃えにするために非常に便利で、レスポンシブデザインにも適しています。

サンプル(1)
「display:flex;justify-content:center;align-items:center;」を使用
1
サンプル(2)
指定なし
2
サンプル(3)
「text-align:center;」を使用
3
サンプル(4)
「align-content:center;」を使用
4
サンプル(5)
「text-align:center;align-content:center;」を使用
5
サンプル(6)
「display:table-cell;vertical-align:middle;text-align:center;」を使用
6

copy
サンプル(1)<br>
「display:flex;justify-content:center;align-items:center;」を使用<br>
<div style="background-color:seagreen;width: 50px;height: 50px;display:flex;justify-content:center;align-items:center;">0</div>
サンプル(2)<br>
指定なし<br>
<div style="background-color:darkseagreen;width: 50px;height: 50px;">1</div>
サンプル(3)<br>
「text-align:center;」を使用<br>
<div style="background-color:yellowgreen;width: 50px;height: 50px;text-align:center;">2</div>
サンプル(4)<br>
「align-content:center;」を使用<br>
<div style="background-color:cornflowerblue;width: 50px;height: 50px;align-content:center;">4</div>
サンプル(5)<br>
「text-align:center;align-content:center;」を使用<br>
<div style="background-color:lightsteelblue;width: 50px;height: 50px;text-align:center;align-content:center;">5</div>
サンプル(6)<br>
「display:table-cell;vertical-align:middle;text-align:center;」を使用<br>
<div style="background-color:lightblue;width: 50px;height: 50px;display:table-cell;vertical-align:middle;text-align:center;">6</div>

text-align:center;
要素の内容(テキストやインライン要素)を水平方向に中央揃えにするためのプロパティです。
主にインライン要素(テキスト、, など)やインラインブロック要素に対して使用されます。
ブロック要素に対して text-align: center; を適用した場合、要素自身は中央揃えになりませんが、要素内のインライン要素やテキストは中央揃えになります。

align-content:center;
要素の内容(テキストやインライン要素)を垂直方向に中央揃えにするためのプロパティです。
text-align:center;と組み合わせて使用すると縦横中央寄せになります。

display:table-cell;
以前によく使用されていた手法のようです。
「vertical-align:middle;」
「text-align:center;」
と組み合わせて縦横中央寄せにすることができます。



戻る
back

Center an element
How to set an element to be centered

display: flex; justify-content: center; align-items: center;


A combination of CSS to center an element.
display: flex;
Creates a flex container and allows child elements to be treated as flex items.
This property defines an element as a flex container.
A flex container is a container that flexibly positions its child elements (flex items).
justify-content: center;
Centers flex items horizontally.
This property defines how flex items in a flex container are aligned horizontally (along the main axis).
If you specify center, the item is centered in the container.
align-items: center;
Vertically center flex items. 
This property defines how flex items are aligned vertically (along the cross axis) in a flex container.
center aligns the items in the center of the container. 

Summary
This combination is very useful for neatly centering elements in a container, and it also works well for responsive design.

Sample (1)
Using "display:flex;justify-content:center;align-items:center;"
1
Sample (2)
No specification
2
Sample (3)
Using "text-align:center;"
3
Sample (4)
Using "align-content:center;"
4
Sample (5)
Using "text-align:center;align-content:center;"
5
Sample (6)
Using "display:table-cell;vertical-align:middle;text-align:center;"
6

copy
sample(1)<br>
Use "display:flex;justify-content:center;align-items:center;"<br>
<div style="background-color:seagreen;width: 50px;height: 50px;display:flex;justify-content:center;align-items:center;">0</div>
sample(2)<br>
Not specified<br>
<div style="background-color:darkseagreen;width: 50px;height: 50px;">1</div>
sample(3)<br>
Use "text-align:center;"<br>
<div style="background-color:yellowgreen;width: 50px;height: 50px;text-align:center;">2</div>
sample(4)<br>
Use "align-content:center;"<br>
<div style="background-color:cornflowerblue;width: 50px;height: 50px;align-content:center;">4</div>
sample(5)<br>
Use "text-align:center;align-content:center;"<br>
<div style="background-color:lightsteelblue;width: 50px;height: 50px;text-align:center;align-content:center;">5</div>
sample(6)<br>
Use "display:table-cell;vertical-align:middle;text-align:center;"<br>
<div style="background-color:lightblue;width: 50px;height: 50px;display:table-cell;vertical-align:middle;text-align:center;">6</div>

text-align:center;
This property is used to center the content of an element (text or inline elements) horizontally.
This is mainly used for inline elements (text, , , etc.) and inline block elements.
When text-align: center; is applied to a block element, the element itself is not centered, but the inline elements and text within the element are centered.

align-content:center;
This property is used to center the content of an element (text or inline elements) vertically.
When used in combination with text-align:center;, it is centered vertically and horizontally.

display:table-cell;
This seems to be a technique that was often used in the past.
You can combine this with "vertical-align:middle;"
"text-align:center;"
to center the text vertically and horizontally.



back



著作権情報
ホームページおよプリ等に掲載されている情報等については、いかなる保障もいたしません。
ホームページおよびアプリ等を通じて入手したいかなる情報も複製、販売、出版または使用させたり、
または公開したりすることはできません。
当方は、ホームペーよびアプリ利用したいかなる理由によっての障害等が発生しても、
その結果ホームページおよびアプリ等を利用された本人または他の第三者が被った損害について
一切の責任を負わないものとします。