html - 计算图像的纵横比以获得与 flexbox 相同的高度

标签 html css image flexbox

我正在看: this code pen about equal responsive height images

我希望图像具有相同的高度,尽管它们的宽度/高度不同。 从下面的 CSS 中可以看出,使用 flexbox 和 flex 来获取图像,如下所示: enter image description here

/* Important stuff for this demo. */

img {
  width: 100%;
  height: auto;
  vertical-align: middle;
}

.pics_in_a_row {
  display: flex;
}

.img1 { flex: 1.3344; } /*  <-- how can I calculate this part? */
.img2 { flex: 1.3345; }
.img3 { flex: 0.7505; }
.img4 { flex: 1.5023; }
.img5 { flex: 0.75; }

如何计算 flex 的部分:数字。我猜这是宽高比?

最佳答案

宽高比是图像尺寸的 X/Y。

所以如果图像是 600 像素宽和 800 像素高,纵横比就是 600/800=0.75

当知道这一点时,您可以选择您引用的文章中提出的方法之一:

With the CSS methods, you do have a few options for specifying the aspect ratio:

  • Work out the aspect ratio yourself and hard-code it into the CSS (as done in this demo)
  • Use CSS's calc() to calculate the aspect ratio (e.g. flex: calc(600/800);)
  • Use a preprocessor to calculate the aspect ratio at build time

关于html - 计算图像的纵横比以获得与 flexbox 相同的高度,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51944581/

相关文章:

javascript - 创建不同大小图像的缩略图

javascript - 在 Canvas 上操作图像

html - 有没有办法使用 CSS 基于屏幕中心(水平)定位某些东西?

html - 尝试以固定高度垂直对齐 div 内的两个元素

段落中的 CSS span 标签

php - Shopware 阿拉伯语发票文本未连接

html - 如何使用 HTML 和 CSS 将垂直图像显示为水平方式?

PHP 文件在 echo 命令后打印所有内容

javascript - 防止 ng-include 内容预加载,直到切换为显示

html - 在 HTML 文档中使用字符引用的最佳做法是什么?