Angular Conditional Class with *ngClass
Last Updated :
24 Apr, 2025
In this article, we will see the basic implementation of the Conditional class with *ngClass in Angular, along with understanding their implementation with the help of examples.
Conditional class with *ngClass
Angular’s *ngClass directive allows us to apply CSS classes conditionally to an element. It provides a convenient way to dynamically toggle classes based on specific conditions. With *ngClass, we can bind a class name or an object to an element and specify the conditions under which the class should be applied. The directive evaluates the expressions and adds or removes the specified classes accordingly. This feature is particularly useful when you want to apply different styles or behavior to an element based on certain conditions, such as user interactions, data states, or dynamic variables. Overall, *ngClass simplifies the process of adding or removing classes dynamically, making it easier to create responsive and interactive Angular applications.
Syntax:
// String expression
<div [ngClass]="'class-name'"></div>
// Array expression
<div [ngClass]="['class1', 'class2']"></div>
// Object expression
<div [ngClass]="{ 'class1': condition1, 'class2': condition2 }"></div>
// Mixed expression
<div [ngClass]="['class1', { 'class2': condition }]"></div>
There are various aspects where the *ngClass is beneficial to implement. A few of them are described below:
- Boolean Properties: Define boolean properties in your component class and use them in the *ngClass expression. Set these properties to true or false based on your conditions to toggle the classes.
<div [ngClass]="{ 'active': isActive, 'highlight': isHighlighted }"></div>
- String Expressions: If you have a static class name or want to apply a class directly based on a condition, you can use a string expression directly in the *ngClass directive.
<div [ngClass]="isActive ? 'active' : 'inactive'"></div>
- Array Expressions: Use an array expression when you want to apply multiple classes conditionally. Combine static class names and class names based on conditions within the array.
<div [ngClass]="['active', isHighlighted ? 'highlight' : '']"></div>
- Object Expressions: Use an object expression when you have multiple classes and conditions. The key represents the class name, and the value represents the condition to apply that class.
<div [ngClass]="{ 'active': isActive, 'highlight': isHighlighted }"></div>
We can also use other directives like *ngIf or *ngFor along with *ngClass to conditionally apply classes based on changing states or data conditions.
Approach 1:
In this approach, when the button is clicked, it toggles the value of the “isClicked” variable and updates the message accordingly. If “isClicked” is true, the message displays “Welcome to GFG!!!” by changing the styling of the message with the help of *ngClass; otherwise, it displays nothing. The ngClass directive is used to conditionally apply the “text-darkgreen” class to the message element based on the value of “isClicked”.
Example: This example illustrates the implementation of a Conditional class with *ngClass in Angular.
HTML
< div class = "container" >
< div class = "row" >
< div class = "col-xs-12" >
< h2 >Geeks For Geeks</ h2 >
< button class = "btn btn-primary"
(click)="getMessage()">
Click Me
</ button >
< b >
< h3 class = "display"
[ngClass]="{'text-darkgreen': isClicked}">
{{message}}
</ h3 >
</ b >
</ div >
</ div >
</ div >
|
CSS
h 2 {
color : darkgreen;
}
.text-darkgreen {
color : white ;
font-size : medium ;
width : 350px ;
padding : 20px ;
background : green ;
}
|
Javascript
import { Component } from '@angular/core' ;
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
styleUrls: [ './app.component.css' ],
})
export class AppComponent {
message: string = '' ;
isClicked = false ;
getMessage() {
this .isClicked = ! this .isClicked;
if ( this .isClicked) {
this .message = 'Welcome to GFG!!!' ;
} else {
this .message = '' ;
}
}
}
|
Output:
.gif)
Approach 2:
In this approach, It contains a user interface with three buttons: “Toggle Success,” “Toggle Error,” and “Toggle Warning.” When a button is clicked, it triggers a corresponding function that changes the styling of the message that has been displayed accordingly with the help of *ngClass. The message is then displayed in an h3 element with a CSS class based on the active state. This allows for toggling between success, error, and warning messages dynamically.
Example: In this example, we are implementing the Conditional class with *ngClass in Angular, where the 3 toggle buttons are added that will render the message dynamically depending on the type of action.
HTML
< div class = "container" >
< div class = "row" >
< div class = "col-xs-12" >
< h2 >Welcome to GFG!!!</ h2 >< br >
< button class = "btn btn-success"
(click)="Success()">
Toggle Success
</ button >
< button class = "btn btn-danger"
(click)="Error()">
Toggle Error
</ button >
< button class = "btn btn-warning"
(click)="Warning()">
Toggle Warning
</ button >
< h3 class = "display"
[ngClass]="{ 'Success': isSuccess,
'Error': isError ,
'Warning':isWarning}">
{{message}}
</ h3 >
</ div >
</ div >
</ div >
|
CSS
h 2 {
color : darkgreen;
}
.Success {
color : black ;
font-size : medium ;
width : 350px ;
padding : 20px ;
background : lightgreen;
}
.Error {
color : black ;
font-size : medium ;
width : 350px ;
padding : 20px ;
background : red ;
}
.Warning {
color : black ;
font-size : medium ;
width : 350px ;
padding : 20px ;
background : yellow;
}
|
Javascript
import { Component } from '@angular/core' ;
@Component({
selector: 'app-root' ,
templateUrl: './app.component.html' ,
styleUrls: [ './app.component.css' ],
})
export class AppComponent {
isSuccess: boolean = false ;
isError: boolean = false ;
isWarning: boolean = false ;
message: string = '' ;
Success() {
this .isSuccess = true ;
this .isError = false ;
this .isWarning = false ;
this .message = 'Success Clicked!' ;
}
Error() {
this .isError = true ;
this .isSuccess = false ;
this .isWarning = false ;
this .message = 'Danger Clicked!!' ;
}
Warning() {
this .isWarning = true ;
this .isError = false ;
this .isSuccess = false ;
this .message = 'Warning Clicked!!' ;
}
}
|
Output:
.gif)
Similar Reads
AngularJS ngClass Conditional
The ngClass is a directive in AngularJS that facilitates to implementation of the CSS classes on an HTML element dynamically, ie, by adding and removing CSS classes on that specific element. We can use the ngClass directive to provide certain conditions, to add different classes to the elements. In
2 min read
How to conditionally apply CSS styles in AngularJS ?
In AngularJS we can build the dynamic single-page application with interactive CSS embedded in it. But as usual, while developing the application, the CSS is once statically defined and used, we are not changing the CSS when the application is running. But, in AngularJS we can dynamically change the
5 min read
AngularJS ng-class Directive
The ng-class Directive in AngularJS is used to specify the CSS classes on HTML elements. It is used to dynamically bind classes on an HTML element. The value for the ng-class has either string, an object, or an array. It must contain more than one class name, which is separated by space, in the case
2 min read
What is NgClass in Angular 10 ?
In this article, we are going to see what is NgClass in Angular 10 and how to use it. NgClass is used to Add or remove CSS classes on an HTML element Syntax: <element [ngClass] = "typescript_property"> Approach: Create the angular app to be usedIn app.component.html make an element and sets it
1 min read
AngularJS ng-class-odd Directive
The ng-class-odd Directive in AngularJS is used to specify the CSS classes on every odd appearance of HTML elements. It is used to dynamically bind classes on every odd HTML element. If the expression inside the ng-class-odd directive returns true then only the class is added else it is not added. T
2 min read
AngularJS ng-switch Directive
The ng-switch Directive in AngularJS is used to specify the condition to show/hide the child elements in HTML DOM. The HTML element will be displayed only if the expression inside the ng-switch directive returns true otherwise it will be hidden. It is supported by all HTML elements. Syntax: <elem
2 min read
Class Binding in Angular 8
Class binding in Angular makes it very easy to set the class property of a view element. We can set or remove the CSS class names from an element's class attribute with the help of class binding. We bind a class of a DOM element to a field that is a defined property in our Typescript Code. Its synta
2 min read
AngularJS ng-class-even Directive
The ng-class-even Directive in AngularJS is used to specify the CSS classes on every even appearance of HTML elements. It is used to dynamically bind classes on every even HTML element. If the expression inside the ng-class-even directive returns true then only the class is added else it is not adde
2 min read
Angular10 NgSwitch Directive
In this article, we are going to see what is NgSwitch in Angular 10 and how to use it. The NgSwitch in Angular10 is used to specify the condition to show or hide the child elements. Syntax: <li *NgSwitch='condition'></li> NgModule: Module used by NgSwitch is: CommonModule Selectors: [NgS
1 min read
Angular PrimeNG StyleClass Animations
Angular PrimeNG is an open-source library that consists of native Angular UI components that are used for great styling and this framework is used to make responsive websites with very much ease. In this article, we will be seeing Angular PrimeNG StyleClass Animations. StyleClass is used to manage C
3 min read