【问题标题】:How to make image take maximum space in flex container?如何使图像在 flex 容器中占据最大空间?
【发布时间】:2019-12-21 00:34:09
【问题描述】:

我需要使图像在 flex 容器中采用最大尺寸(采用最大宽度或最大高度)。

由于父容器没有固定的宽度和高度,我不能使用max-widthmax-heightflex:1 也不起作用。

这是一个问题的例子:https://jsfiddle.net/vb26u0e5/2/

我希望图像自动占用所有可用的绿色空间(删除 width: 40px; 第 20 行)。

#mainContainer {
  position: absolute;
  top: 0;
  bottom: 0;
  left: 0;
  right: 0;
  display: flex;
  flex-direction: column;
  color: white;
}

#imageContainer {
  flex: 1;
  display: flex;
  justify-content: space-between;
  background-color: green;
}

#image {
  width: 40px;
}

#previous,
#next {
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 25px;
  padding: 0 10px;
  background-color: purple;
}

#title,
#footer {
  text-align: center;
}

#title {
  background-color: blue;
}

#footer {
  background-color: red;
}
<div id="mainContainer">
  <div id="title">TITLE</div>
  <div id="imageContainer">
    <div id="previous">&lt;</div>
    <img id="image" src="https://via.placeholder.com/1920x1080" />
    <div id="next">&gt;</div>
  </div>
  <div id="footer">FOOTER</div>
</div>

【问题讨论】:

    标签: html css image flexbox


    【解决方案1】:

    将此添加到您的代码中:

    #image {
      width: 40px;
      flex-grow: 1; /* new */
    }
    

    通常,您可以使用flex-basis(在这种情况下相当于width),并执行以下操作:

    #image {
      flex: 1; /* fg:1, fs:1, fb:0 */
    }
    

    #image {
      flex: 1 0 40px; 
    }
    

    但是,某些浏览器存在一个错误,导致它们忽略 嵌套 flex 容器中的flex-basis。所以width / flex-grow 组合是一个干净的解决方法。

    有关更多详细信息,请参阅我的答案中的“浏览器错误”部分:


    下面的演示通过将图像包装在 div 中并在图像上使用绝对定位和object-fit 来涵盖上面回答的问题以及高度问题(纵横比和垂直滚动)。

    在 Chrome、Firefox 和 Edge 中测试。

    #mainContainer {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      display: flex;
      flex-direction: column;
      color: white;
    }
    
    #imageContainer {
      flex: 1;
      display: flex;
      justify-content: space-between;
      background-color: green;
    }
    
    #image {
      flex-grow: 1;
      position: relative;
    }
    
    img {
      position: absolute;
      width: 100%;
      height: 100%;
      object-fit: cover;
    }
    
    #previous,
    #next {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 25px;
      padding: 0 10px;
      background-color: purple;
    }
    
    #title,
    #footer {
      text-align: center;
    }
    
    #title {
      background-color: blue;
    }
    
    #footer {
      background-color: red;
    }
    <div id="mainContainer">
      <div id="title">TITLE</div>
      <div id="imageContainer">
        <div id="previous">&lt;</div>
        <div id="image">
          <img src="https://pixabay.com/get/52e3dc454f50a414f6d1867dda6d49214b6ac3e45657744e7d2b72dc90/oldtimer-4396528_1920.jpg" />
        </div>
        <div id="next">&gt;</div>
      </div>
      <div id="footer">FOOTER</div>
    </div>

    jsFiddle

    【讨论】:

    • width:40px被删除,只是为了显示绿色空间。它几乎可以在 Firefox 上运行(但高度太大并导致滚动)。它在 Chrome 上根本不起作用。
    • 明白。我只是用它来演示flex-basis。你可以删除它。我还发布了一个完整的演示,它解决了宽度和高度。
    • 效果很好!非常感谢您的帮助!
    • @JeanBlaguin,一般来说,尽量避免将图像制作成弹性/网格项目。浏览器还不能可靠地处理 flex / grid 格式化上下文中的图像(错误行为的风险很高)。将图像包装在 div 中,使 div 成为项目,并将图像保存在块格式上下文中。
    【解决方案2】:

    使用object-fit 属性指定如何调整图像大小以适应其容器。我已将其设置为object-fit: cover,它将切断图像的两侧,保留纵横比,并填充空间。也可以使用flex-grow:1 填充“绿色”空间。

    #mainContainer {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      display: flex;
      flex-direction: column;
      color: white;
    }
    
    #imageContainer {
      display: flex;
      justify-content: space-between;
      background-color: green;
    }
    
    #image {
      object-fit: cover;
      flex-grow: 1;
      height: 100%;
      overflow: hidden;
    }
    
    #previous,
    #next {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 25px;
      padding: 0 10px;
      background-color: purple;
    }
    
    #title,
    #footer {
      text-align: center;
    }
    
    #title {
      background-color: blue;
    }
    
    #footer {
      background-color: red;
    }
    <div id="mainContainer">
      <div id="title">TITLE</div>
      <div id="imageContainer">
        <div id="previous">&lt;</div>
        <img id="image" src="https://via.placeholder.com/1920x1080" />
        <div id="next">&gt;</div>
      </div>
      <div id="footer">FOOTER</div>
    </div>

    【讨论】:

    • 您测试过这个解决方案吗?它似乎不起作用。
    • @Dementic 我使用了一个虚拟图像 URL。我已将其更改为正确的。
    • 它没有按预期工作,如果您尝试调整输出窗口的大小,它不会自动适应容器:jsfiddle.net/1p6n3w9j/4
    • @Dementic 感谢您指出缺陷。我已经编辑了我的答案。
    • 仍然没有工作,图像现在在宽度上被切断了。
    【解决方案3】:

    您可以calc 基于垂直宽度 (100vw) 的宽度并删除您的 PREV/NEXT 的填充 像这样?

    #mainContainer {
      position: absolute;
      top: 0;
      bottom: 0;
      left: 0;
      right: 0;
      display: flex;
      flex-direction: column;
      color: white;
    }
    
    #imageContainer {
      flex: 1;
      display: flex;
      justify-content: space-between;
      background-color: green;
    }
    
    #image {
      width: calc(100vw - 50px);
      height: 100vh;
    }
    
    #previous,
    #next {
      display: flex;
      align-items: center;
      justify-content: center;
      font-size: 25px;
      padding: 0 10px;
      background-color: purple;
    }
    
    #title,
    #footer {
      text-align: center;
    }
    
    #title {
      background-color: blue;
    }
    
    #footer {
      background-color: red;
    }
    <div id="mainContainer">
      <div id="title">TITLE</div>
      <div id="imageContainer">
        <div id="previous">&lt;</div>
        <img id="image" src="https://via.placeholder.com/1920x1080" />
        <div id="next">&gt;</div>
      </div>
      <div id="footer">FOOTER</div>
    </div>

    【讨论】:

    • 页眉和页脚内容是可变的,所以恐怕这不是一个选项。我希望图像自动调整大小
    猜你喜欢
    • 2021-09-10
    • 2012-08-04
    • 2022-12-11
    • 1970-01-01
    • 2021-01-23
    • 1970-01-01
    • 2018-03-10
    • 2021-07-28
    • 2020-04-13
    相关资源
    最近更新 更多