内容
Flexbox の使い方
display: flex
を指定する(または display: inline-flex
)。flex-direction
プロパティで主軸を指定する。justify-content
プロパティで指定する。align-content
, align-self / align-items
プロパティで指定する。参考
<div>
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
なし
<div class="flex1">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex1 {
display: flex;
}
<div class="flex2">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex2 {
display: inline-flex;
}
row
。参考
<div class="flex3">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex3 {
display: flex;
flex-direction: row-reverse;
}
<div class="flex4">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex4 {
display: flex;
flex-direction: column;
}
<div class="flex5">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex5 {
display: flex;
flex-direction: column-reverse;
}
参考
<div class="flex6">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex6 {
display: flex;
justify-content: center;
}
<div class="flex6a">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex6a {
display: flex;
justify-content: center;
flex-direction: column;
}
flex-direction: column;
で主軸がブロック方向になり、その主軸に対して justify-content: center;
が効く。つまり、縦方向に対しての center
位置となる。<div class="flex7">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex7 {
display: flex;
justify-content: left;
}
<div class="flex8">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex8 {
display: flex;
justify-content: space-between;
}
参考
<div class="flex9">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex9 {
display: flex;
align-items: flex-start;
}
<div class="flex10">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex10 {
display: flex;
align-items: center;
}
参考
<div class="flex11">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex11 {
display: flex;
}
.flex11 > div:nth-child(1) {
order: 3;
}
.flex11 > div:nth-child(2) {
order: 2;
}
.flex11 > div:nth-child(3) {
order: 1;
}
<div class="flex12">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex12 {
display: flex;
}
.flex12 > div:nth-child(1) {
order: 2;
}
.flex12 > div:nth-child(2) {
order: 3;
}
.flex12 > div:nth-child(3) {
order: 1;
}
参考
<div class="flex13">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex13 {
display: flex;
}
.flex13 > div:nth-child(1) {
flex-grow: 1;
}
.flex13 > div:nth-child(2) {
flex-grow: 1;
}
.flex13 > div:nth-child(3) {
flex-grow: 1;
}
<div class="flex14">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex14 {
display: flex;
}
.flex14 > div:nth-child(1) {
flex-grow: .1;
}
.flex14 > div:nth-child(2) {
flex-grow: .2;
}
.flex14 > div:nth-child(3) {
flex-grow: .3;
}
<div class="flex15">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex15 {
display: flex;
}
.flex15 > div:nth-child(1) {
flex-grow: 1;
}
.flex15 > div:nth-child(2) {
flex-grow: 2;
}
.flex15 > div:nth-child(3) {
flex-grow: 3;
}
<div class="flex16">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex16 {
display: flex;
}
.flex16 > div:nth-child(1) {
flex-grow: .3;
}
.flex16 > div:nth-child(2) {
flex-grow: .3;
}
.flex16 > div:nth-child(3) {
flex-grow: .6;
}
参考
<div class="flex18">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex18 {
display: flex;
width: 500px;
}
.flex18 div {
flex-basis: 200px;
}
.flex18 > div:nth-child(1) {
flex-shrink: 1;
}
.flex18 > div:nth-child(2) {
flex-shrink: 1;
}
.flex18 > div:nth-child(3) {
flex-shrink: 2;
}
参考
<div class="flex17">
<div>いち</div>
<div>にい</div>
<div>さん</div>
</div>
.flex17 {
display: flex;
flex-wrap: wrap;
}
参考
ソースコードはこちら