简体   繁体   English

在Angular Material网格中缩放图像

[英]Scale images in Angular Material grid

I'm attempting to place images inside grid tiles in an Angular-Material based app I'm working on. 我正在尝试将图像放在我正在处理的基于Angular-Material的应用程序中的网格图块内。 My problem is that these images do not "fit" within their tiles. 我的问题是这些图像不适合他们的瓷砖。 Below is an example. 以下是一个例子。 My image is large (2832x4256) and takes over the entire container rather than scaling to fit within its tile. 我的图像很大(2832x4256)并接管整个容器,而不是缩放以适应其瓷砖。 How could I get images to scale to fit within their respective tiles in the grid? 我怎样才能将图像缩放以适应网格中各自的图块?

<md-grid-list md-cols-gt-md="3" md-cols-md="2" md-cols-sm="1" md-gutter="12px" md-row-height="1:1">
  <md-grid-tile class="green">
    <img src="resources/images/food-beer.jpg" alt="beer">
    <md-grid-tile-footer>
      <h3>first tile</h3>
    </md-grid-tile-footer>
  </md-grid-tile>
  <md-grid-tile class="blue">
    <md-grid-tile-footer>
      <h3>second tile</h3>
    </md-grid-tile-footer>
  </md-grid-tile>
  <md-grid-tile class="purple">
    <md-grid-tile-footer>
      <h3>third tile</h3>
    </md-grid-tile-footer>
  </md-grid-tile>
</md-grid-list>

Below is the result. 结果如下。 What I'm aiming for instead is for the image to occupy the same amount of space as each of the blue and purple tiles. 我的目标是让图像占据与蓝色和紫色瓷砖相同的空间。

在此输入图像描述

如果你想让它们占据整个矩形,我相信使用“layout-fill”属性也会实现这一点。

<img src="resources/images/food-beer.jpg" alt="beer" layout-fill>

You could either do this with targeting the image in CSS or by setting it as a background image. 您可以通过在CSS中定位图像或将其设置为背景图像来执行此操作。

Here is the CSS method but the downside to this is as the aspect ratio is based on the images width, at some breakpoints the image may not fill the tiles height. 这是CSS方法, 缺点是因为纵横比基于图像宽度,在某些断点处图像可能无法填充图块高度。

.green img {
    max-width: 100%;
    height: auto;
}

So going with a background image maybe a little better but this method isn't fully cross browser supported. 因此,使用背景图像可能会更好一些, 这种方法并不完全支持跨浏览器。

.green {
    background-image: url('folder/your-image.png');
    background-size: cover;
}

It's been a year and the syntax has changed dramatically, but the problem remains. 已经过了一年,语法发生了巨大变化,但问题仍然存在。 Here's how I solved this issue in an Angular 5/Angular Material 5 project. 以下是我在Angular 5 / Angular Material 5项目中解决此问题的方法。

For 2 columns and pictures that were 4600px x 3400px, I did the following: 对于2列和4600px x 3400px的图片,我做了以下工作:

In my css: 在我的CSS中:

.tile-image {
  width:100%;
  height: auto;
}

In my html template: 在我的html模板中:

<mat-grid-list cols="2" rowHeight="6:4">
  <mat-grid-tile>
      <img class="tile-image" src="../demo/tilePic1.jpg">
  </mat-grid-tile>

All the answers are nice. 所有答案都很好。 But you can also try 但你也可以试试

<img src="resources/images/food-beer.jpg" style="object-fit: cover; height: inherit; width: inherit;">

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM