基本構造
<div class="container">
<div class="item">1</div>
<div class="item">2</div>
<div class="item">3</div>
</div>
.container {
display: flex;
}
コンテナのプロパティ
flex-direction(主軸の方向)
.container {
flex-direction: row;
flex-direction: row-reverse;
flex-direction: column;
flex-direction: column-reverse;
}
flex-wrap(折り返し)
.container {
flex-wrap: nowrap;
flex-wrap: wrap;
flex-wrap: wrap-reverse;
}
justify-content(主軸の配置)
.container {
justify-content: flex-start;
justify-content: flex-end;
justify-content: center;
justify-content: space-between;
justify-content: space-around;
justify-content: space-evenly;
}
align-items(交差軸の配置)
.container {
align-items: stretch;
align-items: flex-start;
align-items: flex-end;
align-items: center;
align-items: baseline;
}
align-content(複数行の配置)
.container {
flex-wrap: wrap;
align-content: flex-start;
align-content: flex-end;
align-content: center;
align-content: space-between;
align-content: space-around;
align-content: stretch;
}
gap(アイテム間の余白)
.container {
gap: 20px;
row-gap: 20px;
column-gap: 10px;
gap: 20px 10px;
}
アイテムのプロパティ
flex(flex-grow, flex-shrink, flex-basis)
.item {
flex: 1;
flex: 1 1 auto;
flex: 0 0 200px;
}
.item {
flex-grow: 1;
flex-shrink: 0;
flex-basis: 200px;
}
align-self(個別の交差軸配置)
.item {
align-self: auto;
align-self: flex-start;
align-self: flex-end;
align-self: center;
align-self: baseline;
align-self: stretch;
}
order(並び順)
.item {
order: 0;
order: -1;
order: 1;
}
よく使うパターン
中央揃え
.container {
display: flex;
justify-content: center;
align-items: center;
min-height: 100vh;
}
ナビゲーション
.nav {
display: flex;
justify-content: space-between;
align-items: center;
}
.nav {
display: flex;
justify-content: flex-end;
gap: 20px;
}
カードグリッド
.cards {
display: flex;
flex-wrap: wrap;
gap: 20px;
}
.card {
flex: 1 1 300px;
}
サイドバーレイアウト
.layout {
display: flex;
}
.sidebar {
flex: 0 0 250px;
}
.main {
flex: 1;
}
フッターを下に固定
body {
display: flex;
flex-direction: column;
min-height: 100vh;
}
main {
flex: 1;
}
footer {
flex-shrink: 0;
}
等幅カラム
.columns {
display: flex;
}
.column {
flex: 1;
}
入力フォーム
.input-group {
display: flex;
}
.input-group input {
flex: 1;
}
.input-group button {
flex-shrink: 0;
}
アイコン + テキスト
.icon-text {
display: flex;
align-items: center;
gap: 8px;
}
メディアオブジェクト
.media {
display: flex;
gap: 16px;
}
.media-image {
flex-shrink: 0;
}
.media-body {
flex: 1;
}
ショートハンド
.container {
flex-flow: row wrap;
}
.item {
flex: 1 0 auto;
}
関連記事
← Back to list