Sub topic 7
Theming across component boundaries with CSS custom properties
This chapter assumes you know basic CSS selectors and specificity, can write a standalone Angular component with the @Component decorator, and know that Emulated encapsulation scopes component styles to the component's own template.
The problem: scoped styles block outside rules
Emulated encapsulation stops a parent's or global stylesheet's selectors from reaching into a child component's template. That isolation is useful, but it also means a parent cannot simply write app-card .title { ... } to restyle a child's internals.
CSS custom properties solve this without breaking encapsulation. A custom property (also called a CSS variable) is a name starting with -- that holds a value. Custom properties inherit down the DOM tree. Inheritance is not a selector match, so it crosses the encapsulation boundary. The child component declares which properties it reads, and any ancestor can set them.
Define the extension points in the component
A component reads a custom property with var(). The second argument is a fallback that applies when no ancestor has set the property.
import { Component, input } from '@angular/core';
@Component({
selector: 'app-card',
template: `<ng-content />`,
styles: `
:host {
display: block;
padding: var(--card-padding, 16px);
background: var(--card-background, white);
}
:host(.highlighted) {
outline: 2px solid orange;
}
`,
host: {
'[class.highlighted]': 'highlighted()',
},
})
export class CardComponent {
highlighted = input(false);
}The component works with no setup, because the fallbacks supply 16px and white. The names --card-padding and --card-background form the component's public styling API. Choose them deliberately and keep them stable, the same way you would keep an input() name stable.
Set the values from outside
Any ancestor can set the properties. A global stylesheet can set them on the component's host element.
/* styles.css (global) */
app-card {
--card-background: #f8fafc;
--card-padding: 24px;
}The browser resolves var(--card-background, white) inside the card using the value inherited from the app-card element. The global rule never selects anything inside the card's template, so encapsulation stays intact.
You can also set the properties on a wrapper element, and every card inside it picks them up.
.dark-section {
--card-background: #1e293b;
--card-padding: 8px;
}Common errors
- Setting the property on the wrong element. A property set on a child of the card does not reach the card's host. Set it on the host or an ancestor.
- Forgetting the fallback. Without a fallback, an unset property makes the whole declaration invalid at computed time, so the property falls back to its initial or inherited value. For example, an unset
backgroundbecomes transparent. - Expecting a rule to override an inner declaration. If the component sets the same custom property on its own
:host, that value wins over an inherited one, because a rule on the element itself beats inheritance. Read the property withvar()and never redeclare it inside the component. - Using custom properties as selectors. They carry values only. You cannot use them to turn rules on or off.
Works with ShadowDom too
Custom properties also pass through a native shadow root, because inheritance crosses shadow boundaries. This makes them the standard way to theme a component that uses ViewEncapsulation.ShadowDom, where global selectors cannot reach inside at all.
When to choose custom properties
Prefer custom properties over ::ng-deep or ViewEncapsulation.None when you control the child component. The child decides what is configurable, and the parent cannot break its internal structure. If you do not control the child, such as a third-party component with no exposed properties, you may need another approach, which a later chapter covers.
Exercise
A global rule sets --card-background: #f8fafc on app-card, but the card still renders a white background. The card's styles use background: white;. What change makes the theme work, and why does it work without piercing encapsulation?
Show answer
Change the declaration to background: var(--card-background, white);. The custom property inherits from the app-card host into the card's template, and var() reads it. Only the value crosses the boundary. No selector reaches into the component.