【发布时间】:2017-03-21 11:11:47
【问题描述】:
我正在学习 CSS Grid,但我在调整图像大小时遇到了问题。
为了向您展示问题,我创建了一个包含 2 列的主布局,在第一列中,我创建了另一个网格,我想将图像和一些文本放在一列中。
请删除html评论,以便您看到图片。
在代码 sn-p 中,红色区域表示图像应该去的单元格。 现在,图像大于红色“容器”(高度:200px)网格单元格,我希望图像在单元格内缩小,以便它适合内部并保持纵横比(截断左/右或顶部/bottom 是必需的)。图像上的对象拟合不起作用。将图像定位为“object-position: 50% 50%”也不错,但这也不起作用。 我目前不确定如何解决此问题。
.mainlayout {
display: grid;
grid-template-columns: 1fr 1fr;
grid-template-rows: auto;
}
.media {
grid-column: 1 / 2;
grid-row: 1;
display: grid;
grid-template-columns: 1fr;
grid-template-rows: 200px auto auto;
}
.media .image {
grid-column: 1;
grid-row: 1;
background-color: red;
}
.media h4 {
grid-column: 1;
grid-row: 2;
}
.media p {
grid-column: 1;
grid-row: 3;
}
<section class="mainlayout">
<div class="media">
<div class="image">
<!--<img src="http://placehold.it/1000x1000">-->
</div>
<h4>Card title</h4>
<p>Some quick example text to build on the card title and make up the bulk of the card's content.</p>
</div>
</section>
感谢您的帮助。
【问题讨论】:
-
这里是大多数人使用的解决方案->> jsfiddle.net/nsrfpkb2/7
-
object-position 属性决定了 replaced 元素在其框内的对齐方式。