How to set button or icon color in mat-cell based on the color Hexcode value of element


I put a button in mat-cell, actually an icon is also fine, as my purpose is to indicate the color attribute of each record in a table. Each record has its own color values: name, hexcode and foreground, e.g., ‘Red’, value: ‘#ff1744’, foreground: ‘white’.

<ng-container matColumnDef="sku">
                <mat-header-cell *matHeaderCellDef mat-sort-header>SKU</mat-header-cell>
                <mat-cell *matCellDef="let element">{{element.sku}}</mat-cell>
            <ng-container matColumnDef="color">
                <mat-header-cell *matHeaderCellDef mat-sort-header> Color </mat-header-cell>
                <mat-cell *matCellDef="let element">
                    <button [disabled]="true"
            <ng-container matColumnDef="pictureUrl">......          

To give the button a circle shape, set it in css as below. This does woks as expected.

.color-preview {
    width: 20px;
    height: 20px;
    border-radius: 50%;

With above, I seems unable to get the button in the color wanted, e.g., red button with the hex value of #ff1744. Can anyone help? Any correction or solution would be greatly appreciated.


When I tried with Angular Material Mat Table
example and edit stalkblitz

The below code is working. (see screen shot)

In your HTML & Angular Component

<ng-container matColumnDef="position">
    <th mat-header-cell *matHeaderCellDef> No. </th>
    <td mat-cell *matCellDef="let element"> {{element.position}} 
    <button [disabled]="true"                    

    const ELEMENT_DATA: PeriodicElement[] = [
  {position: 1, name: 'Hydrogen', weight: 1.0079, symbol: 'H' , color : '#ff1744'},
  {position: 2, name: 'Helium', weight: 4.0026, symbol: 'He' , color : '#fff000'},
  {position: 3, name: 'Lithium', weight: 6.941, symbol: 'Li' , color : '#ff1744'},
  {position: 4, name: 'Beryllium', weight: 9.0122, symbol: 'Be', color : '#fff000'},
  {position: 5, name: 'Boron', weight: 10.811, symbol: 'B', color : '#ff1744'},
  {position: 6, name: 'Carbon', weight: 12.0107, symbol: 'C', color : '#fff000'},
  {position: 7, name: 'Nitrogen', weight: 14.0067, symbol: 'N', color : '#ff1744'},
  {position: 8, name: 'Oxygen', weight: 15.9994, symbol: 'O', color : '#fff000'},
  {position: 9, name: 'Fluorine', weight: 18.9984, symbol: 'F', color : '#ff1744'},
  {position: 10, name: 'Neon', weight: 20.1797, symbol: 'Ne', color : '#fff000'},

Answered By – Gaurav Upadhyay

Answer Checked By – Senaida (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.