Sub topic 3
Styling the host element with :host
This chapter assumes you can write a standalone component with @Component, and that you know how Emulated encapsulation scopes styles to a template. If not, review the previous chapter first.
Why the host needs its own selector
Every component is attached to the page through a host element, which is the element matched by its selector. For selector: 'app-badge', the host is the <app-badge> tag. The host is not part of the component's own template. It lives in the parent's template.
A rule like app-badge { display: block; } inside the component's styles never matches anything, because the component's styles are scoped to the elements in its own template. The :host pseudo-class selects the host element from inside the component's styles.
Custom element tags default to display: inline. For a component that needs width, height, or vertical padding, setting display on the host is usually the first thing to do.
How :host works under Emulated encapsulation
Angular marks each host element with a generated attribute such as _nghost-abc-c1. It marks each element in the template with a matching _ngcontent-abc-c1. At build time it rewrites your selectors to include those attributes.
You never write these attributes yourself. They explain why styles stay scoped.
The :host forms
| Selector | Matches |
|---|---|
| `:host` | The host element, always |
| `:host(.highlighted)` | The host only when it has the class `highlighted` |
| `:host(:hover)` | The host while the pointer is over it |
| `:host-context(.dark)` | The host when it or any ancestor has the class `dark` |
:host(...) takes a selector for the host itself. It does not match descendants. To style a child of the host conditionally, combine it with a descendant selector, as in :host(.highlighted) .label.
:host-context(...) lets a component react to a theme set higher in the page, for example a dark class on <body>. It is a convenient way to read ambient state. A CSS custom property, covered in a later chapter, is often a cleaner way to pass theme values in.
A worked example
This badge sets its own display, changes when a class is added to it, and adapts to a dark ancestor.
import { Component } from '@angular/core';
@Component({
selector: 'app-badge',
template: `<span class="label"><ng-content /></span>`,
styles: `
:host {
display: inline-block;
}
:host(.highlighted) .label {
background: gold;
}
:host-context(.dark) .label {
background: #334155;
color: white;
}
.label {
padding: 2px 8px;
border-radius: 4px;
background: #e0e7ff;
}
`,
})
export class BadgeComponent {}A parent adds the class on the tag it controls:
import { Component } from '@angular/core';
import { BadgeComponent } from './badge.component';
@Component({
selector: 'app-root',
imports: [BadgeComponent],
template: `<app-badge class="highlighted">New</app-badge>`,
})
export class AppComponent {}The parent owns the highlighted class because the host element sits in its template. The badge decides what that class means visually.
Common errors
- Targeting the host by tag name.
app-badge { ... }in the component's own styles has no effect. Use:host. - Forgetting
display. Settingwidth,height, ormarginon an inline host does not behave as expected. Adddisplay: blockorinline-block. - Putting a descendant selector inside the parentheses.
:host(.a .b)is not valid for this purpose. Write:host(.a) .b. - Expecting
:host-contextto be a stable choice everywhere. Native shadow DOM support for it is not uniform across browsers. Prefer custom properties if you useViewEncapsulation.ShadowDom.
Exercise
A component with selector "app-panel" has the style "app-panel { display: block; }" in its own styles, but the panel still renders inline. Why, and how do you fix it?
Show answer
Component styles are scoped to the component's template, and the host element is not in that template, so the "app-panel" selector matches nothing. Replace it with ":host { display: block; }".