Skip to content
Readerstacks logo Readerstacks
  • Home
  • Softwares
  • Angular
  • Php
  • Laravel
  • Flutter
Readerstacks logo
Readerstacks
Angular Dynamic Style CSS with Example

Angular Dynamic Style CSS with Example

Aman Jain, October 5, 2022March 16, 2024

In this blog post, we’ll explore Angular dynamic style CSS with example in component’s styles based on certain conditions. Sometimes in our application we want dynamically change the color, size, font, background images so Angular provides various ways to conditionally apply styles to elements. In this post, we’ll explore three different ways to do so. We’ll discuss how to use the angular ngClass, element’s style property and angular ngStyle conditionally to achieve this.

Conditional style in angular using ngStyle

One way to conditionally apply styles in Angular is to use the ngStyle directive. The ngStyle directive can be used to set one or more style properties for an element. The style properties are set based on a given expression. If the expression evaluates to true, the corresponding style property is set. Otherwise, the property is unset.

The following example shows how to use the ngStyle directive to set the background-color style property for an element if the changeColor variable is true.

<a [ngStyle]="{'background-color': changeColor ? 'red' : 'white'}"> </a>

This div will be red if the changeColor variable is true.

The ngStyle directive allows you to dynamically update inline styles based on certain conditions. For example, let’s say we have a component with two inline styles: color:blue and margin:10px. We can use the ngStyle directive to conditionally update these styles like so

 
<a [ngStyle]="{'margin':'20px','color':'blue'}"> </a>

let understand Angular Dynamic Style CSS with Example

Step 1 : Create an angular app

First step is to setup an angular app using below command

ng new example-app

Step 2: Create a Component

Create a component and a method

ng g c hello-world
import { Component, OnInit } from '@angular/core';
import { ActivatedRoute } from '@angular/router';

@Component({
  selector: 'app-hello-world',
  templateUrl: './hello-world.component.html',
  styleUrls: ['./hello-world.component.css'],
})
export class HelloWorldComponent implements OnInit {
  dyanmicCssObject:any ={'margin':'20px','color':'blue'};
  changeColor:boolean=false;
  constructor() {
  }
  onChangeColor() {
    this.changeColor = !this.changeColor;
  }
  ngOnInit() {}
}

Here we used to changeColor method to change the color

Step 3 : Create template and add html

Now, add simple html template with input field

<h2>Angular Dynamic Style CSS with Example- Readerstacks.com</h2>

<div [ngStyle]="dyanmicCssObject"> Dynamic Object COlor </div>
 
<div [ngStyle]="{'background-color': ? 'red' : 'white'}"> Dynamic varibale COlor</div>
<a  href="#" (click)='onChangeColor()'>Change</a>

Angular conditional CSS using ngClass

let’s take a look at how to use the ngClass directive. The ngClass directive allows you to dynamically add or remove CSS classes based on certain conditions. For example, let’s say we have a component with two CSS classes: .red and .blue. We can use the ngClass directive to conditionally add or remove these classes like so:

<a  [ngClass]="{'red':true, 'blue':false}">
This div will have the class "red" and not the class "blue".> </a>

Finally, another way to conditionally apply styles in Angular is to use the element’s style property. The element’s style property can be used to set inline styles for an element. The styles are set based on a given expression. If the expression evaluates to true, the corresponding style is set. Otherwise, the style is unset.

The following example shows how to use the element’s style property to set the background-color style for an element if the isActive variable is true.

<a  [style.backgroundColor]="changeColor ? 'red' : 'white'">
This div will have the class "red" and not the class "blue".> </a>

Conclusion

No matter which approach you choose, Angular gives you the power to conditionally apply styles to elements.As you can see, the ngClass and ngStyle directives give us a way to dynamically update our Angular component’s styles based on certain conditions.

Related

Javascript Angular angualrCSSdynamicngClassngStyle

Post navigation

Previous post
Next post

Related Posts

Javascript reusable components in angular

How to make reusable components in angular?

September 11, 2021October 4, 2022

Another rich feature of angular is reusability of component, Angular components are those components which we can reuse multiple times in a module or application. Angular reusable component can be dynamic and much flexible to send updates on any event . Before start, I assume you well aware of installation…

Read More
Javascript How to Insert script tag dynamically in html in angular

How to Insert script tag dynamically in angular ?

March 24, 2022August 4, 2022

Sometimes we want to insert script tag in angular app, For an instance we have third party APIs or widget and we wanted to add it in our page then in this condition provided script are not angular friendly or we can say it can be pure JavaScript. so to…

Read More
Javascript Laravel Dynamic Autocomplete Using Typehead JS

Laravel Dynamic Autocomplete Using Typehead JS

July 16, 2022July 16, 2022

In this article we will learn to use Laravel Dynamic Autocomplete Using Typehead JS. Typehead JS is useful when we want live search of bulk data. Autocomplete search is mostly work of javascript and when we want to fetch live data from database then we require the intervention of laravel…

Read More

Aman Jain
Aman Jain

With years of hands-on experience in the realm of web and mobile development, they have honed their skills in various technologies, including Laravel, PHP CodeIgniter, mobile app development, web app development, Flutter, React, JavaScript, Angular, Devops and so much more. Their proficiency extends to building robust REST APIs, AWS Code scaling, and optimization, ensuring that your applications run seamlessly on the cloud.

Categories

  • Angular
  • CSS
  • Dart
  • Devops
  • Flutter
  • HTML
  • Javascript
  • jQuery
  • Laravel
  • Laravel 10
  • Laravel 11
  • Laravel 9
  • Mysql
  • Php
  • Softwares
  • Ubuntu
  • Uncategorized

Archives

  • June 2025
  • May 2025
  • April 2025
  • October 2024
  • July 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • July 2023
  • March 2023
  • November 2022
  • October 2022
  • September 2022
  • August 2022
  • July 2022
  • June 2022
  • May 2022
  • April 2022
  • March 2022
  • February 2022
  • January 2022
  • December 2021
  • November 2021
  • October 2021
  • September 2021
  • August 2021
  • July 2021
  • June 2021

Recent Posts

  • The Resilience of Nature: How Forests Recover After Fires
  • Understanding Laravel Cookie Consent for GDPR Compliance
  • Understanding High Vulnerabilities: A Critical Overview of the Week of May 12, 2025
  • Installing a LAMP Stack on Ubuntu: A Comprehensive Guide
  • Understanding High Vulnerabilities: A Deep Dive into Recent Security Concerns
©2023 Readerstacks | Design and Developed by Readerstacks
Go to mobile version