flex-grow, flex-shrink, flex-basis ã«ã¤ãã¦
flex-grow CSS ããããã£ã¯ãflex ã¢ã¤ãã ã® flex grow factor ãæå®ãã¾ããããã¯ãã¢ã¤ãã ã flex ã³ã³ããå ã®ã¹ãã¼ã¹ãã©ãã ãå æããããæå®ãã¾ãã
MDN: flex-grow
MDN ã®èª¬æãã·ã³ãã«ããã¦ããåãããªãã£ãã®ã§ãããå°ã詳ãã調ã¹ãå
容ãã¾ã¨ãã¦ããã¾ãã
説æä¸ã«åºã¦ããã¹ã¯ã·ã§ã®åä½ãµã³ãã«ã¯ä¸ã®è¨äºã«ããã¾ãã
ãã®è¨äºã¯ä¸ã®åä½ãµã³ãã«è¨äºãé·ããªã£ããåé¢ããè£è¶³è¨äºã¿ãããªæãã§ãã
flexbox
ã®åºæ¬
flexbox ã®åºæ¬çãªåä½ã¯ä»¥ä¸ã®ãã¼ã¸ãå人çã«ã¯åãããããã£ãã®ã§ãªã¹ã¹ã¡ã§ãã
- CSS3 Flexbox ã®åããããã£ã®ä½¿ãæ¹ãã´ã£ã¸ã¥ã¢ã«ã§è©³ãã解説
flexbox ã®ä»æ§å ¨è¬ã«é¢ãã説æãåããããã - Flexboxã使ããªãç¥ã£ã¦ãããããflexã¢ã¤ãã ãã®å¹
ã®è¨ç®æ¹æ³
flex-grow ã«é¢ãã説æãåããããã
flex-grow
ãã flex ã¢ã¤ãã ãã flex ã³ã³ããå ã®ä»ã® flex ã¢ã¤ãã ã¨æ¯è¼ãã¦ãã©ã®ããã大ãããªããã¨ããããæ´æ°å¤ã§æå®ããã
W3C ã® flex grow
ã®èª¬æã«ã¯ããããã
This
component sets flex-grow longhand and specifies the flex grow factor, which determines how much the flex item will grow relative to the rest of the flex items in the flex container when positive free space is distributed. When omitted, it is set to 1.
W3C: flex-grow
æå³ã¨ãã¦ã¯ã
ãã®æ°å¤ã¯ããã¸ãã£ããªããªã¼ã¹ãã¼ã¹ãåé ãããæã«ã flex ã¢ã¤ãã ã flex ã³ã³ããå ã®ä»ã® flex ã¢ã¤ãã ã¨æ¯è¼ãã¦ã©ããããå¢å¤§ããããã決å®ãã¾ãã
ã¨ãããã¨ã ã¨æãã
flex-grow
ã¯ããã¾ã§ãä½ã£ãã¹ãã¼ã¹ãåã¢ã¤ãã ã§åãåãã
åé
ãããã®ã¯é åå
¨ä½ã§ã¯ãªããä½ã£ãã¹ãã¼ã¹ï¼ä»¥ä¸ãã¸ãã£ã´ãªããªã¼ã¹ãã¼ã¹ï¼ã
ããé åã1:2:3
ã®æ¯çã§åå²ãããã¨ãã¦ãåç´ã« flex-grow
ããããã1
,2
,3
ã¨æå®ãã¦ãä¸æããããªãã
ä¸ã¯ width
ã 60px
, 120px
, 180px
ã«æå®ãã display: block
ã®è¦ç´ ã
ä¸ã¯ flex-grow
ã 1
, 2
, 3
ã«æå®ãã display: flex
ã®è¦ç´ ã
ä¸ã¨ä¸ãè¦æ¯ã¹ãã¨ãä¸ã®æ¯çã1:2:3
ã«ãªã£ã¦ããªããã¨ãåããã
ä¸ã®å³ã«ã¤ãã¦ç´°ãè¦ã¦ããã¨ãä½ã£ãã¹ãã¼ã¹ãåã¢ã¤ãã ã«å¯¾ãã¦ã©ã®ããã«åé ããã¦ããã®ããåããã
flex ã³ã³ããã® width
ã¯360px
ã
flex ã¢ã¤ãã ã® width
㯠auto
ã flex ã¢ã¤ãã å
ã®ã³ã³ãã³ãã¯40px
ã® span
è¦ç´ ãªã®ã§ã flex ã¢ã¤ãã ã®å¹
ã¯40px
ã¨ãªãã
flex-grow
ãåãåããã¸ãã£ã´ãªããªã¼ã¹ãã¼ã¹ã¨ã¯ã flex ã³ã³ãããã flex ã¢ã¤ãã ã®å¹
ãå¼ããå¤ã360px - (40px + 40px + 40px) = 240px
ã®ãã¨ãæãã
ãã®240px
ã flex-grow
ã®å¤1:2:3
ã®æ¯çã§åé
ãã width
ã«è¶³ããåãããå¤ãæçµçãªå¹
ã¨ãªãã
(40 + 1/6 * 240) + (40 + 2/6 * 240) + (40 + 3/6 * 240)
80 + 120 + 160
ãã®ä¾ã§ã¯æ¯çã2:3:4
ã«ãªãã
flex-shrink
ãã flex ã¢ã¤ãã ãã flex ã³ã³ããå ã®ä»ã® flex ã¢ã¤ãã ã¨æ¯è¼ãã¦ãã©ã®ãããå°ãããªããã¨ããããæ´æ°å¤ã§æå®ããã
W3C ã® flex-shrink
ã®èª¬æã«ã¯ããããã
This
component sets flex-shrink longhand and specifies the flex shrink factor, which determines how much the flex item will shrink relative to the rest of the flex items in the flex container when negative free space is distributed.
W3C: flex-shrink
æå³ã¨ãã¦ã¯ã
ãã®æ°å¤ã¯ããã¬ãã£ããªããªã¼ã¹ãã¼ã¹ãåé ãããæã«ã flex ã¢ã¤ãã ã flex ã³ã³ããå ã®ä»ã® flex ã¢ã¤ãã ã¨æ¯è¼ãã¦ã©ãããã縮å°ããããã決å®ãã¾ãã
ã¨ãããã¨ã ã¨æãã
ãã¬ãã£ã´ãªããªã¼ã¹ãã¼ã¹ã¨ãããåããã«ããã
flex ã³ã³ããã®å¹
㨠flex ã¢ã¤ãã ã®å¹
ã®å·®ããã¸ãã£ã´ããã¬ãã£ã´ãã¨ãããµãã«èããã¨åããããããããããªãã
flex-grow
ã®æã¨éã®ç¶æ³ãèããã
flex ã¢ã¤ãã ã® width
ã 160px
ã«ãã¦ã flex ã³ã³ããããã¯ã¿åºãããã«ãã¦ã¿ãã
- flex ã¢ã¤ãã ãå
¨ã¦è¶³ããå¹
ã flex ã³ã³ããããå°ãã
360px - (40px + 40px + 40px) = 240px
= ãã¸ãã£ããªããªã¼ã¹ãã¼ã¹ã - flex ã¢ã¤ãã ãå
¨ã¦è¶³ããå¹
ã flex ã³ã³ãããã大ãã
360px - (160px + 160px + 160px) = -120px
= ãã¬ãã£ã´ãªããªã¼ã¹ãã¼ã¹
flex-shrink
ã¯ãã®ãã¬ãã£ã´ãªããªã¼ã¹ãã¼ã¹ãã©ãåé
ããããã¨ãããã¨ãæå®ããã
ä¸ã®ã¹ã¯ã·ã§ã¯ã
ä¸ã display: block
+ float: left
ã width: 120px
ã®è¦ç´ ã
ä¸ã flex-shrink
ã 1
,2
,3
ã«æå®ãã display: flex
ã®è¦ç´ ã¨ãªã£ã¦ããã
ãã¬ãã£ã´ãªããªã¼ã¹ãã¼ã¹ -120px
ãã flex-shrink
ã®å¤1:2:3
ã®æ¯çã§åé
ããã¨ã
(160 + 1/6 * -120) + (160 + 2/6 * -120) + (160 + 3/6 * -120)
140 + 120 + 100
flex ã¢ã¤ãã ã¯ãããã140px
ã120px
ã100px
ã¨ãªããæ¯çã¯7:6:5
ã¨ãªãã
flex-basis
flex-basis
ã«ã¤ãã¦ã¯ã¾ã ä»æ§ãä¸å®å®ãªå°è±¡ã§ãã
ã¨ãããã MDN ã®èª¬æãå¼ç¨ã
flex-basis CSS ããããã£ã¯ãflex ã¢ã¤ãã ã®åæ main size ã§ãã flex basis ãæå®ãã¾ããbox-sizing ã使ç¨ãã¦å¥ã«æå®ããã¦ããªãéãããã®ããããã£ã content-box ã®å¯¸æ³ãå®ç¾©ãã¾ãã
MDN: flex-basis
è¦ã¯ãã¼ã¹ã¨ãªã width
ã height
ãæå®ããã¨ãããã¨ã ã¨æãã
box-sizing
ã®å½±é¿ãåããã®ã§ã border
ã padding
ãå«ããå¤ãæå®ãããå ´å㯠box-sizing
ã border-box
ã«ããã
ï¼ãã ããIEã§ã¯ box-sizing
ã®æå®ã«é¢ãããã°æãã Flexbugs: 7. flex-basis doesn't account for box-sizing:border-boxï¼
æå®åºæ¥ãå¤ã¯ width
ã height
ã«è¨å®åºæ¥ãå¤ã§ããã°ä½ã§ãè¯ãã
ãããã¯ã content
ãæå®ããã
content
çµè«ããæ¸ãã¨ãåä½ããã©ã¦ã¶ã«ãã£ã¦ãã©ãã©ãªã®ã§ãç¾æ®µéã§ã¯ä½¿ããªãã»ããè¯ãããã§ãï¼2016å¹´9æ4æ¥ï¼ã
ã³ã³ãã³ããµã¤ãºã«å¿ãã¦ãããªã«ãã£ã¦ãããçãªè¨å®ãªããããªããã¼ãããã®ãã¨ããåããã¾ããã§ããã
W3C ã®èª¬æã
Indicates automatic sizing, based on the flex itemâs content.
ã¨ä¸è¡ã ãã§ãã
å®éã®åä½ãµã³ãã«ã«ã¤ãã¦ã¯å¥è¨äºã«è¿½è¨ãããã¾ãã
ã¡ã¢ãæã: Case Studies in Flexbox - flex-grow, flex-shrink, flex-basis
ãã©ã¦ã¶éã®å·®ç°ã®åå ã«ãªã£ã¦ããï¼2016å¹´9æ4æ¥ç¾å¨ï¼
flex-basis
㯠Chrome ã Firefox ã Edge ã°ã«ã¼ã㨠Safari ã IE11 ã°ã«ã¼ãã¨ã§åä½ãç°ãªãã
2017å¹´8æ17æ¥ç¾å¨ã¯ IE11 ã®ã¿åä½ãç°ãªãã
Chrome ã Firefox ã Edge ã¯ã³ã³ãã³ãã®ãµã¤ãºãç¶æã
Safari ã IE11 ã¯ã³ã³ãã³ããµã¤ãºãç¡è¦ããã
Chrome ã Firefox ã Safari ã Edge ã¯ã³ã³ãã³ãã®ãµã¤ãºãç¶æã
IE11 ã¯ã³ã³ãã³ããµã¤ãºãç¡è¦ããã
ãã¡ãã詳ããã¯å¥è¨äºã«è¨è¿°ãããã¾ãã
ã¡ã¢ãæã: Case Studies in Flexbox - flex-grow, flex-shrink, flex-basis
ãã®ä»ã®èæ ®ãã¹ããã¨
ããã¾ã§ã® flex-grow
ã flex-shrink
ã®åä½ã¯ãä½ã£ãããããã¯è¶³ããªãã¹ãã¼ã¹ã flex-grow
ã flex-shrink
ã«åºãã¦åé
ããã¨ãããããæå³ã·ã³ãã«ãªã«ã¼ã«ã§ãã
ããããå ´åã«ãã£ã¦ã¯æ§ã
ãªè¦å ã«ãã£ã¦çµæãäºæ³ãã«ãããªããã¨ãããã¾ãã
åè¨ç®ãå¿ è¦ã«ãªãå ´å
ããªã¼ã¹ãã¼ã¹ã®åé ãè¡ãããå¾ãä½ããã®åå ã«ãã£ã¦åã³ãã¬ãã£ãã¹ãã¼ã¹ãä½ã£ã¦ãã¾ã£ãå ´åãåè¨ç®ãè¡ããã¾ãã
0以ä¸ã«ãªãå ´å
flex
ã³ã³ããã®å¹
ã 360px
ã
flex
ã¢ã¤ãã ã® flex-shrink
ã 1
ã 2
ã 3
ã
width
ã 400px
ã
min-wditn
ã 0
ã«è¨å®ããã¦ããå ´åãä¸ã®ã¹ã¯ã·ã§ã®æ§ãªçµæã«ãªãã
é ã追ã£ã¦è¦ã¦ããã¨ã
flex
ã³ã³ããããflex
ã¢ã¤ãã ï¼ã¤ã®åè¨ãå¼ã
360 - (400 + 400 + 400) = -840
- ãã¬ãã£ããªããªã¼ã¹ãã¼ã¹
-840px
ãflex-shrink
ã«åºãã¦åé ãã
(400 - 1/6 * 840) + (400 - 2/6 * 840) + (400 - 3/6 * 840)
- ï¼ã¤ç®ã®
flex
ã¢ã¤ãã ã0
ãä¸åã£ã¦ãã¾ã
(260) + (120) + (-20)
- åã³çã¾ãããã¬ãã£ããªããªã¼ã¹ãã¼ã¹
-20px
ã®ååé ãè¡ããã
(260 - 1/3 * 20) + (120 - 2/3 * 20) + (0)
- æçµçãªãµã¤ãºã決å®ãã
253.3333... + 106.6666... + 0
ã³ã³ãã³ããµã¤ãºãä¸åãå ´å
Chrome ã Firefox ã Edge ã® flex-basis
ã®æåã«ããèµ·ããç¾è±¡ã
flex
ã³ã³ããã®å¹
ã 360px
ã
flex
ã¢ã¤ãã ã® flex-grow
ã 1
ã 5
ã 6
ã
flex-basis
ã 0
ã«è¨å®ããã¦ããå ´åãä¸ã®ã¹ã¯ã·ã§ã®æ§ãªçµæã«ãªãã
é ã追ã£ã¦è¦ã¦ããã¨ã
flex
ã³ã³ããããflex
ã¢ã¤ãã ï¼ã¤ã®åè¨ãå¼ã
360 - (0 + 0 + 0) = 360
- ãã¸ãã£ããªããªã¼ã¹ãã¼ã¹
360px
ãflex-grow
ã«åºãã¦åé ãã
(0 + 1/12 * 360) + (0 + 5/12 * 360) + (0 + 6/12 * 360)
- ï¼ã¤ç®ã®
flex
ã¢ã¤ãã ãã³ã³ãã³ããµã¤ãºã®40px
ãä¸åã£ã¦ãã¾ã
(30) + (150) + (180)
- ï¼ã¤ç®ã®
40px
ãé¤ãã320px
ãåã³ãã¸ãã£ããªããªã¼ã¹ãã¼ã¹ã¨ãã¦ååé ããã(40) + (0 + 5/11 * 320) + (180 + 6/11 * 320)
- æçµçãªãµã¤ãºã決å®ãã
40 + 145.4545... + 174.5454...
margin, paddingãä¸åãå ´å
margin
ã padding
㯠flex-grow
ã flex-shrink
ã«ãã£ã¦å¢å¤§ã縮å°ãããªããããããªã¼ã¹ãã¼ã¹ã®åé
çµæãããããä¸åãå ´åã¯ååé
ãè¡ãããã
flex
ã³ã³ããã®å¹
ã 360px
ã
flex
ã¢ã¤ãã ã® flex-shrink
ã 1
ã 2
ã 3
ã
width
ã 160px
ã
å·¦å³ã® padding
ã 40px
ã
min-width
ã 0
ã«è¨å®ããã¦ããå ´åãä¸ã®ã¹ã¯ã·ã§ã®æ§ãªçµæã«ãªãã
flex
ã³ã³ããããflex
ã¢ã¤ãã ï¼ã¤ã®åè¨ãå¼ã
360 - (240 + 240 + 240) = -360
- ãã¸ãã£ããªããªã¼ã¹ãã¼ã¹
360px
ãflex-grow
ã«åºãã¦åé ãã
(240 - 1/6 * 360) + (240 - 2/6 * 360) + (240 - 3/6 * 360)
- ï¼ã¤ç®ã®
flex
ã¢ã¤ãã ãå·¦å³ã®padding
ã®åè¨80px
ãä¸åã£ã¦ãã¾ã
(180) + (120) + (60)
- åã³çã¾ãããã¬ãã£ããªããªã¼ã¹ãã¼ã¹
-20px
ã®ååé ãè¡ããã
(180 - 1/3 * 20) + (120 - 2/3 * 20) + (80)
- æçµçãªãµã¤ãºã決å®ãã
173.3333... + 106.6666... + 80
ããã§ã解決ããªãå ´å
ååé ãã¦ã以ä¸ã®æ§ã«ã
0
ãä¸åãmin-width
ãä¸åã- margin, paddingãä¸åã
- ã³ã³ãã³ããµã¤ãºãä¸åã
ã«å½ã¦ã¯ã¾ãå ´åã¯ããµã¤ãºãä¸åããªãæ§ã«ã³ã³ããããã¯ã¿åºããåé ã¯ããã§çµããã¨ãªãã
flex
ã³ã³ããã®å¹
ã 360px
ã
flex
ã¢ã¤ãã ã® flex-shrink
ã 1
ã 2
ã 3
ã
width
ã 0px
ã
å·¦å³ã® padding
ã 70px
ã«è¨å®ããã¦ããå ´åãä¸ã®ã¹ã¯ã·ã§ã®æ§ãªçµæã«ãªãã
å·¦å³ã® padding
ã®åè¨ 140px
ãã縮å°ãããã¨ãåºæ¥ãªãããã flex ã³ã³ããããã¯ã¿åºãã