简体   繁体   中英

Angular Material: How to use `mat-icon` as `mat-card-avatar` within `mat-card-header`?

Providing an image avatar within a mat-card-header is well supported via mat-card-avatar .

在此处输入图像描述

There are many use-cases where we'd like to use an icon, instead of an image, as the card's "avatar".

Is there an easy way to replace the avatar with an icon?

This below sort of works, but is not quite as nice as the avatar image.

在此处输入图像描述

The mat-card-avatar directive can be applied to any element including <mat-icon> . You will need to apply style to the icon to get the size right:

<mat-card>
  <mat-card-header>
    <mat-icon mat-card-avatar>home</mat-icon>
    ...

mat-icon.mat-card-avatar {
  width: 40px;
  height: 40px;
  font-size: 40px;
}

They do not alter the style automatically. One approach I used was to declare it as and increase the font size to match the default 40px.

<mat-card class="example-card">
  <mat-card-header>
    <i mat-card-avatar class="material-icons">accessibility</i>
    <mat-card-title>Shiba Inu</mat-card-title>
    <mat-card-subtitle>Dog Breed</mat-card-subtitle>
  </mat-card-header>
.....
</mat-card>

With this, you only have to specify font-size:40px;

.example-card {
  max-width: 400px;

  & i {
    font-size: 40px;
  }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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