Flexbox维持不同尺寸图像的纵横比

发布于 2025-02-09 04:10:26 字数 906 浏览 1 评论 0原文

在此网站上浏览了数十个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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

李不 2025-02-16 04:10:26

如果比率知道如下。请注意,在定义每个图像比率的情况下使用变量

ul {
  list-style-type: none;
  padding: 0;
  display: flex;
}

li {
  flex-grow: calc(var(--r));
  flex-basis: 0;
}

img {
  max-width: 100%;
}
<ul>
  <li style="--r: 1920/1080">
    <img src="https://dummyimage.com/1920x1080"/>
  </li>
  <li  style="--r: 1400/1000">
    <img src="https://dummyimage.com/1400x1000"/>
  </li>
  <li  style="--r: 500/500">
    <img src="https://dummyimage.com/500"/>
  </li>
</ul>

If the ratio is know do it like below. Note the use of a variable where you define the ratio of each image

ul {
  list-style-type: none;
  padding: 0;
  display: flex;
}

li {
  flex-grow: calc(var(--r));
  flex-basis: 0;
}

img {
  max-width: 100%;
}
<ul>
  <li style="--r: 1920/1080">
    <img src="https://dummyimage.com/1920x1080"/>
  </li>
  <li  style="--r: 1400/1000">
    <img src="https://dummyimage.com/1400x1000"/>
  </li>
  <li  style="--r: 500/500">
    <img src="https://dummyimage.com/500"/>
  </li>
</ul>

风筝有风,海豚有海 2025-02-16 04:10:26

我最终梳理了 @temani afif ' /2442099/g-cyrillus“>@g-cyrillus '的方法:

let smoothImg = function () {
    const images = document.querySelectorAll("ul li img");
    for (image of images) {
        image.parentElement.style.flexGrow = image.offsetWidth/image.offsetHeight;
    }
};

window.onload = smoothImg;
window.onresize = smoothImg;
ul {
  list-style-type: none;
  padding: 0;
  display: flex;
}

li {
  flex-grow: 1;
  flex-basis: 0;
}

img {
  max-width: 100%;
}
<ul>
    <li>
        <img src="https://dummyimage.com/1920x1080" />
    </li>
    <li>
        <img src="https://dummyimage.com/1400x1000" />
    </li>
    <li>
        <img src="https://dummyimage.com/200x150" />
    </li>
    <li>
        <img src="https://dummyimage.com/300" />
    </li>
</ul>

但是,我意识到我最终尝试的是复杂,并且最好只使用图书馆: https ://github.com/naturalatlas/image-layout

I ended up combing @Temani Afif's and @G-Cyrillus' approaches to get:

let smoothImg = function () {
    const images = document.querySelectorAll("ul li img");
    for (image of images) {
        image.parentElement.style.flexGrow = image.offsetWidth/image.offsetHeight;
    }
};

window.onload = smoothImg;
window.onresize = smoothImg;
ul {
  list-style-type: none;
  padding: 0;
  display: flex;
}

li {
  flex-grow: 1;
  flex-basis: 0;
}

img {
  max-width: 100%;
}
<ul>
    <li>
        <img src="https://dummyimage.com/1920x1080" />
    </li>
    <li>
        <img src="https://dummyimage.com/1400x1000" />
    </li>
    <li>
        <img src="https://dummyimage.com/200x150" />
    </li>
    <li>
        <img src="https://dummyimage.com/300" />
    </li>
</ul>

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文