search

Home  >  Q&A  >  body text

css3 - How to align child elements under flex layout?

As shown in the picture, I use justify-content:space-around;, but I hope that the second row of child elements can be aligned up and down with the first row. The number of child elements is not fixed. How to solve it?

淡淡烟草味淡淡烟草味2802 days ago1825

reply all(4)I'll reply

  • 習慣沉默

    習慣沉默2017-07-04 13:47:08

    justify-content:space-between;

    reply
    0
  • 滿天的星座

    滿天的星座2017-07-04 13:47:08

    You may need some calculations:

    .container {
      width: 510px;
      display: flex;
      flex-wrap: wrap;
      border: 1px solid #ccc;
    }
    
    .item {
      width: 150px;
      height: 150px;
      background: red;
      margin: 10px;
    }
    <p class="container">
      <p class="item"></p>
      <p class="item"></p>
      <p class="item"></p>
      <p class="item"></p>
      <p class="item"></p>
    </p>

    View online https://jsfiddle.net/zktf3oc5/

    reply
    0
  • 给我你的怀抱

    给我你的怀抱2017-07-04 13:47:08

    flex The layout cannot achieve the effect you want when the parent container has a variable width.

    reply
    0
  • 世界只因有你

    世界只因有你2017-07-04 13:47:08

    Flex implements layout, mainly because your idea breaks through the convention. If the horizontal layout is unsuccessful, use the vertical axis layout, set it to three columns, each column,
    flex-direction:colomn;
    justify-content:space-around;
    Total Use the three columns again
    justify-content :space-around;

    reply
    0
  • Cancelreply