Sub topic 13
Choosing a styling strategy for your application
This chapter assumes you know basic CSS (selectors, specificity), standalone components, the @Component decorator, and the Emulated, ShadowDom and None encapsulation modes covered earlier. It pulls those tools into one decision process.
Start from the default
Emulated encapsulation is the default for good reason. Angular adds generated attributes to your template elements and rewrites your selectors to match them, so a rule like .label applies only inside its own component. Most components need nothing more.
Move away from the default only when a concrete requirement forces it. Each alternative below solves a specific problem and has a cost.
Match the problem to the tool
| Situation | Preferred tool | Cost or risk |
|---|---|---|
| Style a component's own template | Component `styles` or `styleUrl` | None |
| Style the component's host element | `:host` and `:host(.class)` | None |
| Toggle appearance from state | `[class.x]` and `[style.prop]` bindings | None |
| Set host classes from inputs | `host` metadata bindings | None |
| Let a parent adjust a child's look | CSS custom properties | You must design the variable names |
| Reset, typography, app-wide tokens | Global stylesheet | Rules apply everywhere |
| Strict isolation, such as an embeddable widget | `ViewEncapsulation.ShadowDom` | Global styles do not reach in |
| Style markup you do not own | `:host ::ng-deep` | Deprecated, may be removed |
| Component must be restyled by any outside CSS | `ViewEncapsulation.None` | Styles leak to the whole page |
A decision flow
Work top to bottom and stop at the first answer that fits.
A worked example
Suppose you build a card that the app themes and that can be highlighted. Components expose the knobs as custom properties, and the app sets values globally.
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);
}/* styles.css (global) */
app-card {
--card-background: #f8fafc;
--card-padding: 24px;
}Each layer has one job. The component owns its structure and defaults. The host binding turns an input into a class. The global stylesheet supplies theme values. Nothing pierces encapsulation.
When ng-deep or None is justified
Third-party components render markup you cannot edit, and they expose no custom properties. In that case, scope the override so it cannot leak:
:host ::ng-deep .third-party-title {
font-weight: 700;
}The :host prefix limits the rule to descendants of this component. Without it, the rule becomes global.
ViewEncapsulation.None suits a small number of cases, such as a component that deliberately ships styles for a whole page region. Because the styles become global, use specific class names. Two components that both define .title will override each other depending on load order.
Common mistakes
- Putting page-wide rules in a component's
styles. Emulated encapsulation silently limits them to that component. - Using
ViewEncapsulation.Noneto avoid learning custom properties. - Writing
::ng-deep .xwithout:host, which leaks globally. - Expecting global styles to reach into a
ShadowDomcomponent. Only inherited properties and custom properties cross the shadow boundary. - Duplicating the same colors in many components instead of defining tokens once.
A team rule of thumb
- Default to Emulated with component styles.
- Use bindings for state, and host bindings for host state.
- Define design tokens as custom properties in the global stylesheet.
- Read tokens in components with
var(--name, fallback). - Use ShadowDom only for true isolation needs.
- Allow
::ng-deepandNoneonly with a written reason in code review.
Exercise
A third-party date picker renders an inner .title element you cannot edit, and it has no theming options. You want it bold only inside your app-booking component. Which approach do you use, and how do you keep it from leaking?
Show answer
Use ::ng-deep as a last resort, prefixed with :host, in app-booking's styles: :host ::ng-deep .title { font-weight: 700; }. The :host prefix limits the rule to descendants of app-booking, so no other .title on the page is affected. Add a comment naming the library, because ::ng-deep is deprecated.
Exercise
Your app has a dark theme. Several components hard-code #ffffff backgrounds. What strategy lets you switch themes without editing each component's CSS rules?
Show answer
Define custom properties such as --surface in the global stylesheet (with different values under a theme class), and have each component use background: var(--surface, white). Custom properties inherit through the DOM, so they cross Emulated and ShadowDom boundaries.