Sub topic 8
Setting host classes and styles with host bindings
This chapter assumes you can write a standalone component, use signal inputs, and read :host and :host(.class) selectors. Class and style bindings in templates should also be familiar.
Why the host element needs its own bindings
A component's host element is the tag that matches its selector, such as <app-alert>. That tag sits in the parent's template. The component's own template only describes what goes inside it. So [class.error]="..." in the component's template can't reach the host.
Host bindings fix this. They are declared in the host property of the @Component metadata. Angular evaluates them against the component instance and updates the host element when the values change.
The host metadata
Each key in host names a target, and each value is an expression:
| Key | Effect |
|---|---|
| `'class': 'alert'` | Static class, always applied |
| `'[class.error]': 'expr'` | Adds the class when `expr` is truthy |
| `'[style.opacity]': 'expr'` | Sets one style property |
| `'[style.width.px]': 'expr'` | Sets a style property with a unit |
| `'[attr.aria-hidden]': 'expr'` | Sets an HTML attribute |
| `'role': 'alert'` | Static attribute |
Expressions are strings. Inside them you can read signals, inputs, and other members of the class.
A worked example
This alert component changes its host classes and styles based on its inputs. The component's styles react to those classes with :host(...) selectors.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-alert',
template: `<ng-content />`,
styles: `
:host {
display: block;
padding: 12px;
border-left: 4px solid gray;
}
:host(.error) {
border-left-color: crimson;
}
:host(.dismissed) {
display: none;
}
`,
host: {
'class': 'alert',
'role': 'alert',
'[class.error]': "severity() === 'error'",
'[class.dismissed]': 'dismissed()',
'[style.opacity]': 'dismissed() ? 0 : 1',
'[attr.aria-hidden]': 'dismissed()',
},
})
export class AlertComponent {
severity = input<'info' | 'error'>('info');
dismissed = input(false);
}A parent uses it like this:
<app-alert severity="error" [dismissed]="false">Disk is almost full</app-alert>Angular renders <app-alert class="alert error" role="alert" ...>. The :host(.error) rule matches because the class is now on the host element.
How the pieces connect
This split keeps responsibilities clear. TypeScript decides which state the host is in. CSS decides what that state looks like.
Precedence and merging
Several sources can set classes on one host element: a static class in host, a [class.x] host binding, and bindings written by the parent in its template. Angular merges them rather than replacing the whole attribute. If two sources set the same class or style property, the parent's template binding wins over the component's host binding.
That order is useful. A component can ship sensible defaults, and a consumer can still override them on a specific usage.
Host bindings versus `@HostBinding`
An older form uses the @HostBinding('class.error') decorator on a property or getter. It does the same job, but the Angular style guide prefers the host metadata. All of a component's host behavior then lives in one place, and you can read it without scanning the class body.
Common mistakes
- Putting side effects in expressions. Host binding expressions run during change detection. Keep them to simple reads, such as signals and comparisons, and move logic into a
computed(). - Styling the host from a child selector. A plain
.errorrule in the component's styles targets elements inside the template. Use:host(.error)to match the host. - Using host bindings for state the parent already controls. If the parent can write
[class.error]on the tag, a host binding adds indirection. Use host bindings when the component itself owns the state. - Forgetting the unit.
'[style.width]': '50'is invalid CSS. Use'[style.width.px]'or bind a string such as'50%'.
Exercise
A component sets '[class.compact]': 'compact()' in its host metadata. Which selector in the component's styles targets the host when it has this class, and why does .compact { ... } not work?
Show answer
Use :host(.compact) { ... }. A plain .compact selector is scoped to elements inside the component's template, so it never matches the host element, which belongs to the parent's template.
Exercise
A parent writes <app-alert [style.opacity]="0.5"> while AlertComponent has '[style.opacity]': 'dismissed() ? 0 : 1'. Which opacity does the element get when dismissed() is false, and why?
Show answer
0.5. Bindings written in the parent's template take precedence over the component's host bindings, so the consumer's value overrides the component's default.