View encapsulation and component styling
How Angular scopes a component's CSS by default, the three encapsulation modes, and why `::ng-deep` is a trap.
View encapsulation and component styling
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- Angular's default encapsulation (`Emulated`) scopes a component's styles by rewriting selectors with generated attributes — it's not real Shadow DOM, just made to behave like it.
- `:host` targets the component's own host element from inside its styles; `:host-context()` reaches up to an ancestor's class or attribute to conditionally style the host.
- `::ng-deep` punches a hole straight through encapsulation and is deprecated — reach for `:host ::ng-deep` at most, or better, restructure the child component to accept the style as an input.
CSS was never built to be scoped
Every CSS selector is, by default, global. Write .card { padding: 1rem; }
anywhere in a stylesheet and it matches every element with class="card" on the
entire page — there’s no built-in concept of “this rule only applies inside this
one component.” That’s fine for a single hand-authored stylesheet. It’s a real
problem once an app is assembled from dozens of independently-written components,
each written assuming its own class names won’t collide with anyone else’s.
Angular’s answer is view encapsulation: by default, a component’s styleUrl CSS
only affects that component’s own template, not the rest of the app, even though
nothing about plain CSS would normally guarantee that.
How Emulated actually pulls this off
The honest, slightly deflating answer: it’s not real browser-level scoping. The
real mechanism — Shadow DOM — exists and is one of the three modes Angular
supports, but it’s not the default, because Shadow DOM has its own set of
practical tradeoffs (some third-party CSS and global style resets stop reaching
in). The default mode, Emulated, fakes the effect instead: at build time,
Angular generates a unique attribute for each component (something like
_ngcontent-xyz) and adds it to every element that component renders, then
rewrites every selector in that component’s stylesheet to also require that
attribute. .card becomes, roughly, .card[_ngcontent-xyz]. The browser never
sees anything exotic — just ordinary attribute-selector CSS that happens to be
specific enough that it can’t accidentally reach into another component’s markup.
The other two modes are worth knowing exist, even if you’ll use them rarely:
ShadowDom uses the browser’s real Shadow DOM, with genuine style isolation and
its tradeoffs; None turns encapsulation off entirely, letting the component’s
styles leak globally — occasionally useful for a component whose whole job is
defining global theme variables, but a footgun anywhere else.
:host and :host-context: styling the wrapper from inside
A component’s styles can’t normally reach the element the component is attached
to — app-card { ... } in card.component.css matches nothing, because
app-card is outside the template this stylesheet is scoped to, not inside it.
:host is the pseudo-class built for exactly this gap:
:host {
display: block;
border: 1px solid #ddd;
}
That targets the host element itself — the <app-card> tag — from inside the
component’s own styles. :host-context() extends the idea outward: it still
styles the host, but conditionally, based on something further up the DOM tree:
:host-context(.theme-dark) {
background: #222;
}
That rule only applies to this component’s host if some ancestor element carries
the class theme-dark — useful for a component that needs to adapt to a
theme set higher up in the page, without that ancestor needing to know anything
about this component specifically.
::ng-deep: a hole in the wall, and why it’s deprecated
Sometimes you genuinely need to style something inside a child component’s
template — a third-party component library’s internals, say, where you can’t
just edit the child’s own stylesheet. ::ng-deep exists to punch through
encapsulation and let a selector reach down into descendant components anyway.
It’s also deprecated, for a reason worth internalizing: it works by disabling the
scoping attribute rewrite for everything after it in the selector, which means a
rule written with ::ng-deep can end up affecting far more of the DOM tree than
you intended — including other, unrelated components that happen to share a
class name. The common mitigation is scoping it to at least the current host
first (:host ::ng-deep .some-child-class), but the better fix, where you can
manage it, is to give the child component an @Input() or a CSS custom property
it exposes on purpose, so you’re styling it through an API instead of breaking
its encapsulation open.
This lesson’s video covers all three encapsulation modes directly; the two linked
videos go deeper on :host/:host-context and on why ::ng-deep specifically
is worth avoiding, with real examples of the leak it can cause.
Check yourself
If `Emulated` encapsulation isn't real Shadow DOM, what is it actually doing to keep one component's CSS from leaking into another's?
It rewrites your component's selectors and adds a generated, component-specific attribute to every element the component renders, then adds that same attribute to each CSS selector at build time. The browser sees ordinary attribute-selector CSS — there's no actual DOM-level style boundary, just selectors specific enough that they can't accidentally match another component's markup.
You want a component's host element to turn red whenever an ancestor has the class `.theme-dark`. Which selector are you reaching for?
`:host-context(.theme-dark) { color: red; }` — `:host` alone only targets the component's own host element unconditionally; `:host-context()` is the one that looks at an ancestor to decide whether to apply.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.