【问题标题】:flex dynamic height fit imageflex 动态高度适合图像
【发布时间】:2018-07-25 03:49:32
【问题描述】:

由于 flex,需要将 img 放入动态计算的 li 高度。目前它需要所有高度,并拉高内容。图像应该刚好适合动态计算的 LI 高度,例如50px 并适应它。

不需要硬编码的高度。页面在任何设备中都应为 100%。页眉/页脚 - 弹性:1,主要 - 弹性:3。

body,
ul {
  margin: 0;
  padding: 0;
}

html,
body,
.root {
  height: 100%;
}

.root {
  display: flex;
  flex-direction: column;
}

main {
  flex: 3;
  background: lightyellow;
}

ul {
  display: flex;
  flex-direction: column;
  height: 100%;
}

ul li {
  flex: 1;
  background: aliceblue;
  outline: 1px solid #000;
  display: flex;
  /* align-items: center; */
  justify-content: space-between;
}

img {
  /* object-fit: contain; */
}

header,
footer {
  flex: 1;
}

header {
  background: lightgray;
}

footer {
  background: lightblue;
}
<div class="root">
  <header>
    HEAD
  </header>

  <main>
    <ul>
      <li>
        <img src="http://via.placeholder.com/200x200" />
        <span>text</span>
      </li>
      <li>
        <img src="http://via.placeholder.com/200x200" />
        <span>text</span>
      </li>
      <li>
        <img src="http://via.placeholder.com/200x200" />
        <span>text</span>
      </li>
    </ul>
  </main>

  <footer>
    FOOTER
  </footer>
</div>

https://jsfiddle.net/sefb3o95/18/

【问题讨论】:

    标签: html css flexbox


    【解决方案1】:

    使这项工作的主要内容是一直使用 Flexbox。

    简单地说,需要嵌套 flex 父/子而不是使用 height: 100%,以利用 Flexbox 自己的属性使其 flex 子拉伸/填充其父,原因是 height: 100% 不起作用正确。

    此外,将img 作为弹性项目会导致一些跨浏览器问题,因此将其包装起来并给它一个max-height: 100%,它的大小会适当。

    查看我在 CSS 中的注释。 Firefox 的min-height: 0 修复在这里得到了很好的解释:


    Updated fiddle

    堆栈sn-p

    body,
    ul {
      margin: 0;
      padding: 0;
    }
    
    html,
    body,
    .root {
      height: 100%;
    }
    
    .root {
      display: flex;
      flex-direction: column;
    }
    
    main {
      flex: 3;
      background: lightyellow;
      display: flex;                 /*  added  */
      flex-direction: column;        /*  added  */
      min-height: 0;                 /*  added, Firefox need this  */
    }
    
    ul {
      display: flex;
      flex-direction: column;
      min-height: 0;                 /*  added, Firefox need this  */
    }
    
    ul li {
      flex: 1;
      background: aliceblue;
      outline: 1px solid #000;
      display: flex;
      /* align-items: center; */
      justify-content: space-between;
      min-height: 0;                 /*  added, Firefox need this  */
    }
    
    img {
      max-height: 100%;              /*  added  */
    }
    
    header,
    footer {
      flex: 1;
    }
    
    header {
      background: lightgray;
    }
    
    footer {
      background: lightblue;
    }
    <div class="root">
      <header>
        HEAD
      </header>
    
      <main>
        <ul>
          <li>
            <span>
              <img src="http://via.placeholder.com/200x200" />
            </span>
            <span>text</span>
          </li>
          <li>
            <span>
              <img src="http://via.placeholder.com/200x200" />
            </span>
            <span>text</span>
          </li>
          <li>
            <span>
              <img src="http://via.placeholder.com/200x200" />
            </span>
            <span>text</span>
          </li>
        </ul>
      </main>
    
      <footer>
        FOOTER
      </footer>
    </div>

    【讨论】:

    • 谢谢!它现在适用于 Chrome,但不适用于 Safari - iOS 10.3.3。其实我需要让它在那里工作......
    • @AlexIvasyuv 现在无法访问 Safari。能发个截图吗?
    • @AlexIvasyuv 这在 Safari 中有效吗? jsfiddle.net/622ftLcL/9
    • Safari 中的主要问题是在img 上使用max-height: 100%。当子元素具有百分比高度时,Safari 非常严格地要求父元素具有指定的height 引用。弹性高度和最小/最大高度是不够的。 Safari 需要 height 属性。更多细节在下面的帖子中。特别是,请参阅 Solutions 部分之前的段落:stackoverflow.com/q/33636796/3597276
    • @Michael_B 非常感谢...并且在该链接的答案上加 1。
    猜你喜欢
    • 2014-10-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-03-02
    • 2015-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多