Div 在媒体查询将 flex-direction 设置为列后消失,可能是因为 flex 属性

Div disappears after media query sets flex-direction to column, probably because of flex property

所以我想使用 flex-box 制作一些重叠的内容。在宽屏幕上 flex-direction 设置为行,但在较小的屏幕上我想将其更改为列。

body {
  min-height: 100vh;
  background-color: #d6bd9e;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container {
  width: 90%;
  max-width: 960px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.left {
  border: 2px solid green;
  flex: 1 40%;
  width: 40%;
  height: 30rem;
  background: url("https://images.unsplash.com/photo-1512641406448-6574e777bec6?ixlib=rb-   1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHw%3D&w=1000&q=80") no-repeat;
  background-size: cover;
  border-radius: 8px;
}

.right {
  min-height: 20rem;
  flex: 1 50%;
  background-color: #4d6d52;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.5em;
  border-radius: 8px;
  margin-left: -4em;
}

@media(max-width: 500px) {
  .container {
    width: 100%;
    flex-direction: column;
  }
  .left {
    width: 90%;
  }
  .right {
    min-height: 14rem;
    margin: 0;
    margin-top: -6em;
    background: transparent;
  }
}
<div class="container">
  <div class="left"></div>
  <div class="right">
    <h1 class="heading">
      This is a heading!
    </h1>
    <p class="lorem">
      Lorem ipsum dolor sit amet consectetur adipisicing elit. Molestiae iste nihil, quasi eos aspernatur doloribus quos consequatur animi. In, nemo!
    </p>
  </div>
</div>

我还将 .right 的背景设置为透明,这样我可以看到我的 div 仍然存在(我可以看到绿色边框),但背景图像似乎丢失了。 我很确定它与 flex: 1 40%; 有关系。我一开始在 .left 中设置的,因为当我摆脱这个 flex 属性 时,它似乎起作用了。但我的问题是: 为什么 .left div 在我改变 flex-direction 时基本上崩溃了? 我做了一些研究,我唯一发现的是我必须为 div 设置一个高度,但正如您在我的代码中看到的那样,我的 .left 已经有一个高度。所以我确定它与我的 .left.

的 flex 属性 有关

如果我没理解错的话,当你达到媒体查询宽度时,背景图像会折叠但不会完全消失,因为有绿色边框。

我在媒体代码和原始容器选择器中为容器添加了一个高度 属性,现在图像没有消失。还要确保将元标记添加到您的 html head 标记中!希望这对您有所帮助!

HTML

<meta name="viewport" content="width=device-width, initial-scale=1">

CSS

body{
  min-height: 100vh;
  background-color:#d6bd9e;
  display: flex;
  align-items: center;
  justify-content: center;
}

.container{
  width: 90%;
  height: 100vh;
  max-width: 960px;
  display: flex;
  justify-content: center;
  align-items: center;
}

.left{
  border: 2px solid green;
  flex: 1 40%;
  width: 40%;
  height: 30rem;
  background: url("https://images.unsplash.com/photo-1512641406448-6574e777bec6?ixlib=rb-   1.2.1&ixid=MXwxMjA3fDB8MHxleHBsb3JlLWZlZWR8MXx8fGVufDB8fHw%3D&w=1000&q=80") no-repeat;
  background-size: cover;
  border-radius: 8px;
}

.right{
  min-height: 20rem;
  flex: 1 50%;
  background-color: #4d6d52;
  color: white;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 1.5em;
  border-radius: 8px;
  margin-left: -4em;
}

@media all and (max-width: 500px){
  .container{
    width: 100%;
    flex-direction: column;
    height: 80vh;
  }

  .left{
    width: 90%;
  }   

  .right{
    width: 75%;
    min-height: 14rem;
    margin: 0;
    margin-top: -6em;
  }
}