Skip to content
tezvyn:

Angular Component Styles: Scoped by Default

Source: angular.devEasyHow cards are made

Angular Component Styles: Scoped by Default

Angular styles are scoped to their component by default, preventing one component's styles from accidentally breaking another. This lets you build reusable UI pieces without style conflicts. The footgun is using ::ng-deep, which breaks this isolation.

Why it exists

CSS is global by nature. In large applications with many components, it's easy for styles to clash. A style for .button in one component could unintentionally override the style for .button somewhere else, leading to hard-to-debug visual bugs. Angular component styles solve this by creating isolated style scopes for each component.

The mental model

Imagine each Angular component is a ship in a bottle. The styles you define are inside that bottle and only affect the ship within it. By default, Angular doesn't use a real 'bottle' (like native Shadow DOM), but it cleverly fakes one. It adds a unique tracking attribute to your component's HTML and rewrites your CSS rules to include that attribute, ensuring they only apply locally.

How it works

Angular uses a setting called ViewEncapsulation to control style scoping. The default is Emulated. In this mode, Angular processes your CSS. For a component with a unique attribute like _ngcontent-c123, a rule like p { color: blue; } becomes p[_ngcontent-c123] { color: blue; }. It also adds that unique attribute to all p elements in that component's template. This makes the style specific to that component instance. Other modes include ShadowDom (uses the browser's native feature) and None (disables scoping, making styles global).

When to use it

Use component-scoped styles for almost all component development. It's the idiomatic Angular way to create modular, self-contained, and reusable UI elements. This approach prevents your component's styles from interfering with the rest of the application, and vice-versa.

When not to use it

The main escape hatch is ::ng-deep, which the Angular team strongly discourages. It's a legacy tool for situations where you must style a child component from a parent. A better approach is to use global styles for application-wide rules or pass data to the child to let it style itself. Setting encapsulation to None is also rare and makes all of the component's styles global, which should be done with caution.

One canonical example

A component defines a style for an image to be circular.

@Component({
selector: 'profile-photo',

styles: 'img { border-radius: 50%; }', template: '<img src="photo.jpg">',

})
export class ProfilePhoto {}

When this component is rendered, Angular ensures that only img tags within this component get the border-radius: 50% style, not every img tag on the page.

Interview question

How does Angular primarily achieve style isolation for components by default?

  • a.By adding unique attributes to component elements and modifying CSS selectors to include them.Correct
  • b.By utilizing the browser's native Shadow DOM for each component.
  • c.By automatically generating unique class names for all elements within a component's template.
  • d.By compiling all component styles into a single, highly specific global stylesheet.
Why?

Angular's default ViewEncapsulation (Emulated) works by adding unique attributes to a component's HTML elements and then rewriting the component's CSS rules to target only those elements with the specific attribute. Option B is incorrect because while Shadow DOM is a ViewEncapsulation mode, it is not the default; Angular 'fakes' it by default.

Just read this? Test yourself on what you have been reading.

Read the original → angular.dev

You just looked this up. Could you explain it out loud?

That is the part interviews actually test. Tezvyn takes questions like this one and gives you what the interviewer is really checking, the answer that lands, and the mistake that ends the conversation, in the four minutes before your next meeting.

The iPhone app is on the way

We are building it. Until it lands, nothing here is held back from you: every interview card, your saved cards, streaks and the job board all work in Safari, plus hundreds of free practice quizzes of thirty questions each. Sign in and it all carries over to the app the day it arrives.

Want it as an icon? Tap Share at the bottom of Safari, then Add to Home Screen. It opens full screen and the cards you have read stay available offline.

Get it on Google PlayiPhone app coming soon

We are hiring for this. Open roles that interview on angular — each one lists the topics its interview covers.

See open roles