In this article we will see how can we bind CSS class to HTML element (class binding) in Angular 5. We will also discuss about ngClass directive to add or remove multiple classes from HTML element with example.

Examples

I have already created first-app component in my Angular 5 application.

  • In first-component.component.css file include the following three CSS classes.

  • In first-app.component.ts, include an <h2> element as shown below. Notice we have set the class attribute of the <h2> element to ‘class-success’.

At this point, run the application and notice that the class-success is added to the <h2> element as expected.

Modify the code in first-component.component.ts as shown below.

  1. We have added a property ‘classesToApply’in FirstComponent class.
  2. We have also specified class binding for the <h2> element. The word ‘class’ is in a pair of square brackets and it is binded to the property ‘classesToApply’
  3. This will replace the existing css classes of the <h2> with classes specified in the class binding.

Run the application and notice class-success is removed and class-fail, class-special classes are added.

Add or Remove single class

  • To add or remove a single class, include the prefix ‘class’ in a pair of square brackets, followed by a DOT and then the name of the class that you want to add or remove.
  • The following example adds class-special to the <h2> element. Notice it does not remove the existing class-success already added using the class attribute.
  • If you change applySpecialClass property to false or remove the property from the FirstComponent class, css class is not added to the <h2> element.

Example :

You can also remove an existing class that is already applied.

Consider the following example. Notice we have 3 classes (class-success & class-special) added to the <h2> element using the class attribute. The class binding removes the class-special.

Example:

To add or remove multiple classes use ngClass directive as shown in the example below.

  1. Added three public property with default values in FirstComponent:

  1. Add object styleClasses having with 3 key/value pairs. The key is a CSS class name. The value can be true or false. True to add the class and false to remove the class.

  1. Since two keys (class-fail & class-special) are set to true, both classes will be added to the <h2> element. Here class-success will not apply as corresponding property value isSuccess is false.
  2. Use ngClass directive with square bracket in HTML code and assign object name.

Example for ngClass directive:

Run the application and notice class-success is not applied as the component property isSuccess is set to false but class-fail and class-special is applied because isFail is set to true and isSpecial is set to true.

Nisarg Dave
He works as a software developer. He has hands-on experience on .net and other Microsoft technologies. He also works on AngularJS, KnockOutJS.
Nisarg Dave on FacebookNisarg Dave on Linkedin