![](/img/trans.png)
[英]How can I set height:100% on child of flex item that has flex-grow:1?
[英]How can I grow/shrink an image to the same height as its flex item sibling?
基于这个答案,我尝试将 flex-item img
元素缩小/增长到其兄弟元素的高度。 高度得到正确调整,图像被正确缩小 - 尊重纵横比 - 但img
元素仍然消耗与不缩小它一样多的宽度(查看空白):
main { display: flex; } section { display: flex; flex-direction: column; border: thin solid black; } img { flex-basis: 0px; flex-grow: 1; object-fit: contain; overflow-y: auto; }
<main> <section> <img src="http://via.placeholder.com/200x200"> </section> <section> <div>Match my height.</div> </section> </main>
我也尝试设置overflow-x: auto;
或width: 100%
(在img
上),但没有任何区别。 有没有办法在这个 flexbox 布局中获得预期的缩小图像尺寸?
有很多类似的问题,但他们不提供上述 flexbox 解决方案。 仅举几例:
所以,有替代方案,如果没有 flexbox 解决方案...
CSS 网格可能会更好,但我认为这不适用于所有浏览器。
Chrome 会给出很好的结果,但 Firefox 会失败
main { display: grid; grid-template-columns: auto auto; place-content: start; } section { border: thin solid black; } img { height: 0; min-height: 100%; object-fit: contain; overflow-y: auto; }
<main> <section> <img src="http://via.placeholder.com/200x200"> </section> <section> <div>Match my height.</div> </section> </main>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.