【发布时间】:2019-12-13 08:06:33
【问题描述】:
我有六张图片,我想保持 1.6 的宽高比。
问题是我正在使用网格:一行中的 3 列(1fr 1fr 1fr)。可以通过媒体查询更改为 2 列(1fr 1fr)。
我尝试使用一些 css 属性,例如将图像高度和宽度设置为 100%,但是列发生了变化,图像也在增长。
如何用 CSS 网格解决这个问题并保持 1.6 的纵横比? (而不是更改为背景只是图像标签)
<h1>all the images should be fill the width of the div. with aspect ratio of 1.6 and they need to be img tag.</h1>
<div class="grid">
<div><img src="https://picsum.photos/200/300?a"></div>
<div><img src="https://picsum.photos/200/300?b"></div>
<div><img src="https://picsum.photos/100/300?c"></div>
<div><img src="https://picsum.photos/200/200?d"></div>
<div><img src="https://picsum.photos/200/300?e"></div>
<div><img src="https://picsum.photos/200/100?f"></div>
</div>
body { padding:50px; }
.grid {
display:grid;
grid-template-columns: 1fr 1fr 1fr;
/* img { height:100%; width:100%;} */
}
【问题讨论】:
-
但是您的图片没有 8:5 的原生宽高比,所以它们会失真。