LoginSignup
32
42

More than 5 years have passed since last update.

display:flex系をまとめる①(古いブラウザ対応用)

Last updated at Posted at 2017-11-27

flexってスマホサイトや最新のブラウザ対応だったらめちゃ便利だけど業務だとそうは言ってられない...
古いIE9~も対応せねばだったりAndroid2.3もサポートだったりというケースがあるのでまとめておく٩( 'ω' )و
IE8以下などはfloatやtableを検討するのもありかもね(´Д`。)

flexのサポート範囲
https://caniuse.com/#search=flex

基本形

まずは基本となるベースを作っておく(色とサイズは適当)
スクリーンショット 2017-11-27 18.42.25.png

html
<body>
  <div class="wrap">
    <div style="background-color: red;">1</div>
    <div style="background-color: blue;">2</div>
    <div style="background-color: green">3</div>
  </div><!-- /.wrap -->
</body>
css
.wrap div{
  width: 100px;
  height: 100px;
}

display: flex

親要素(.wrap)につけることで子要素の横並びレイアウトが簡単にできます!
スクリーンショット 2017-11-27 18.56.57.png

css
.wrap{
  display: -webkit-box;  //この辺が
  display: -webkit-flex; //Android2.3~とか
  display: -ms-flexbox; //IE9~用
  display: flex; //最新のブラウザのみであればflexだけでもよいかも
}

align-items

垂直方向の考え方
デフォルトは上揃え、左揃えになっている。
※白背景だとわかりにくかったため背景を黄色にしました。

flex-start(上揃え)

スクリーンショット 2017-11-27 19.19.43.png

css
    -webkit-box-align: start;
    -webkit-align-items: flex-start;
    -ms-flex-align: start;
    align-items: flex-start;

flex-end(下揃え)

スクリーンショット 2017-11-27 19.24.58.png

css
    -webkit-box-align: end;
    -webkit-align-items: flex-end;
    -ms-flex-align: end;
    align-items: flex-end;

center(中央寄せ)

スクリーンショット 2017-11-27 19.26.53.png

css
    -webkit-box-align: center;
    -webkit-align-items: center;
    -ms-flex-align: center;
    align-items: center;

baseline

スクリーンショット 2017-11-27 19.30.22.png
高さが異なる要素でも文字のベースラインを合わせることができる!

css
    -webkit-box-align: baseline;
    -webkit-align-items: baseline;
    -ms-flex-align: baseline;
    align-items: baseline;

justify-content

水平方向の考え方
デフォルトは左上揃えになっている。

flex-start(左揃え)

スクリーンショット 2017-11-27 19.40.24.png

css
    -webkit-box-pack: start;
    -webkit-justify-content: flex-start;
    -ms-flex-pack: start;
    justify-content: flex-start;

flex-end(右揃え)

スクリーンショット 2017-11-27 19.44.04.png

css
    -webkit-box-pack: end;
    -webkit-justify-content: flex-end;
    -ms-flex-pack: end;
    justify-content: flex-end;

center(中央揃え)

スクリーンショット 2017-11-27 19.45.32.png

css
    -webkit-box-pack: center;
    -webkit-justify-content: center;
    -ms-flex-pack: center;
    justify-content: center;

space-between(均等に間隔を空ける)

スクリーンショット 2017-11-27 19.47.42.png

css
    -webkit-box-pack: justify;
    -webkit-justify-content: space-between;
    -ms-flex-pack: justify;
    justify-content: space-between;

space-around(均等に間隔を空ける)

左右の端にも半分ずつ間隔が空いているver
スクリーンショット 2017-11-27 19.51.26.png

css
    -webkit-justify-content: space-around;
    -ms-flex-pack: distribute;
    justify-content: space-around;

長くなったので残りはdisplay:flex系をまとめる②にてまとめる(「゚ー゚)ドレドレ..

32
42
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
42