【问题标题】:Div disappears after media query sets flex-direction to column, probably because of flex property媒体查询将 flex-direction 设置为列后,div 消失,可能是因为 flex 属性
【发布时间】:2021-05-28 06:14:32
【问题描述】:

所以我想使用 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 属性时,它似乎可以工作。但我的问题是: 当我改变 flex-direction 时,为什么 .left div 基本上会塌陷? 我做了一些研究,唯一发现的是我必须为 div 设置一个高度,但正如你在我的代码中看到的那样,我的 .left 已经有一个高度。所以我确定这与我的 .left 的 flex 属性有关。

【问题讨论】:

    标签: html css flexbox media-queries


    【解决方案1】:

    如果我理解正确,当您达到媒体查询宽度时,背景图像会折叠,但由于绿色边框不会完全消失。

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

    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;
      }
    }
    
    

    【讨论】:

    • 啊,是的,非常感谢!现在它起作用了!你能解释一下为什么会发生这种情况,那么为什么我需要一个高度?我猜这是因为一旦我将 flex-direction 更改为 column,flex 属性就不再真正指代宽度(就像它最初对 flex-direction: row 所做的那样),但现在它指的是高度。对吗?
    • 是的,我认为它结合了 flex-direction 和 height 的工作原理。 flex 容器上没有高度,列中就没有可用空间(该行设置了宽度)。但是,显示右侧框的原因是 a) 您在其上设置了最小高度 b) 如果删除了最小高度,则您有一个

      标签在其中,默认情况下有一个 display: block;财产。这意味着将绘制右侧框以显示内容。 (尝试将内容也添加到左侧框中,看看它是如何反应的)。至少我是这么理解的,希望对你有帮助!

    • 完美,非常感谢!欣赏!
    猜你喜欢
    • 2019-09-05
    • 2023-02-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-18
    • 1970-01-01
    • 2013-12-06
    • 2019-02-25
    相关资源
    最近更新 更多