margin-top only when the flex item is wrapped

后端 未结 1 441

I have a flex container with two flex items. I want to set a margin-top on the second one, but only when it\'s wrapped and not at the first flex line.

If possible, I

相关标签:
1条回答
  • 2020-12-07 22:16

    You can add some margin-top to both flex items, and a negative margin-top of the same amount to the flex container.

    This way, the negative margin of the flex container will neutralize the margin of the flex items at the first line, but not the margin of the items that wrapped to other lines.

    .container {
      margin-top: -30px;
    }
    .item-big, .item-small {
      margin-top: 30px;
    }
    

    .container {
      display: flex;
      flex-wrap: wrap;
      justify-content: space-around;
      margin-top: -30px;
    }
    .item-big, .item-small {
      margin-top: 30px;
      background: red;
    }
    .item-big {
      background: blue;
      width: 300px;
    }
    <div class="container">
       <div class="item-big">
          FIRST BIG ELEM
       </div>
       <div class="item-small">
          SECOND SMALL ELEM
       </div>
    </div>

    0 讨论(0)
提交回复
热议问题