フレックスボックスの配置制御
フレックスボックスの配置制御は、justify-content と align-items の2つの主要なプロパティによって行われます。 これらのプロパティを理解することで、アイテムを自在に配置できるようになります。
軸と配置の関係
フレックスボックスでは、配置を制御する際に**主軸(main axis)と交差軸(cross axis)**を意識することが重要です。
- justify-content: 主軸方向(通常は水平方向)のアイテム配置を制御
- align-items: 交差軸方向(通常は垂直方向)のアイテム配置を制御
justify-content: 主軸方向の配置
justify-content プロパティは、フレックスアイテムを主軸方向にどのように配置するかを指定します。 余剰スペースがある場合の分配方法を制御できます。
.flex-container {
display: flex;
justify-content: center; /* アイテムを中央に配置 */
}
justify-content の値
flex-start(デフォルト)
アイテムを主軸の開始位置に配置します。
.flex-container {
justify-content: flex-start;
}
flex-end
アイテムを主軸の終了位置に配置します。
.flex-container {
justify-content: flex-end;
}
center
アイテムを主軸の中央に配置します。
.flex-container {
justify-content: center;
}
space-between
最初のアイテムを開始位置に、最後のアイテムを終了位置に配置し、残りのアイテムを等間隔で配置します。
.flex-container {
justify-content: space-between;
}
space-around
各アイテムの周りに等しいスペースを配置します。アイテム間のスペースは、端のスペースの2倍になります。
.flex-container {
justify-content: space-around;
}
space-evenly
すべてのスペース(アイテム間、端)を等しく配置します。
.flex-container {
justify-content: space-evenly;
}
align-items: 交差軸方向の配置
align-items プロパティは、フレックスアイテムを交差軸方向にどのように配置するかを指定します。 アイテムの高さが異なる場合の配置方法を制御できます。
.flex-container {
display: flex;
align-items: center; /* アイテムを交差軸の中央に配置 */
}
align-items の値
stretch(デフォルト)
アイテムをコンテナの高さいっぱいに伸ばします。
.flex-container {
align-items: stretch;
}
(複数行)
flex-start
アイテムを交差軸の開始位置に配置します。
.flex-container {
align-items: flex-start;
}
(高さ異なる)
flex-end
アイテムを交差軸の終了位置に配置します。
.flex-container {
align-items: flex-end;
}
(高さ異なる)
center
アイテムを交差軸の中央に配置します。
.flex-container {
align-items: center;
}
(高さ異なる)
baseline
アイテムのベースライン(文字のベースライン)を揃えて配置します。
.flex-container {
align-items: baseline;
}
組み合わせの実例
justify-content と align-items を組み合わせることで、様々な配置パターンを実現できます。
完全中央配置
最も一般的な使用例の一つです。
.flex-container {
display: flex;
justify-content: center;
align-items: center;
height: 200px;
}
右下配置
.flex-container {
display: flex;
justify-content: flex-end;
align-items: flex-end;
height: 150px;
}
カードレイアウトの配置
.card-container {
display: flex;
justify-content: space-around;
align-items: stretch;
gap: 20px;
}
カード 1
カード 2
カード 3
flex-direction との関係
重要: justify-content と align-items の効果は、flex-direction の値によって変わります。
row の場合(デフォルト)
- justify-content: 水平方向(左右)の配置を制御
- align-items: 垂直方向(上下)の配置を制御
column の場合
- justify-content: 垂直方向(上下)の配置を制御
- align-items: 水平方向(左右)の配置を制御
column での justify-content
.flex-container {
display: flex;
flex-direction: column;
justify-content: center;
height: 200px;
}
column での align-items
.flex-container {
display: flex;
flex-direction: column;
align-items: center;
height: 200px;
}
実用的なレイアウトパターン
ヘッダーレイアウト
ロゴを左に、ナビゲーションを右に配置する一般的なパターン:
.header {
display: flex;
justify-content: space-between;
align-items: center;
padding: 10px 20px;
}
ロゴ
モーダルの中央配置
画面中央にモーダルを配置する場合:
.modal-overlay {
position: fixed;
top: 0;
left: 0;
width: 100%;
height: 100%;
display: flex;
justify-content: center;
align-items: center;
background-color: rgba(0, 0, 0, 0.5);
}
モーダルコンテンツ
フッター配置
コンテンツの量に関係なく、フッターをページ下部に固定:
.page-container {
min-height: 100vh;
display: flex;
flex-direction: column;
}
.main-content {
flex: 1;
}
.footer {
display: flex;
justify-content: center;
align-items: center;
padding: 20px;
background-color: #f5f5f5;
}
試してみよう
以下のインタラクティブなデモで、justify-content と align-items の組み合わせを試してみてください:
justify-content: flex-start
| align-items: stretch
(高さ異なる)
(小さめ)
各プロパティ値の一覧
すべての値を確認して、違いを理解しましょう:
justify-content の各値
align-items の各値
まとめ
- justify-content: 主軸方向(通常は水平)のアイテム配置を制御
- align-items: 交差軸方向(通常は垂直)のアイテム配置を制御
- flex-direction の値によって軸が変わることに注意
- 2つのプロパティを組み合わせることで、柔軟で実用的なレイアウトを実現
- 完全中央配置、ヘッダーレイアウト、モーダル配置など、実際のWebサイトでよく使われるパターンを習得
これらのプロパティを理解することで、従来のCSSでは複雑だったレイアウトを、シンプルで直感的なコードで実現できるようになります。