html - Vertically center and left-align a column of flex items -


requirements

  1. flexbox
  2. vertically middle
  3. horizontally left
  4. vertically stacked child elements
  5. number of child elements can 1 or multiple
  6. use old syntax android 4.2 understands

sounds difficult describe. pink box in demo want. green box good, don't know how multiple child elements.

i think solution may combination of following, cannot make it.

align-items: center; flex-direction: column; 

body {    margin: 1em .5em;  }  article {    display: flex;    align-items: center;    flex-wrap: wrap;  }    section {    height: 18em;    background: #ccc;    margin: 0 .5em;    position: relative;    display: flex;    align-items: center;  }  section:after {    font-size: smaller;    color: blue;    position: absolute;    bottom: 0;  }  section.big {    width: 20%;    height: 5em;  }  section.small {    width: 10%;    flex: 1;  }  section div {    outline: 1px dotted green;  }  section.want {    background-color: pink;  }  section.want:after {    content: "1) want this: vertically middle, horizontally left, while section contain 1 or several child elements";  }  section.only1 {    background-color: lightgreen;  }  section.only1:after {    content: "2) except 1 point:the number of child element must one.";  }  section.row:after {    content: "3) default flex-diraction row";  }  section.col {    flex-direction: column;  }  section.col:after {    content: "4) can vertically stack multiple stacks, no longer horizontally left-align";  }  section.col.justify {    justify-content: center;    align-items: flex-start;    background-color: lightblue;  }  section.col.justify:after {    content: "6) good!  solved.";  }  section.colmar {    flex-direction: column;  }  section.colmar:after {    content: "5) can vertically stack multiple stacks, , left-align divs, divs not vertically center";  }  section.colmar div {    margin-right: auto;  }
<article>    <section class="small want">      line1      <br/>line2    </section>    <section class="small only1">      <div>only 1 div</div>    </section>    <section class="small row">      <div>div1</div>      <div>div2</div>    </section>    <section class="small col">      <div>div1</div>      <div>div2</div>    </section>    <section class="small colmar">      <div>div1</div>      <div>div2</div>    </section>    <section class="small col justify">      <div>div1</div>      <div>div2</div>    </section>  </article>

codepen demo

justify-content aligns flex items along main axis.

align-items aligns flex items along cross axis, perpendicular main axis.

depending on flex-direction, main axis may horizontal or vertical.

since flex-direction:column means main axis vertical, need use justify-content not align-items center flex items.

here's example:

#container {    display: flex;    flex-direction: column;    justify-content: center;    height: 200px;    background-color: lightpink;  }
<div id="container">    <div class="box">div 1</div>    <div class="box">div 2</div>    <div class="box">div 3</div>    <div class="box">div 4</div>    <div class="box">div 5</div>  </div>

learn more flex alignment along main axis here:

learn more flex alignment along cross axis here:


Comments

Popular posts from this blog

javascript - Chart.js (Radar Chart) different scaleLineColor for each scaleLine -

apache - Error with PHP mail(): Multiple or malformed newlines found in additional_header -

android - Go back to previous fragment -