Sub topic 3
Setting Host Classes and Styles with Host Bindings
Assume you know basic CSS selectors, standalone Angular components, TypeScript decorators, and template property binding.
Host bindings
A component’s host element is the custom element that represents it, such as <app-card>. Use the host metadata property to bind classes and styles directly to that element:
import { Component, signal } from '@angular/core';
@Component({
selector: 'app-card',
template: `<ng-content />`,
host: {
'[class.highlighted]': 'highlighted()',
'[style.padding.px]': 'padding()',
},
})
export class CardComponent {
highlighted = signal(true);
padding = signal(24);
}The component can style these bindings with :host:
:host {
display: block;
}
:host(.highlighted) {
outline: 2px solid orange;
}Choosing an encapsulation mode
Angular supports three main ViewEncapsulation modes.
| Mode | Isolation | Typical use |
|---|---|---|
| `Emulated` | Angular scopes selectors to the component | Default application components |
| `None` | Styles become global | Deliberately shared styles |
| `ShadowDom` | Browser shadow root isolates styles | Strong native isolation |
ViewEncapsulation.None removes Angular’s style scoping. A rule such as .title { color: red; } can affect matching elements throughout the application. Use it only for intentionally global, uniquely named styles. Global stylesheets or a design-system stylesheet are often clearer.
ViewEncapsulation.ShadowDom creates a native shadow root. Document styles cannot enter it, and component styles cannot escape it. This gives strong isolation, but global resets, fonts, and utility classes may not work inside the component.
import { Component, ViewEncapsulation } from '@angular/core';
@Component({
selector: 'app-widget',
template: `<p class="note">Isolated content</p>`,
styles: `.note { color: darkgreen; }`,
encapsulation: ViewEncapsulation.ShadowDom,
})
export class WidgetComponent {}`::ng-deep` and Sass
::ng-deep pierces Angular’s emulated boundary, but it is deprecated and can create fragile dependencies on another component’s internal markup. Prefer component inputs, CSS custom properties, or a documented global style. If unavoidable, limit its reach:
:host ::ng-deep .third-party-title {
font-weight: 700;
}Sass can organize component styles with nesting, variables, and mixins. Configure a component with styleUrl: './card.component.scss'. Sass improves authoring, but it does not change Angular’s encapsulation rules.
A practical strategy
Use Emulated encapsulation by default. Use host bindings for state on the component element, global styles for application-wide rules, and CSS custom properties for theming across boundaries. Choose ShadowDom when native isolation is more important than easy integration. Choose None only when global leakage is intentional.
Exercise
Which approach should you choose to change a child component’s color without targeting its internal class?
Show answer
Expose a CSS custom property or component input. Avoid ::ng-deep because it depends on private markup.