Twitter LogoFacebook Logo
Card
The Card Component from Angular Material. Hello, in this tutorial, I will be talking about the Card Component from Angular Material.
By: King Wai Mark

Hello, in this tutorial, I will be talking about the Card Component from Angular Material.


The card component is an element with a default drop shadow effect, so it looks like it is floating on the screen. There are 3 main sections to the card, the header, the content, and the footer.

Image from Codeible.com

The header is in the top area of the card. It can have an icon, a title, and a subtitle.

The content area is located underneath the header where we place all our contents for the card.

And the footer an area anchored at the bottom of the card. 

Before we can use the card component, we have to add the MatCardModule to the project. 
Go to the app.module typescript file and import the MatCardModule. 

import { MatCardModule } from '@angular/material/card';

@NgModule({
  declarations: [
    AppComponent
  ],
  imports: [
    ...,
    MatCardModule
  ],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule { }

To begin, go to the app component html page and add the mat-card element.

<mat-card>


</mat-card>

This will create blank area for our card content. To see it better, we will wrap our card within a div and give it some styles so we can see the card.

<div style="max-width: 400px; margin: auto; padding: 1em;">
    <mat-card>

    </mat-card>
</div>

Adding Header

To add the header section, add the mat-card-header element inside our card. With this, we can add the title, subtitle, and the icon for our card.

<div style="max-width: 400px; margin: auto; padding: 1em;">
    <mat-card>
       <mat-card-header>
   
       </mat-card-header>
    </mat-card>
</div>

For the title, we use <mat-card-title>
For the subtitle, we use <mat-card-subtitle>
And for the icon, we use a regular img element and apply the mat-card-avatar property

<div style="max-width: 400px; margin: auto; padding: 1em;">
    <mat-card>
       <mat-card-header>
            <mat-card-title>Codeible</mat-card-title>
            <mat-card-subtitle>Codeible.com</mat-card-subtitle>
            <img src="" mat-card-avatar>
       </mat-card-header>
    </mat-card>
</div>

Adding Content

To add content, we just place our contents underneath the header element. 


To add images, we should use the regular image element along with mat-card-image property. This will stretch the image's width to match the card’s width. 

<mat-card>
    <mat-card-header>
         ...
    <mat-card-header>

    <img src="" mat-card-image />

</mat-card>

For texts, we should put them inside the mat-card-content element. 

<mat-card>
    <mat-card-header>
         ...
    <mat-card-header>

    <img src="" mat-card-image />

    <mat-card-content>
         <p>Like</p>
         <p>Share</p>
         <p>Subscribe</p>
    </mat-card-content>

</mat-card>

And for buttons and other interactive elements, we should place them inside the mat-card-actions element.

<mat-card>
    ...

    <mat-card-actions>
         <button>Like</button>
         <button>Share</button>
         <button>Subscribe</button>
    </mat-card-actions>

</mat-card>

This will correctly align the elements inside the container.

One advantage of using the material card actions element is that we can change the alignment of the elements by setting the align property. We can use start to align the elements to the left or end to align the elements to the right.

<mat-card-actions align="start">

<mat-card-actions align="end">

Adding Footer

Lastly to add the footer, just add the mat-card-footer element to the card. This will create a container that will be anchored to the bottom of the card. 

<mat-card>
    ...

    <mat-card-footer>
        
    </mat-card-footer>

</mat-card>

We can put content inside and it will show up at the bottom.

<mat-card-footer>
   <div style="box-sizing: border-box; padding: 1em">
       @#169; Codeible
   <div>
</mat-card-footer>


Sign In