防止包装后伸缩盒项目收缩

时间:2018-11-22 21:34:20

标签: css flexbox

对于HTML

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

我定义了以下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;
    }
  }
}

当视口大于768像素时,我得到了所需的行为,即两个div占据了屏幕的一部分,请参见下文

enter image description here

但是当我降至768px以下时,我的第二项(绿色div)消失了

enter image description here

在包裹弹性物品后,如何调整css以保持div的比例?

1 个答案:

答案 0 :(得分:1)

只需更改方向:

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>