Skip to content

Conversation

@Gouvernathor
Copy link
Contributor

@Gouvernathor Gouvernathor commented Nov 29, 2025

Clarify usage of Angular's ::ng-deep pseudo class and its implications for global styles.

This is a documentation-only change, that explains how ::ng-deep actually works, explaining in effect what was mentioned in #25160.

I will also be proposing a change to the discouragement line - though I expect it will require more discussion since it explicitly engages "the Angular team" - to only discourage CSS rules where ::ng-deep appears at the start of the rule / applies to the whole selector.
In my view, it is perfectly useful and legitimate to want a component to have style authority on both its template and the children that it's provided with. It's even less drastic than the other ViewEncapsulation modes.

@pullapprove pullapprove bot requested a review from atscott November 29, 2025 14:43
@angular-robot angular-robot bot added area: docs Related to the documentation area: common Issues related to APIs in the @angular/common package labels Nov 29, 2025
@ngbot ngbot bot added this to the Backlog milestone Nov 29, 2025
@angular angular deleted a comment from google-cla bot Nov 29, 2025
@github-actions
Copy link

github-actions bot commented Nov 30, 2025

Deployed adev-preview for 8acb318 to: https://ng-dev-previews-fw--pr-angular-angular-65718-adev-prev-14gyzu2s.web.app

Note: As new commits are pushed to this pull request, this link is updated after the preview is rebuilt.

@Gouvernathor
Copy link
Contributor Author

Gouvernathor commented Dec 1, 2025

I'll wonder about how to make it lint-compliant if the wording and general idea is accepted.

The change in the "discouragement" line that I mentioned above, will go in a separate PR, unless someone wants me to add it here and now.

*after* `::ng-deep` will be matched with elements irrespective of whether they are inside the component's template.
For instance:
- a CSS rule whose selector is `p a` will match `<a>` elements inside the component's template, which are also
descendants of a `<p>` element that's itself also inside the component's template. That's Angular's default behavior.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
descendants of a `<p>` element that's itself also inside the component's template. That's Angular's default behavior.
When using the emulated encapsulation, a selector like `p a`, Angular matches `<a>` elements within the component’s own template that are descendants of a `<p>` element.

Copy link
Contributor Author

@Gouvernathor Gouvernathor Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I disagree with that one. qi think it's important to mention that (1) it's the default behavior in Angular, the baseline against which the rest compares, and (2) that the <p> also needs to be in the template.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docs already mentions that emulated encapsulation is the default. I agree about 2., it can be improved.

For instance:
- a CSS rule whose selector is `p a` will match `<a>` elements inside the component's template, which are also
descendants of a `<p>` element that's itself also inside the component's template. That's Angular's default behavior.
- `::ng-deep p a` will match `<a>` elements anywhere in the page, descendants of a `<p>` element anywhere in the page.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A selector like ::ng-deep p a matches anchor elements anywhere in the application, as long as they are descended from a paragraph element anywhere in the application. In effect, this behaves like a global style.

descendants of a `<p>` element that's itself also inside the component's template. That's Angular's default behavior.
- `::ng-deep p a` will match `<a>` elements anywhere in the page, descendants of a `<p>` element anywhere in the page.
That effectively makes it a global style.
- `p ::ng-deep a` will match `<a>` elements anywhere in the page, but only those that are descendents
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `p ::ng-deep a` will match `<a>` elements anywhere in the page, but only those that are descendents
- In `p ::ng-deep a`, Angular requires the paragraph element to come from the component’s own template, but the anchor element can be anywhere in the application. This means the anchor element may be in the component’s template or in any of its projected or child content.

- `p ::ng-deep a` will match `<a>` elements anywhere in the page, but only those that are descendents
of a `<p>` element inside the component's template.
So, effectively, the `<a>` can only be in the component's template, or among the component's children.
- `:host ::ng-deep p a` will match `<a>` elements descendants of `<p>` elements, both being descendants of the component.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
- `:host ::ng-deep p a` will match `<a>` elements descendants of `<p>` elements, both being descendants of the component.
- With `:host ::ng-deep p a`, both the paragraph element and the anchor element must be descendants of the component’s host element. They can appear in the component’s template or in the views of its child components, but not elsewhere in the app.

@JeanMeche
Copy link
Member

The content is correct but mostly needs rephrasing, can you PTAL at the suggestions. Thank you.

@atscott atscott removed their request for review December 1, 2025 18:50
@Gouvernathor
Copy link
Contributor Author

I rephrased using a mix of the suggested terms and my own formulations. Let me know if that's good.

@JeanMeche JeanMeche added the action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews label Dec 1, 2025
@JeanMeche
Copy link
Member

can you make sure to squash all the commits to satisfy the linter. Thank you.

@JeanMeche
Copy link
Member

we still the squash into a single commit.

@Gouvernathor
Copy link
Contributor Author

You need me to squash all the commits into one, before squashing the commits when merging ?

@JeanMeche
Copy link
Member

Yes, we only squash fixups and we want that change to be a single commit.

@Gouvernathor
Copy link
Contributor Author

Bump, this seems ready to ship

@JeanMeche JeanMeche added action: merge The PR is ready for merge by the caretaker memory leak Issue related to a memory leak target: patch This PR is targeted for the next patch release action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews action: merge The PR is ready for merge by the caretaker labels Dec 8, 2025
@JeanMeche
Copy link
Member

Looks like we still have a formatting issue, can you PTAL.

@angular-robot angular-robot bot requested a review from JeanMeche December 8, 2025 18:18
@JeanMeche JeanMeche added action: merge The PR is ready for merge by the caretaker and removed action: cleanup The PR is in need of cleanup, either due to needing a rebase or in response to comments from reviews labels Dec 8, 2025
@alxhub alxhub merged commit 5607f51 into angular:main Dec 9, 2025
19 checks passed
@alxhub
Copy link
Member

alxhub commented Dec 9, 2025

This PR was merged into the repository. The changes were merged into the following branches:

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

action: merge The PR is ready for merge by the caretaker adev: preview area: common Issues related to APIs in the @angular/common package area: docs Related to the documentation memory leak Issue related to a memory leak target: patch This PR is targeted for the next patch release

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants