html - Flexbox child 不尊重其 parent 的边界

标签 html css flexbox

我正在尝试使用 flexbox 来制作响应式布局。很简单,我试图让图像位于 this example 的中心表现得像this example当浏览器窗口的高度和宽度被操纵时。

这两个示例几乎相同,只是在第一个示例中添加了一个具有设定高度的蓝色横幅。绿色横幅需要占据剩余的垂直空间,图像需要尊重绿色横幅的高度。

*注意:蓝色横幅的高度是动态的,在页面呈现之前我不知道高度是多少。它在 200px 的示例中被硬编码,因此该示例将起作用。

如有任何帮助,我们将不胜感激。

代码示例:

<html>
  <body>
    <div class="wrapper">
      <div class="imageWrapper">
        <img src="http://fpoimg.com/1000x800">
      </div>
    </div>
    <div class="stacksWrapper"></div>
  </body>
</html>

    * { -moz-box-sizing:border-box; -webkit-box-sizing:border-box; box-sizing:border-box; }
html { 
  margin:0; 
  padding:0; 
  height:100%; 
  width: 100%; 
}
body { 
  margin:0; 
  padding:0;  
  background:red; 
  height: 100%;
  width: 100%;
  display: flex;
  flex-direction: column;
}
.wrapper { 
 width: 100%;
  text-align: center;
  background: green;
  flex: 1;
  flex-basis: 0;
  min-height: 0;
  position: relative;
}

.imageWrapper {
  height:100%; 
  width: 100%;

  img {
      max-height:100%; 
      max-width:100%; 
      width: auto;
  }
}

.stacksWrapper {
  height: 200px;
  width: 100%;
  background-color: blue;
}

最佳答案

你也可以使用 flex 作为图像包装器,你可以将图像设置在绝对位置以避免看到它缩小:

.imageWrapper {
  height: 100%;  
  width: 100%;
  display:flex;

  img {
      max-height:100%; 
      max-width:100%; 
    /* cure shrinking effect */
    position:absolute;
    top:0;
    left:0;
    right:0;
    bottom:0; 
    /* end cure shrinking effect */
      margin:auto;
  }
}

http://codepen.io/gc-nomade/pen/wWKGOK

关于html - Flexbox child 不尊重其 parent 的边界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37665822/

相关文章:

html - CSS Flex 元素垂直然后水平

android - 如何在移动设备上调用 Facebook 页面的桌面站点?

html - 使用 Flexbox 垂直居中 div

asp.net - CSS 在 updatepanel 中搞砸了

css - 使用 IE 的 "Send page by e-mail"选项发送页面上特定 div 的内容

html - Flexbox 和溢出 :scroll

html - 使用 Angular 提交表单时更改类并显示消息

html - 在 css 中重叠的 block

Javascript 或 JQuery 需要重新加载页面,为什么?

html - 使 flexbox 容器垂直滚动