css - 使 mat-card-image 中的所有图像大小相同但正确缩放

标签 css twitter-bootstrap angular angular-material2

这通常不是问题,但只有 Material 2 在 atm 中的记录非常少,所以我无法将所有图像设置为相同大小,但可以根据窗口更改适当调整大小

我当时创建的所有图像在尺寸上都非常相似,但有一个在高度方面稍长一些,但我的旧代码只是通过一些 Bootstrap 调用对其进行了调整。

但是使用垫卡中的图像,它的长度更长,因此看起来不合适:

enter image description here

BAC 计算器的图像比其他的长,所以它看起来不合适并且将其他计算器推到它下面不成比例或强制一个空单元格,这是我对显示这些的呼吁:

<div *ngIf="data;else other_content">
    <div class="row">
      <div *ngFor="let project of data" class="col-md-6">
        <div>
          <mat-card class="mat-elevation-z4">
            <mat-card-header>
              <div mat-card-avatar class="example-header-image"></div>
              <mat-card-title>{{project.title}}</mat-card-title>
              <mat-card-subtitle>{{project.category}}</mat-card-subtitle>
              <div class="grow-empty"></div>
              <div *ngIf="project.source">
                <button mat-raised-button (click)="openSite(project.source)">SOURCE</button>
              </div>
            </mat-card-header>
            <img mat-card-image src="{{project.image}}" alt="{{project.title}}">
            <mat-card-content>
              <p>
                {{project.detail  | slice:0:250}}...
              </p>
            </mat-card-content>
            <mat-card-actions>
              <button mat-raised-button>Information</button>
            </mat-card-actions>
          </mat-card>
        </div>
        <br>
      </div>
    </div>
  </div>

如果我将高度设置为像“300”这样的静态值,它在技术上解决了这个问题,但重新缩放只会毁了它

我希望它像现在一样是固定比例,但拍摄任何图像并将其放大/缩小以适合卡片本身的尺寸(例如 Callum.Tech 卡片)

最佳答案

只需在您的代码中添加此样式引用即可。 如果您想将此样式应用于 View 中的所有卡片,请添加到您的样式文件:

mat-card img{
  object-fit: cover; /*this makes the image in src fit to the size specified below*/
  width: 100%; /* Here you can use wherever you want to specify the width and also the height of the <img>*/
  height: 80%;
}

这个问题的另一个解决方案可能是使用 Bootstrap 类“img-responsive”。 Here is a brief description

关于css - 使 mat-card-image 中的所有图像大小相同但正确缩放,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46841648/

相关文章:

javascript - 将 D3 线条样式改回常规

css - Bootstrap 4 Card 截断 div 绝对内容

css - 将大图像拟合到特定尺寸

css - 调整 btn bootstrap 中的文本

html - Bootstrap 导航栏 : Width of left element affects position of element in the center

JavaScript/ typescript : should decorator return a function

angular - 如何使用 Angular 6 在单个组件中渲染多个 ag-grid

html - Chrome (Win) 中的鼠标中键溢出错误

javascript - 背景图片平滑变化

html - 如何将纯 CSS 卡片样式的 div 放置在顶部的下方?