Prevent flex box item shrinking after wrap

霸气de小男生 提交于 2020-04-30 16:35:36

问题


For the HTML

<section class="flex-container">
<div class="item"></div>
<div class="item"></div>
</section>

I have defined the following css

.flex-container {
  display: flex;
  flex-wrap: wrap;
  min-height: 100vh;

  .item {
    background-color: #6f42c1;
    flex: 3;
  }

  .item:nth-child(2) {
    background-color: #0c5460;
    flex: 5;
  }

  @media (max-width: 768px) {
    .item:first-child {
      flex-basis: 100%;
    }
    .item:nth-child(2) {
      flex-shrink: 0;
    }
  }
}

When the viewport is greater than 768px I get the desired behaviour, i.e., two divs taking up a proportion of the screen, see below

but when I drop below 768px, my second item (green div) disappears

How can I adjust my css such that I maintain proportioned divs after the flex items have wrapped?


回答1:


Simply change the direction:

body {
 margin:0;
}

.flex-container {
  display: flex;
  flex-wrap: wrap;
  min-height: 100vh;
}

.item {
  background-color: #6f42c1;
  flex: 3;
}

.item:nth-child(2) {
  background-color: #0c5460;
  flex: 5;
}

@media (max-width: 768px) {
  .flex-container {
    flex-direction: column;
  }
}
<section class="flex-container">
  <div class="item"></div>
  <div class="item"></div>
</section>


来源:https://stackoverflow.com/questions/53438205/prevent-flex-box-item-shrinking-after-wrap

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!