html - 如何在 Angular Material 卡中使用图片元素

标签 html angular angular-material

如何将 picture 元素与 Angular Material 卡片结合起来?

在 Angular Material 卡片中 https://material.angular.io/components/card/examples你可以使用 <img mat-card-image src="../assets/img/shiba2.jpg" alt="Photo">

(mat-card-image 指令使图像占据卡片的整个宽度。)

但是当我介绍 picture元素与 mat-card-image图片和/或源元素上的指令会破坏 CSS,而且 Angular-Material 似乎不支持该元素。

这有效但总是显示 img元素:

<picture>
    <source srcset="../assets/img/shiba2.webp" type="image/webp">
    <source srcset="../assets/img/shiba2.jpg" type="image/jpg">
    <img mat-card-image src="../assets/img/shiba2.jpg" alt="Photo">
</picture>

我想使用 picture 元素以便使用 webp 图像并在 Safari 等不受支持的浏览器中回退到 jpg/png。

最佳答案

使用 example stackblitz given on material's website ,您可以执行以下操作以获得您要查找的内容...

  <picture>
    <source media='(min-width:0px)' srcset="https://material.angular.io/assets/img/examples/shiba2.jpg">
    <img mat-card-image src="" alt="Photo of a Shiba Inu">
  </picture>

material 网站上示例 stackblitz 的相关 HTML:

<mat-card class="example-card">
  <mat-card-header>
    <div mat-card-avatar class="example-header-image"></div>
    <mat-card-title>Shiba Inu (using <code>picture</code> tag)</mat-card-title>
    <mat-card-subtitle>Dog Breed</mat-card-subtitle>
  </mat-card-header>
  <picture>
    <source media='(min-width:0px)' srcset="https://material.angular.io/assets/img/examples/shiba2.jpg">
    <img mat-card-image src="" alt="Photo of a Shiba Inu">
  </picture>
  <mat-card-content>
    <p>
      The Shiba Inu is the smallest of the six original and distinct spitz breeds of dog from Japan.
      A small, agile dog that copes very well with mountainous terrain, the Shiba Inu was originally
      bred for hunting.
    </p>
  </mat-card-content>
  <mat-card-actions>
    <button mat-button>LIKE</button>
    <button mat-button>SHARE</button>
  </mat-card-actions>
</mat-card>

关于html - 如何在 Angular Material 卡中使用图片元素,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57361145/

相关文章:

javascript - 将div结构转换为单行 ListView

javascript - 如何优化我的网页(相当大)?

javascript - 对复杂数据元素的引用

Angular 5 - 用管道清理 HTML

javascript - 错误TS2339 : Property 'default' does not exist on type 'Special[]'

javascript - 如何访问 angularjs promise 中的数据?

angular - 单击 Angular 7 中的另一个组件时从 mat-table 传递数据行

javascript - 有没有办法用子列表迭代无序列表 - 没有 jQuery?

javascript - 将值写入表单字段 - 值写入多次

javascript - 单击 mat-button 隐藏一个组件并显示其他一个 Angular 9