Flexbox维持不同尺寸图像的纵横比
在此网站上浏览了数十个Flexbox问题之后,我认为是时候问了。
我想要一排的图像,其中行会伸展窗口的整个宽度,每个图像的高度相同,并且所有图像都保持其原始长宽比。例如,我有:
ul {
width: 100%;
list-style-type: none;
padding: 0;
display: flex;
align-items: stretch;
}
li {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}
img {
max-height: 100%;
max-width: 100%;
object-fit: contain;
}
<ul>
<li>
<img src="https://dummyimage.com/1920x1080"/>
</li>
<li>
<img src="https://dummyimage.com/1400x1000"/>
</li>
<li>
<img src="https://dummyimage.com/500"/>
</li>
</ul>
问题在更改高度时保持图像宽高比非常相似,但是这些答案的问题是所有图像都是相同的纵横比。
After sorting through dozens of Flexbox questions on this website with no luck, I figure it's time to just ask.
I want a row of images where the row stretches the entire width of the window, each image is the same height, and all images maintain their original aspect ratios. As an example, I have:
ul {
width: 100%;
list-style-type: none;
padding: 0;
display: flex;
align-items: stretch;
}
li {
flex-grow: 1;
flex-shrink: 1;
flex-basis: 0;
}
img {
max-height: 100%;
max-width: 100%;
object-fit: contain;
}
<ul>
<li>
<img src="https://dummyimage.com/1920x1080"/>
</li>
<li>
<img src="https://dummyimage.com/1400x1000"/>
</li>
<li>
<img src="https://dummyimage.com/500"/>
</li>
</ul>
The question Maintain image aspect ratio when changing height is very similar, but the problem with those answers is that all the images are the same aspect ratio.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果比率知道如下。请注意,在定义每个图像比率的情况下使用变量
If the ratio is know do it like below. Note the use of a variable where you define the ratio of each image
我最终梳理了 @temani afif ' /2442099/g-cyrillus“>@g-cyrillus '的方法:
但是,我意识到我最终尝试的是复杂,并且最好只使用图书馆: https ://github.com/naturalatlas/image-layout
I ended up combing @Temani Afif's and @G-Cyrillus' approaches to get:
However, I realized that what I am ultimately attempting is complicated and that I was better off just using a library: https://github.com/naturalatlas/image-layout