# Class and style binding Use class and style bindings to add and remove CSS class names from an element's `class` attribute and to set styles dynamically. ## Prerequisites * [Property binding](guide/property-binding) ## Binding to a single CSS `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](guide/style-precedence#styling-delegation) for more information. ## Binding to multiple CSS classes To bind to multiple classes, type the following: `[class]="classExpression"` The expression can be one of: * A space-delimited string of class names. * An object with class names as the keys and truthy or falsy expressions as the values. * An array of class names. With the object format, Angular adds a class only if its associated value is truthy.
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']` |
## Binding to a single style
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.
1. To write a style in dash-case, type the following:
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 styling-precedence}
## Styling precedence
A single HTML element can have its CSS class list and style values bound to multiple sources (for example, host bindings from multiple directives).
## What’s next
* [Component styles](/guide/component-styles)
* [Introduction to Angular animations](/guide/animations)
@reviewed 2022-05-09