Open In App
Related Articles

Style Binding in Angular 8

Improve Article
Improve
Save Article
Save
Like Article
Like

It is very easy to give the CSS styles to HTML elements using style binding in Angular 8. Style binding is used to set a style of a view element. We can set the inline styles of an HTML element using the style binding in angular. You can also add styles conditionally to an element, hence creating a dynamically styled element.

Syntax:

<element [style.style-property] = "'style-value'">

Example 1:

app.component.html:

HTML




<h1 [style.color] = "'green'" 
    [style.text-align] = "'center'" >
  GeeksforGeeks
</h1>


Output:

Example 2: Setting the size of the font using style binding.

app.component.html:

HTML




<div [style.color] = "'green'" 
     [style.text-align] = "'center'" 
     [style.font-size.px]="'24'" >
  GeeksforGeeks
</div>


Output:

Example 3: Conditional styling.

app.component.html:

HTML




<div [style.color]="status=='error' ? 'red': 'green'" 
     [style.text-align] = "'center'" 
     [style.font-size.px]="'24'" >
  GeeksforGeeks
</div>


app.component.ts:

Javascript




import { Component } from '@angular/core';    
@Component({    
  selector: 'app-root',    
  templateUrl: './app.component.html',    
  styleUrls: ['./app.component.css']    
})    
export class AppComponent {   
  status = "All good";
}


Output:


Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Last Updated : 14 Sep, 2020
Like Article
Save Article
Previous
Next
Similar Reads
Complete Tutorials