【问题标题】:How to use picture element in angular Material cards如何在角度材质卡中使用图片元素
【发布时间】:2019-12-13 03:04:07
【问题描述】:

如何将图片元素与 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 指令使图像占据卡片的整个宽度。)

但是当我在图片和/或源元素上引入带有mat-card-image 指令的picture 元素时,它会破坏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>

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

【问题讨论】:

  • 这个解决方案在 Angular 中完美运行:/

标签: html angular angular-material


【解决方案1】:

使用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>

材料网站上示例 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>

【讨论】:

  • 这太聪明了!您能否详细说明为什么需要media='(min-width:0px)' 才能在 Angular 中完成这项工作?谢谢
  • 我猜是从参考资料中复制的...没有它也应该可以工作...只需:` material.angular.io/assets/img/examples/shiba2.jpg"> `
  • 谢谢。现在我觉得很愚蠢,因为我在问题中的原始解决方案完美无缺:(
  • stack Overflow 就像我们办公室里的高级同事(大师),他让事情变得更简单......很高兴能提供帮助:)
猜你喜欢
  • 2017-11-24
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-01-19
  • 2019-09-13
  • 1970-01-01
  • 2018-05-08
  • 2016-12-11
相关资源
最近更新 更多