LoginSignup
3
3

More than 1 year has passed since last update.

flex-shrinkについて

Last updated at Posted at 2016-03-03

flex-shrinkは、flexアイテムの縮み率を指定するプロパティ
下記のサンプルでは、spanがflexアイテムとなります。

sample code


<div class="flex-container">
  <span>1</span>
  <span>2</span>
  <span>3</span>
</div>


.flex-container{
  display: flex;
  flex-flow: row nowrap;

  width: 300px;
  height: 120px;

  border: 5px dotted black;
  box-sizing: border-box;
  margin-bottom: 20px;
}

span{
  /* flex-grow , flex-shrink , flex-basisをまとめて記述 */
  flex: 0 0 200px;
}

span:nth-child(1){ background-color: #FF99AA; }
span:nth-child(2){ background-color: #AAFF99; }
span:nth-child(3){ background-color: #99AAFF; }

はみ出る


.flex-container{
  flex-flow: row nowrap;
  width: 300px;
}

span{
  flex: 0 0 200px;
}

親要素(flexコンテナ)の幅は 300px で、
子要素(flexアイテム)の幅は 200px * 3 = 600pxなので

row_nowrap.png

はみ出ます

はみ出ないように子要素を縮小する


span{
  flex: 0 1 200px;
}

flex_shrink_1.png

綺麗に収まります。

要素がはみ出ない場合は flex-shrinkは機能しない

row_wrap.png

.flex-container{
  flex-flow: row wrap;
  width: 300px;
}

span{
  flex: 0 1 200px;
}

複数行が可能で子要素のサイズが親要素以下の場合、はみ出ないので縮小されません。
※ 子要素のサイズが親要素以上の場合、はみ出るので縮小されます。

transitionを使ったデモ

リンク

flex-growについて

3
3
0

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
3
3