【问题标题】:Nested CSS-Grids and image resizing嵌套 CSS 网格和图像大小调整
【发布时间】: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 元素在其框内的对齐方式。

标签: html css css-grid


【解决方案1】:

图片本身没有样式。将以下行添加到您的样式表中。

.media .image img {
  display: block;
  max-width: 100%;
  height: auto;
}

示例:https://jsfiddle.net/nsrfpkb2/8/


更新

您需要为 .image 元素指定 position: relative;overflow: hidden;。然后给你的img一个position: absolute;top: 50%;left: 50%;transform: translate(-50%, -50%);

.news_layout{
        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;
        position: relative;
    }
    
    .media .image{
        height: 200px;
        grid-column: 1;
        grid-row: 1;
        background-color:red;
        position: relative;
        overflow: hidden;
    }
          
    .media .image img {
        position: absolute;
        top: 50%;
        left: 50%;
        transform: translate(-50%, -50%);
        display: block;
        max-width: 100%;
        height: auto;
    }
    
    .media h4{
        grid-column: 1;
        grid-row: 2;
    }
    
    .media p{
        grid-column: 1;
        grid-row: 3;
    }
<section class="news_layout">
  <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>

【讨论】:

  • 谢谢,在您的解决方案中,图像显示完整。但是应该放置图像的区域只有 200px 高度 .media{ grid-template-rows: 200px auto auto;} 我想要的是图像具有容器的完整宽度(有效)但也只是200 像素高度。因此,必须通过 CSS 对图像进行垂直定位,否则我可能只会看到图像的顶部 200 像素。
  • 感谢您的编辑版本。如果降低容器的宽度,则会在图像上方和下方获得空间。是否可以用图像填充 200px 的整个高度并保持纵横比?我知道因此图像会在左侧和右侧“裁剪”,但没关系。我认为这类似于 object-fit:cover。
  • 你也可以设置一个最大高度:100%;在 img 元素上。
  • max-height 垂直拉伸图像,纵横比消失。我不想那样。
猜你喜欢
  • 1970-01-01
  • 2018-04-30
  • 1970-01-01
  • 2011-03-18
  • 1970-01-01
  • 2021-05-30
  • 1970-01-01
  • 1970-01-01
  • 2013-03-26
相关资源
最近更新 更多