Skip to main content

6 Ways to Bind Styles in Angular – From Basics to Advanced!

Styling elements dynamically in Angular can be achieved in multiple ways. Let's explore different methods to bind styles in an Angular template. 

Method 1: Inline Style 

The simplest way to apply styles directly in HTML is by using the style attribute. 

<pstyle="color:red">Start editing to see some magic happen :)</p> 

 

Method 2: Binding Style Using Angular Property Binding 

You can bind a specific style property dynamically using Angular's property binding: 

 

<p[style.color]="'yellow'">Start editing to see some magic happen :)</p> 


 

Method 3: Using ViewChild and Renderer2 

This method allows us to manipulate DOM elements dynamically using @ViewChild and Renderer2. 

 

<p#a1>Start editing to see some magic happen :)</p> 

 

In the Component: 

  @ViewChild('a1') paragraph!: ElementRef; 

 

constructor(privaterender: Renderer2) {} 

 

  ngAfterViewInit() { 

    this.render.setStyle(this.paragraph.nativeElement, 'color', 'green'); 

  } 

 

Method 4: Using ngStyle Directive 

Using [ngStyle] allows us to bind an object containing multiple styles dynamically. 

 

<p[ngStyle]="onP">Start editing to see some magic happen :)</p> 

 

In the Component: 

  onP = { 

    color:'Blue', 

  }; 

 

Method 5: Using a Method to Return Style Object 

You can also use a function to dynamically return styles. 

 

<p[ngStyle]="getStyle()">Start editing to see some magic happen :)</p> 

In the Component: 

  getStyle() { 

    return { 

      color:'brown', 

    }; 

 

Method 6: Using API Data to Apply Styles Dynamically 

You can apply styles dynamically using data fetched from an API or defined in an array. 

 

<div*ngFor="letitemofprndet; leti = index"> 

  <p[ngStyle]="item.collblcmd">{{ item.colval }}</p> 

</div> 

 

Example Data: 

prndet = [ 

    { 

      prntid:'309', 

      colval:'Start editing to see some magic happen :)', 

      field:'sname', 

      tbname:'vou1', 

      collbl:'Name Of Party : ', 

      collblcmd: { 

        // 'font-family': 'Arial', 

        //'font-size': '12px', 

        color:'pink', 

        // 'font-weight': 'normal', 

        // 'text-decoration': 'none', 

        // 'font-style': 'normal', 

        // transform: 'translate3d(10px,148px,0px)', 

        // position: 'relative', 

        //  width: '92px', 

        'text-align':'left', 

        'line-height':'12px', 

        'word-wrap':'break-word', 

      }, 

    }, 

    // Additional objects can be added here 

 ]; 

} 

 

Conclusion 

Each method has its use case: 

  • Inline styles are quick but not dynamic. 

  • Property binding is useful for setting individual styles dynamically. 

  • ViewChild with Renderer2 is useful for DOM manipulations after the view initializes. 

  • ngStyle provides a clean way to apply multiple styles dynamically. 

  • Using a method can allow conditional styling. 

  • Fetching styles from an API makes styles completely data-driven. 

Choose the best method based on your project needs!  

 

Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.