CSSã§display: flex
ã使ç¨ããã¨ãã«ãflex: 1;
ã®è¨è¼ãè¦ããããã¨ãããããããã¾ãã
ãã®è¨äºã§ã¯ãä½æ°ãªãæ¸ãã¦ãã¾ããflex: 1;ãã«ã¤ãã¦è§£èª¬ãããã¨æãã¾ãã
flexã¨ã¯ï¼
ã¾ããã®å ´åã® flex ã¯ãdisplay: flex
ãæå®ããè¦ç´ å
ã«ããåè¦ç´ ã«æå®ããããããã£ã§ãã
display: flex
ã¯ãã¬ãã¯ã¹ããã¯ã¹ã¨å¼ã°ãã¦ãã¦ãããè¦ç´ ã«å®ç¾©ããã ãã§ããã®ç´ä¸ã®è¦ç´ ã並åã«ãªãã¹ã¿ã¤ã«ã§ãã
ã·ã³ãã«ãªå°å
¥ã§ããã°ãCSSã§display:flex
ã¨ããã¹ã¿ã¤ã«ãæå®ããã ãã§ããå·¦ãã並ã¹ããå³ãã並ã¹ãããåçã«ä¸¦ã¹ããã®ã«ã¹ã¿ãã¤ãºãå¯è½ã§ãã
flex:1ã¨ã¯ï¼
flex: 1ã¨ã¯ãflexããããã£ãä¸æ¬æå®ã§ãflex:1 1 0ãçç¥ãã¦æ¸ãããã®ã§ãã
ããããã®ããããã£ãç´°ããè¦ã¦ã¿ãã¨ãflex-grow: 1
ã¨flex-shrink: 1
ã¨flex-basis: 0
ã®çµã¿åããã§ãã
ï¼ç¨®é¡ã®ããããã£ãããããã確èªãã¦ããã¾ãã
flex-grow
flex-growã¯ã親è¦ç´ ã®flexã³ã³ããã®ä½ã£ã¦ããã¹ãã¼ã¹ããåè¦ç´ ã®flexã¢ã¤ãã ã«åé ãã¦ãflexã¢ã¤ãã ã伸ã°ãããããã£ã§ãã flex-growã®å¤ã¯æ´æ°å¤ã®ã¿ã§ãflexã¢ã¤ãã ã伸ã³ãæ¯çãæå®ãã¾ãã
ä¸è¨HTMLã¨CSSãä¾ã«ãã¦å°ã説æãã¾ãã
<div class="flex-container"> <div class="flex-item item1">ã¢ã¤ãã â </div> <div class="flex-item item2">ã¢ã¤ãã â¡</div> </div> .flex-container { display: flex; background-color: blue; } .flex-item { background-color: red; margin: 10px; padding: 50px 0; }
表示ããã¨ä¸è¨ã®ããã«æ¨ªä¸¦ã³ã«ãªãã¾ã
ã¢ã¤ãã â ã¨ã¢ã¤ãã â¡ã®å³å´ã¯ç©ºãã¹ãã¼ã¹ã«ãªãã¾ãã
ããã§ä¸è¨ã®CSSã追å ããã¨ã©ããªãã§ããããï¼
.item1 { flex-grow: 1; } .item2 { flex-grow: 2; }
表示ãããã¨ã¢ã¤ãã â ã¨ã¢ã¤ãã â¡ã§æ¨ªå¹ ãåã¾ãã¾ãã
ããã¯ãããããã®flexã¢ã¤ãã ã«ãflex-grow: 1;ãã¨ãflex-grow: 2;ããæå®ããã¦ããããã空ãã¹ãã¼ã¹ã3åå²ããã¾ãã ããã¦ã3åå²ããã空ãã¹ãã¼ã¹ããflex-growã§æå®ãããå²åã«å¿ãã¦ãåflexã¢ã¤ãã ã«åé ããã¾ãã
ä»åã®å ´åã«ã¯item1ã«ã¯ç©ºãã¹ãã¼ã¹ã®1/3ãitem2ã«ã¯2/3ãå²ãå½ã¦ããã¾ãã
flex-shrink
flex-shrink
ã¯ã親è¦ç´ ã®flexã³ã³ããããã¯ã¿åºããåãå
ã«ãåè¦ç´ ã®flexã¢ã¤ãã ã縮ããããããã£ã§ãã
flex-shrinkãå¤ã¯æ´æ°å¤ã®ã¿ã§ãflexã¢ã¤ãã ã縮ããæ¯çãæå®ãã¾ãã
ãã£ãããã³ã¼ããè¦ã¦ããã¾ãã flex-shrinkã0ã«æå®ããã¨ã縮ã¾ãã«ãªãªã¸ãã«ãµã¤ãºã§è¡¨ç¤ºããã¾ãã
.flex-container { display: flex; background-color: blue; } .flex-item { background-color: red; margin: 10px; padding: 50px 0; width: 1000px; } .item1 { flex-shrink: 0; } .item2 { flex-shrink: 0; }
ããã§ä¸è¨ã®ããã«item1ã ãflex-shrink: 1ãè¨å®ããã¨ã両æ¹ã¨ãæ å ã«åã¾ãããã«item1ã縮ã¿ã¾ãã
.item1 { flex-shrink: 1; } .item2 { flex-shrink: 0; }
flex-basis
flex-basis
ã¯ãflexã¢ã¤ãã ã®åºæºã¨ãªãå¹
ãè¨å®ããããããã£ã§ãã
flex-basisããããã£ã§ã¯ãwidthã¾ãã¯heightããããã£ã¨åãå¤ã使ç¨ã§ããpxãem ãªã©ã®åä½ä»ãã®æ°å¤ãã親è¦ç´ ã®flexã³ã³ããã«å¯¾ãããã¼ã»ã³ãã¼ã¸ãæå®ãã¾ãã ããã©ã«ã㯠auto ã§ãã³ã³ãã³ãã®å 容ã«å¿ãã¦èªåãµã¤ãºè¨å®ããã¾ãã
flex-basisã¯ãflex-growãflex-shrinkãã¤ãã¦ãªããã°ãwidthãheightããããã£ã¨åãã§ãã flexã¢ã¤ãã ã横並ã³ã®ã¨ãã« widthããããã£ã¨åãåä½ããã縦並ã³ã®æã«heightããããã£ã¨åãåä½ã«ãªãã¾ãã
ä¾ãã°ãä¸è¨ã®ãããªCSSãããã¨ãã¾ãã
.flex-container { display: flex; background-color:blue; } .flex-item { background-color:red; margin: 10px; padding: 50px 0; width: 300px; } .item1 { flex-basis: 150px; } .item2 { flex-basis: 100px; }
ããããã®flexã¢ã¤ãã ã¯ãflex-basisã§æå®ããæ¨ªå¹ ã«ãªã£ã¦ãã¾ãã flex-itemã¯ã©ã¹ã§ãwidth ãæå®ãã¦ãã¾ãããwidthããflex-basisã§æå®ãããã®ãåªå ããã¾ãã
ããã¾ã§ã§ããããã£ã«ã¤ãã¦èª¬æãã¾ããã ããã¦ãä»åã¯flex:1ã«ã¤ãã¦èª¬æãã¾ãããããã以å¤ã®ãã¿ã¼ã³ãè¨å®ã§ããã®ã§ãä¸è¨ã«ãã¿ã¼ã³ã®ä¸ä¾ãè¨è¼ãã¾ãã
/* åä½ããªãæ°å¤ã 1 ã¤æå®: flex-grow ãã®å ´å flex-basis 㯠0 ã¨çãããªã*/ flex: 2; /* å¹ ã¾ãã¯é«ãã 1 ã¤æå®: flex-basis */ flex: 10em; flex: 30%; flex: min-content; /* å¤ã 2 ã¤æå®: flex-grow | flex-basis */ flex: 1 30px; /* å¤ã 2 ã¤æå®: flex-grow | flex-shrink */ flex: 2 2; /* å¤ã 3 ã¤æå®: flex-grow | flex-shrink | flex-basis */ flex: 2 2 10%;