css - 调整 2:1 图像的大小以放入 1:1 div
问题描述
只想让所有产品图片使用相同的 1:1 响应区域。问题是,如果图像是 2:1,它使用的空间比预期的 (1:1) 多。如何使用 css 调整它的大小以显示这样的内容?(第一个例子)
当前设置:
max-width: 100%;
width: 100%;
height: auto;
max-height: 100%;
margin: 0 auto;
解决方案
您可以使用它object-fit:cover;
来覆盖图像所在的整个容器。在下面的示例中,我加载了一个 200x100 像素的图像,并将其包含在一个 100x100 像素的区域中。
请注意,IE 不支持此功能:https ://caniuse.com/#search=object-fit
#product {
width:100px;
height:100px;
}
#product img {
object-fit:cover;
width:100%;
height:100%;
}
<div id="product">
<img src="http://placehold.it/200x100&text=productimage">
</div>
推荐阅读
- php - : 在 php 中是什么意思?=> 在 php 中是什么意思?
- html - 如何在我的网站上添加 quora 关注按钮?
- javascript - 我的代码没有显示日期和时间,我该如何让它工作?另外,我可以再次单击该按钮以隐藏/显示日期和时间吗
- c - 如何使用 fork() 将数字与父进程和子进程相加
- android - 为什么我的 Flutter res/values/strings_en.arb 文件是空的?
- javascript - 如何修复模块以获得正确的输出?
- python - 计算 numpy 2D 矩阵中的“孔”
- multithreading - 为什么禁用中断会在单处理器系统中实现互斥?
- linux - 遍历目录中的文件;提取文件名以替换现有文件中的字符串
- sql - 分组依据以及子句外部的列