css - flexbox height adjust to content -


i use "full design" flexbox. have weird issue : have container takes remaining space , want in container child, flexbox, have height adjust content.

here issue:

body, html {    width:100%;    height:100%;    display:flex;  }    .container {    display:flex;    flex:1;    flex-wrap:wrap;  }    .icon {    width:10vh;    margin:10px;    display:flex;    flex-direction:column;  }  .img {    width:10vh;    height:10vh;    display:flex;    align-items:center;    justify-content:center;    background-color:red;  }    .text {    text-align:center;      }
<div class="container">  <div class="icon">  <div class="img">  </div>  <div class="text">  action 1  </div>  </div>  <div class="icon">  <div class="img">  </div>  <div class="text">  action 2  </div>  </div>    <div class="icon">  <div class="img">  </div>  <div class="text">  action 3  </div>  </div>    <div class="icon">  <div class="img">  </div>  <div class="text">  action 4  </div>  </div>    <div class="icon">  <div class="img">  </div>  <div class="text">  action 5  </div>  </div>    </div>

as can see, icon takes full height of container : in fact, don't want specify height because don't know text length , want that, if content huge, icon takes height of content ( don't want cut text). moreover, if page resized, want icon aligned (like on smartphone).

also, don't understand why icon takes height of parent , not content because didn't specify "flex:1" on it. assume default behaviour it's fit content size, seems not working.

thanks help.

image of issue

.icon's flex-column makes .img's stretch default unless .icon's have align-items. reason why didn't apply align-items .icon's because other nested flex-containers/flex-items started collapsing. instead of adjusting down through hierarchy, went , adjusted .container instead.

the relevant css:

.container {   display: flex;   flex: 1;         /* if remove .container shrink wrap around .icon's */   flex-wrap: wrap;   justify-content: center;  /* centers .icon's along horizontal axis. */   align-items: baseline;    /* aligns .icon's along common baseline vertically. */   outline: 3px dashed blue; /* show size of .container */ } .icon {   width: 10vh;   margin: 10px;   display: flex;   flex-direction: column;   outline: 1px dashed red; /* show size of .icon */ }  

body,  html {    width: 100%;    height: 100%;    display: flex;  }  .container {    display: flex;    flex: 1;    flex-wrap: wrap;    justify-content: space-between;    align-items: baseline;    align-content: flex-start;    outline: 3px dashed blue;  }  .icon {    width: 10vh;    margin: 10px;    display: flex;    flex-direction: column;    outline: 1px dashed red;  }  .img {    width: 10vh;    height: 10vh;    display: flex;    align-items: center;    justify-content: center;    background-color: red;  }  .text {    text-align: center;  }
<div class="container">    <div class="icon">      <div class="img">      </div>      <div class="text">        action 1      </div>    </div>    <div class="icon">      <div class="img">      </div>      <div class="text">        action 2      </div>    </div>      <div class="icon">      <div class="img">      </div>      <div class="text">        action 3      </div>    </div>      <div class="icon">      <div class="img">      </div>      <div class="text">        action 4      </div>    </div>      <div class="icon">      <div class="img">      </div>      <div class="text">        action 5      </div>    </div>    </div>


Comments

Popular posts from this blog

c++ - llvm function pass ReplaceInstWithInst malloc -

Cross-Compiling Linux Kernel for Raspberry Pi - ${CCPREFIX}gcc -v does not work -

java.lang.NoClassDefFoundError When Creating New Android Project -