CSS Class Binding In Angular 5

We use CSS classes to give an impressive look and feel to our applications. It is very important to add or remove CSS classes at runtime to maintain high user experience in the application. It is a very common thing for any front-end developer to do so either in JavaScript or jQuery.

Let’s see how to do the same in Angular5.

If you are new to Angular5, then click here to get started.

In this article, I am using pipes also. If you want to know more about pipes, here is the guide.

Code, for an example

Following is the baseline code I am using for the example. We will keep changing the code as we progress.

Component .css 

  1. h1{  
  2.     font-size: 30px;  
  3.     color: #fff;  
  4.     text-transform: uppercase;  
  5.     font-weight: 300;  
  6.     text-align: center;  
  7.     margin-bottom: 15px;  
  8.   }  
  9.   table{  
  10.     width:100%;  
  11.     table-layout: fixed;  
  12.   }  
  13.   .tbl-header{  
  14.     background-color: rgba(255,255,255,0.3);  
  15.    }  
  16.   .tbl-content{  
  17.     height:300px;  
  18.     overflow-x:auto;  
  19.     margin-top: 0px;  
  20.     border: 1px solid rgba(255,255,255,0.3);  
  21.   }  
  22.   th{  
  23.     padding: 20px 15px;  
  24.     text-align: left;  
  25.     font-weight: 500;  
  26.     font-size: 12px;  
  27.     color: rgb(224, 18, 18);  
  28.     text-transform: uppercase;  
  29.   }  
  30.   .td{  
  31.     padding: 15px;  
  32.     text-align: left;  
  33.     vertical-align:middle;  
  34.     font-weight: 300;  
  35.     font-size: 12px;  
  36.     color: rgb(125, 19, 196);  
  37.     border-bottom: solid 1px rgba(255,255,255,0.1);  
  38.   }  


  1. <div class="tbl-header">  
  2.     <table cellpadding="0" cellspacing="0" border="0">  
  3.         <thead>  
  4.             <th>Name</th>  
  5.             <th>Gender</th>  
  6.             <th>Salary</th>  
  7.             <th>Date of joining</th>  
  8.         </thead>  
  9.         <tbody>  
  10.             <tr *ngFor='let credit of credits; let i = index'>  
  11.                 <td class = "td">{{credit.name | title : credit.gender}}</td>  
  12.                 <td class = "td">{{credit.gender}}</td>  
  13.                 <td class = "td">{{credit.salary | currency}}</td>  
  14.                 <td class = "td">{{credit.DOJ | date:'shortDate'}}</td>  
  15.             </tr>  
  16.         </tbody>  
  17.     </table>  
  18. </div>  



Adding a class

There are multiple ways to declare a class in Angular5.

Any simple HTML class declaration holds good in Angular also.

  1. <div class="tbl-header">  

But, if you are looking at a complicated Angular code, then the following two types of assignments are also very common.

  1. <td [class.td] = true>{{credit.name | title : credit.gender}}</td>  

Here, class will be “td” as the condition is true.

  1. <td [ngClass] = "{'td' : true}">{{credit.gender}}</td>  

In the above example, [ngClass] is used to add the class to the element.

Adding class on condition

Let’s change the code to change the color of the rows where the gender is male. If the person is female, then the row will remain unchanged.

Let’s add a CSS class for the same.

  1. .td-green{  
  2.     padding: 15px;  
  3.     text-align: left;  
  4.     vertical-align:middle;  
  5.     font-weight: 300;  
  6.     font-size: 12px;  
  7.     color: rgb(15, 148, 48);  
  8.     border-bottom: solid 1px rgba(255,255,255,0.1);  
  9.   }  

Change the HTML code also.

  1. <tr *ngFor='let credit of credits; let i = index'>  
  2.     <td class="td" [class.td-green]="credit.gender=='male'">{{credit.name | title : credit.gender}}</td>  
  3.     <td class="td" [class.td-green]="credit.gender=='male'">{{credit.gender}}</td>  
  4.     <td class="td" [class.td-green]="credit.gender=='male'">{{credit.salary | currency}}</td>  
  5.     <td class="td" [class.td-green]="credit.gender=='male'">{{credit.DOJ | date:'shortDate'}}</td>  
  6. </tr>  


In all the columns, I have added the following condition.

  1. <td class="td" [class.td-green]="credit.gender=='male'">  

It means when the gender is male, the default CSS is overridden with the specified CSS class.



Adding multiple classes on condition

There are times when we need to add multiple classes to an element to achieve our required output.

Let's create another CSS class to change the font of the Name column. 

  1. .td-arial{  
  2.       font-weight: 800;  
  3.       font-family: Arial, Helvetica, sans-serif;  
  4.   }  

If the person is male, then we will add 2 CSS classes to the element <td>.

Changed HTML part

  1. <tr *ngFor='let credit of credits; let i = index'>  
  2.     <td class="td" [ngClass]="{'td-green td-arial' : credit.gender=='male'}">{{credit.name | title : credit.gender}}</td>  
  3.     <td class="td">{{credit.gender}}</td>  
  4.     <td class="td">{{credit.salary | currency}}</td>  
  5.     <td class="td">{{credit.DOJ | date:'shortDate'}}</td>  
  6. </tr>  


Following is the part of the code which does the trick (for name column).

  1. [ngClass]="{'td-green td-arial' : credit.gender=='male'}"  

It checks if the gender is male, then it adds 2 CSS classes to the element.



Remove class on condition

Let’s see how to remove an added class from an element.

To begin with, I have added 2 CSS classes in the name column. Like this:

  1. <tr *ngFor='let credit of credits; let i = index'>  
  2.   <td class = "td td-arial" >{{credit.name | title : credit.gender}}</td>  
  3.   <td class = "td" >{{credit.gender}}</td>  
  4.   <td class = "td" >{{credit.salary | currency}}</td>  
  5.   <td class = "td" >{{credit.DOJ | date:'shortDate'}}</td>  
  6. </tr>  



Let’s remove the td-arial class when the person is female, i.e., if the person is male, then the class will be added; else it will be removed.

Following is the change in code. 

  1. <tr *ngFor='let credit of credits; let i = index'>  
  2.    <td class = "td td-arial" [class.td-arial] = "credit.gender == 'male'">   {{credit.name | title : credit.gender}}</td>  
  3.    <td class = "td" >{{credit.gender}}</td>  
  4.    <td class = "td" >{{credit.salary | currency}}</td>  
  5.    <td class = "td" >{{credit.DOJ | date:'shortDate'}}</td>  
  6. </tr>  

I have added the following code.

  1. [class.td-arial] = "credit.gender == 'male'">  

This ensures that the class will be added when a person is male.



Please let me know your feedback/comments.