1. flexbox とは
flexbox とは CSS のプロパティ ( 記述 ) のひとつです。コンテンツの横並びなどが少ないソースで簡単に実現できます。
flexboxの詳しい説明:参考サイト
flexbox はできることが多く、またテーマ側の CSS 容量の関係もあり、記事作成でよく使うと想定されるものをピックアップして予め CSS を準備しています。
●準備している CSS
1 2 3 4 5 6 |
.flexbox,.flexbox2{display:flex;} .flex-1{flex:1;} .flex-2{flex:2;} .flex-center{justify-content:center;} .items-center{align-items:center;} .row-reverse{flex-direction:row-reverse;} |
テーマ側で準備していない CSS は各自で作成して追加してください。また、ユーザー様の便宜を図るために flexbox の情報を提供していますが、より詳細な設定や CSS 作成は【 サポート対象外 】となりますので予めご了承ください。
2. 応用記述
■ 基本
- → 記事装飾パーツ一覧 説明箇所
- レスポンシブ時 ( 画面横幅 666 px 以下のスマホ等 ) は横並びは解除されて自動的に縦並びになります
- 1 行目 flexbox → flexbox2 に変更するとレスポンシブ時でも横並びになります
- 2 / 3 行目 border を削除すると罫線は非表示になります
1 2 3 4 5 6 7 8 |
<div class="flexbox"> <div class="flex-1 border margin-t-5px margin-b-5px margin-r-5px"> 左側テキスト </div> <div class="flex-1 border margin-t-5px margin-b-5px margin-l-5px"> 右側テキスト </div> </div> |
左側テキスト
右側テキスト
■ 横幅が文字列幅 ( flex-none )
- 2 / 5 行目 flex-1 → flex-none に変更すると box 横幅が均等ではなく文字列の幅になります
- レスポンシブ時は画面横幅いっぱいに広がります
1 2 3 4 5 6 7 8 |
<div class="flexbox"> <div class="flex-none margin-t-5px margin-b-5px margin-r-5px"> 左側テキスト </div> <div class="flex-none margin-t-5px margin-b-5px margin-l-5px"> 右側テキスト </div> </div> |
左側テキスト
右側テキストテキスト
■ 横幅比率が 2 対 1 ( flex-2 )
- 2 行目 flex-1 → flex-2 に変更すると 左側 box 横幅が右側の 2 倍になります
1 2 3 4 5 6 7 8 |
<div class="flexbox"> <div class="flex-2 border margin-t-5px margin-b-5px margin-r-5px"> 左側テキスト </div> <div class="flex-1 border margin-t-5px margin-b-5px margin-l-5px"> 右側テキスト </div> </div> |
左側テキスト
右側テキスト
■ 垂直方向の中央揃え ( items-center )
- 1 行目 items-center を追加すると垂直方向の中央揃えになります
1 2 3 4 5 6 7 8 |
<div class="flexbox items-center"> <div class="flex-1 border margin-t-5px margin-b-5px margin-r-5px"> 左側テキスト </div> <div class="flex-1 border margin-t-5px margin-b-5px margin-l-5px"> 右側テキスト </div> </div> |
左側テキスト
右側テキスト
右側テキスト
右側テキスト
■ 左右逆並び ( row-reverse )
- 1 行目 row-reverse を追加すると逆並びになります
- レスポンシブ時は順並びに戻ります
1 2 3 4 5 6 7 8 |
<div class="flexbox row-reverse"> <div class="flex-1 border margin-t-5px margin-b-5px margin-l-5px"> 左側テキスト </div> <div class="flex-1 border margin-t-5px margin-b-5px margin-r-5px"> 右側テキスト </div> </div> |
左側テキスト
右側テキスト
■ 文字背景・蛍光マーカーを併用
- 2 / 5 行目 border → b-blue ( 文字背景の記述 ) に変更すると box に色をつけることができます
- m-blue ( 蛍光マーカーの記述 ) も併用できます
1 2 3 4 5 6 7 8 |
<div class="flexbox"> <div class="flex-1 b-blue margin-t-5px margin-b-5px margin-r-5px"> 左側テキスト </div> <div class="flex-1 m-blue margin-t-5px margin-b-5px margin-l-5px"> 右側テキスト </div> </div> |
左側テキスト
右側テキスト
■ 3 列横並び
- 次のように中の div 要素を 1 つ追加すると 3 列横並びにできます
1 2 3 4 5 6 7 8 9 10 11 |
<div class="flexbox"> <div class="flex-1 border margin-t-5px margin-b-5px margin-r-5px"> <p>左側テキスト</p> </div> <div class="flex-1 border margin-t-5px margin-b-5px"> <p>中央テキスト</p> </div> <div class="flex-1 border margin-t-5px margin-b-5px margin-l-5px"> <p>右側テキスト</p> </div> </div> |
左側テキスト
中央テキスト
右側テキスト
■ 1 列・文字列幅で水平方向の中央揃え
- 1 行目 flex-center を追加して中の div 要素を flex-none で 1 つにすると文字列の幅で水平方向の中央揃えになります
1 2 3 4 5 6 7 |
<div class="flexbox flex-center"> <div class="flex-none border"> <p>テキスト</p> <p>テキストテキスト</p> <p>テキストテキストテキスト</p> </div> </div> |
テキスト
テキストテキスト
テキストテキストテキスト
- 次のように中の div 要素をリストタグにすることもできます
1 2 3 4 5 6 7 |
<div class="flexbox flex-center"> <ul class="ul-02-main border"> <li>テキスト</li> <li>テキストテキスト</li> <li>テキストテキストテキスト</li> </ul> </div> |
- テキスト
- テキストテキスト
- テキストテキストテキスト