Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

626
Views
What is the difference between using :host and not using it?

I found the usage of :host in angular is very confusing.

For example, there is a CSS file a-component.component.css, and its related HTML file is a-component.component.html whose selector is app-a-component.

And in the CSS file there is a statement

h1 {
 font-size: 12px;
}

I think this CSS only applies to the <app-a-component> HTML element beased on the following explanation, is that correct?

https://angular.io/guide/component-styles

And if I change the CSS to

:host h1 {
 font-size: 12px;
}

(adding :host)

it will still applies to the same <app-a-component> HTML element, because it is the host HTML element of this component, is that correct?

If they are both correct, why would we need :host given it is the same effect with or without it?

Thanks in advance!

about 4 years ago · Juan Pablo Isaza
1 answers
Answer question

0

In your example :host h1 { ...} there's no difference. Where the :host becomes useful is when you need to use ::ng-deep or you want to style the host element.

e.g.

You have a component <my-component></my-component> and you want to apply styles directly to that element. You can add the styles with the :host selector.

:host {
  background: red;
}

This will set the background of the my-component element to red.

For :host ::ng-deep { ... you can disable ViewEncapsulation for that rule but ONLY for descendant components. If you were to just use ::ng-deep without the :host that will become a global style.

about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!