【问题标题】:How can an <img> scale to the height of a sibling?<img> 如何缩放到兄弟的高度?
【发布时间】:2020-01-03 00:48:09
【问题描述】:

标题非常具有自我描述性。请记住,容器元素应该有自己的宽度和高度,如示例所示。虽然这个示例是用 flexbox 编写的,但我不介意使用其他方法,只要它是纯 HTML/CSS 即可。

我觉得这应该是微不足道的,但是我一直在寻找一段时间,包括Stack Overflow上的相关答案,我似乎找不到任何令人满意的答案。

body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100vh;
  display: flex;
  justify-content: center;
  align-items: center;
}

img {
  margin-left: 8px;
}
<!DOCTYPE html>
<html>

<head>
  <meta charset="utf-8">
</head>

<body>
  <div>
    <h1>Lorem Ipsum</h1>
    <h2>Dolor Sit Amet, here's some extra text</h2>
  </div>
  <img src="https://lorempixel.com/g/64/64/" />
</body>

</html>

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    如果像我一样,您的图像可能比兄弟元素长,那么以下方法对我有用

    .wrapper {
      display: flex;
    }
    
    img {
      height: 100%;
      object-fit: cover;
      position: absolute;
    }
    
    .img-wrap {
      position: relative;
    }
    
    /* Some extra styling */
    .sibling {
      border: 1px dotted #999;
      padding: 16px;
    }
    <body>
      <div class="wrapper">
        <div class="sibling">
          <h1>Lorem Ipsum</h1>
          <h2>Dolor Sit Amet, here's some extra text</h2>
        </div>
        <div class="img-wrap">
          <img src="https://via.placeholder.com/150x300">
        </div>
      </div>
    </body>

    【讨论】:

      【解决方案2】:

      如果你再添加几个 div,这是可能的。

      .wrapper {
        display: flex;
        align-items: stretch;
      }
      
      img {
        height: 100%;
      }
      <body>
        <div class="wrapper">
          <div>
            <h1>Lorem Ipsum</h1>
            <h2>Dolor Sit Amet, here's some extra text</h2>
          </div>
          <div class="img-wrap">
            <img src="https://lorempixel.com/g/64/64/">
          </div>
        </div>
      </body>

      【讨论】:

      • 嗯,我想说这是一个正确的解决方案,但是 notice how the &lt;img&gt; overflows from the wrapper
      • @Xerz 嗯...尝试将 flex-shrink: 0 添加到 .img-wrap 中?
      • 似乎没有效果......老实说,如果我不想将内容居中或向右对齐,我不会介意
      • @Xerz 看起来这只是 Firefox 中的一个问题,在 Chrome 中它可以按预期工作。我有点搞砸了,但由于某种原因,当其中的图像增长时,Firefox 并没有扩展父容器。我不确定,但我想说这是他们的错误/疏忽。
      • github.com/webcompat/web-bugs/issues/38951 开了一张票,同时我会接受这个答案。非常感谢! ?
      【解决方案3】:

      您可以简单地从父级 (body) 中删除 height: 100vh 并将图像上的 align-self 设置为 stretch。但是,这不会保留图像的纵横比。

      更好的方法是使用网格:

      body {
        margin: 0;
        padding: 0;
        width: 100%;
        display: grid;
        grid-template-columns: 1fr max-content;
        grid-template-rows: auto auto;
        justify-content: center;
        align-items: center;
      }
      
      h2 {
        grid-row: 2;
      }
      
      img {
        height: 100%;
        grid-column: 2;
        grid-row: 1 / 3;
      }
      <!DOCTYPE html>
      <html>
      
      <head>
        <meta charset="utf-8">
      </head>
      
      <body>
        <h1>Lorem Ipsum</h1>
        <h2>Dolor Sit Amet, here's some extra text</h2>
        <img src="https://lorempixel.com/g/64/64/" />
      </body>
      
      </html>

      【讨论】:

        【解决方案4】:

        正确地将图像结构化为 div 并应用 flex 属性应该可以解决问题。

        <body>
          <div class="wrapper">
            <div>
              <h1>Lorem Ipsum</h1>
              <h2>Dolor Sit Amet, here's some extra text</h2>
            </div>
            <div class="img-wrap flex-1">
              <img src="https://lorempixel.com/g/64/64/">
            </div>
          </div>
        </body>
        
        
          .wrapper {
            display: flex;
            align-items: stretch;
          }
        
          img {
            height: 100%;
          }
        .flex-1{
          flex: 1;
        }
        

        【讨论】:

        • 似乎这个答案对解决溢出问题没有任何作用,否则它与马特的相同
        • flex: 1 不应让图像溢出。如果它溢出了,你能分享一支笔吗?
        • 哦,这就解释了为什么事情在 chrome 中看起来很完美。
        【解决方案5】:

        您可以将两个兄弟姐妹放入父&lt;div&gt;,给父标签一个设定的高度,然后给&lt;img/&gt;标签一个100%的高度。这样它将缩放到父框的高度。 例如,

        <div style="height: 100px;">
          <div>
            <h1>Lorem Ipsum</h1>
            <h2>Dolor Sit Amet, here's some extra text</h2>
          </div>
          <img style="height: 100%;" />
        </div>
        

        类似的东西应该可以工作。

        【讨论】:

        • 我不想有一个固定的高度,我想动态设置它以匹配兄弟&lt;div&gt;
        猜你喜欢
        • 1970-01-01
        • 2012-05-15
        • 1970-01-01
        • 2017-03-14
        • 2018-07-06
        • 1970-01-01
        • 1970-01-01
        • 2017-06-28
        • 2014-09-20
        相关资源
        最近更新 更多