How to Use ngStyle in Angular for Dynamic Styling?

Websolutionstuff | Oct-04-2023 | Categories : Angular

A user-friendly and dynamic interface is paramount to engaging users on your site. Angular is one of the most powerful and famous JavaScript frameworks that offers a wide range of features to build an attractive interface.

"ngStyle" is a versatile directive that helps you dynamically style your elements based on changing data and user interactions.

Here, we will help you learn ngStyle effectively. So, get ready to embark on a journey towards captivating users!
 

What is a Style Attribute?

The Style attribute helps control how a view element looks and allows you to set styles directly.

Syntax:

<p style="background-color: red;">
</p>

In the example above, it changes the background color of a <p> tag to red.

You can have multiple style declarations in a block.

<p style "background-color: green; margin: 2px; font-size: 10px;"> 
</p>

The text inside the style attribute contains the CSS style declaration, where you specify properties (like color or size) and their values (like red or 14px).

Style declaration:

1    selector {
2    css-property: value;
3    css-property: value;
4    }

 

Example of the style attribute:

<p style="background-color: yellow; color:black; font-size: 35px;">
</p>

 

The inline style for the p element styles from the selector.

 

Property Binding With Style:

Property binding is used for styling CSS properties. We can style only one property at a time.

Syntax:

<element [style.css-property.unit] = "template-expression"> </element>

 

Example of the property binding with the style:

<p [style.color] "Blue"> 
</p>

 

Here, the square brackets serve as a dynamic property binding, holding the property to be updated.

Additionally, style binding includes the unit of measurement. Certain CSS properties require a specific unit, such as font size, padding, margin, width, height, and more.

In the app.component.html file, you can write the style property like this:

<p [style.font-size.px] = "isValid? 30: 15"> </p>
<p [style.font-size.%]= "isValid: 50: 30"> </p>

 

In the app.component.ts file, we can set the isValid value to true:

import { Component } from '@angular/core';
    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
    title = 'NgStyleDemo';
    isValid = true;
    }

 

What is the NgStyle?

The NgStyle attribute helps you transform the look and feel of an element, adding extra charm to your web design. NgStyle is truly helpful when the value is dynamic.

With the ngStyle directive, you can easily create styles and captivate your audience.

<div [ngStyle] = "{ "background-color" : 'red' }"> </div>

 

In the example above with NgStyle, it sets the div's background to red.

You can add multiple CSS properties at once with NgStyle.

<div [ngStyle] = "{ "background-color': 'red', 'color': 'white', 'font-size': '40px' }"> </div>

 

Here we have set various CSS properties like background color, color, and font size to div element.

Example 1
In this example, we will use ngStyle for styling and write the inline CSS on the element.

<h1 [ngStyle] "{ 'background-color': 'green', 'color': 'black', 'font-w eight': 'bold', 'font-size: 20px' }"> 
</h1>

 

Example 2

app.components.ts

import { Component } from '@angular/core';
    @Component({
    selector: 'app-root',
    templateUrl: './app.component.html',
    styleUrls: ['./app.component.scss']
    })
    export class AppComponent {
    title = 'NgStyleDemo';
      
    myCss = {
    'background-color' : 'green',
    'color' : 'white', 
    'font-size' : '40px',
    'font-weight' : 'bold',
    };
    }

 

app.component.html

<p [ngStyle]="myCss"> 
</p>

In this example, we can write the CSS in the app.component.ts file and use the CSS variable in the ngstyle directive in the app.component.html file.

 

Conclusion

Leveraging ngStyle in your web development projects will help you create attractive interfaces and captivating user experiences.

With ngStyle, you can now breathe life into your applications, adapting them to user interactions and injecting personality into your code. The possibilities are endless, so go ahead, experiment, push boundaries, and create interfaces that not only function flawlessly but also leave a lasting impression on users.

 


You might also like:

Recommended Post
Featured Post
How To Image Upload In CKeditor With Laravel 10
How To Image Upload In CKedito...

In this article, we will see how to image upload in CKEditor with laravel 10. Here, we will learn about image uploa...

Read More

May-08-2023

Laravel Firebase Push Notification
Laravel Firebase Push Notifica...

In this example we will see laravel firebase push notification, firebase notification through you can notify u...

Read More

Mar-26-2021

Datatables Show And Hide Columns Dynamically In jQuery
Datatables Show And Hide Colum...

In this article, we will see how to hide and show columns in datatable in jquery. This example shows how you can ma...

Read More

Jun-07-2022

How To Install Bootstrap In React JS
How To Install Bootstrap In Re...

In this article, we will see how to install bootstrap in react js. Also, we will see how to use bootstrap in react...

Read More

Sep-08-2022