Use class and style bindings to add and remove CSS class names from an element's class
attribute and to set styles dynamically.
class
To create a single class binding, type the following:
[class.sale]="onSale"
Angular adds the class when the bound expression, onSale
is truthy, and it removes the class when the expression is falsy—with the exception of undefined
. See styling delegation for more information.
To bind to multiple classes, type the following:
[class]="classExpression"
The expression can be one of:
With the object format, Angular adds a class only if its associated value is truthy.
With any object-like expression—such as
object
,Array
,Map
, orSet
—the identity of the object must change for Angular to update the class list. Updating the property without changing object identity has no effect.
If there are multiple bindings to the same class name, Angular uses styling precedence to determine which binding to use.
The following table summarizes class binding syntax.
Binding Type | Syntax | Input Type | Example Input Values |
---|---|---|---|
Single class binding | [class.sale]="onSale" | boolean | undefined | null |
true , false
|
Multi-class binding | [class]="classExpression" | string | "my-class-1 my-class-2 my-class-3" |
Multi-class binding | [class]="classExpression" | Record<string, boolean | undefined | null> | {foo: true, bar: false} |
Multi-class binding | [class]="classExpression" | Array<string> | ['foo', 'bar'] |
To create a single style binding, use the prefix style
followed by a dot and the name of the CSS style.
For example, to set the width
style, type the following: [style.width]="width"
Angular sets the property to the value of the bound expression, which is usually a string. Optionally, you can add a unit extension like em
or %
, which requires a number type.
To write a style in dash-case, type the following:
<nav [style.background-color]="expression"></nav>
To write a style in camelCase, type the following:
<nav [style.backgroundColor]="expression"></nav>
To toggle multiple styles, bind to the [style]
attribute—for example, [style]="styleExpression"
. The styleExpression
can be one of:
"width: 100px; height: 100px; background-color: cornflowerblue;"
.{width: '100px', height: '100px', backgroundColor: 'cornflowerblue'}
.Note that binding an array to [style]
is not supported.
When binding
[style]
to an object expression, the identity of the object must change for Angular to update the class list. Updating the property without changing object identity has no effect.
@Component({ selector: 'app-nav-bar', template: ` <nav [style]='navStyle'> <a [style.text-decoration]="activeLinkStyle">Home Page</a> <a [style.text-decoration]="linkStyle">Login</a> </nav>` }) export class NavBarComponent { navStyle = 'font-size: 1.2rem; color: cornflowerblue;'; linkStyle = 'underline'; activeLinkStyle = 'overline'; /* . . . */ }
If there are multiple bindings to the same style attribute, Angular uses styling precedence to determine which binding to use.
The following table summarizes style binding syntax.
Binding Type | Syntax | Input Type | Example Input Values |
---|---|---|---|
Single style binding | [style.width]="width" | string | undefined | null | "100px" |
Single style binding with units | [style.width.px]="width" | number | undefined | null | 100 |
Multi-style binding | [style]="styleExpression" | string | "width: 100px; height: 100px" |
Multi-style binding | [style]="styleExpression" | Record<string, string | undefined | null> | {width: '100px', height: '100px'} |
A single HTML element can have its CSS class list and style values bound to multiple sources (for example, host bindings from multiple directives).
© 2010–2023 Google, Inc.
Licensed under the Creative Commons Attribution License 4.0.
https://angular.io/guide/class-binding