If you are still using ng-deep to style child components you really should consider the alternatives.

Host styling, content projection, and CSS variables are all potential solutions for this common problem.

Let’s explore the options below 👇

#Angular @Component({   selector: 'expansion-panel',   template: `   @Component({   selector: 'expansion-panel',   template: `
First a couple of words why we need ng-deep in the first place.

In Angular, component's styles are encapsulated within the component's element so that they don't affect the rest of the application.

That has a side effect of making it difficult to style child components. @Component({   selector: 'child',   template: `<div class=&q
To alleviate this issue, Angular provides an ng-deep selector which pierces the encapsulation of the child component.

All is good then?

Not really, the selector has been deprecated for years and is not recommended by the official docs. @Component({   selector: 'child',   template: `<div class=&qThe shadow-piercing descendant combinator is deprecated and
:host selector may be used to create styles that target the component element itself, as opposed to targeting elements inside its template.

Host styles can be overridden from the parent component without the use of ng-deep.

Sometimes rearranging HTML slightly is all you need @Component({   selector: 'child',   template: `<div class=&q@Component({   selector: 'child',   template: `content`,   s
Building on that example, content projection is another way to work around the view encapsulation issue.

See how in the example below, refactoring the <app-button-group> to use content projection gives us a way to style the host element of <app-button>? @Component({   selector: 'app-button',   template: '<ng-cont@Component({   selector: 'app-button',   template: '<ng-cont
In more complex scenarios, styling host might not be enough.

If you need more flexibility, exposing your component styles as CSS variables should be the go-to solution.

CSS vars are not affected by style encapsulation so they allow for styling across component boundaries. @Component({   selector: 'expansion-panel',   template: `
Also, If you’re worried about the browser support, there is really no need to.

At the day of posting this, the support for CSS vars sits comfortably at 96.5%.

And IE11 is dead anyway (not supported by Angular 13+). CSS Variables (Custom Properties)  -Global	96.47%	+	0.1%	=	9
Lastly, since Angular 9, you can bind to CSS variables in the templates of your components by using the [style] binding.

What’s not to love? 😍 @Component({   selector: 'expansion-panel',   template: `
And that’s a wrap for the styles tip ✅

If you like the content, consider following @realTomaszKula on Twitter.

I’ll post more tips similar to this one from time to time 🅰️

• • •

Missing some Tweet in this thread? You can try to force a refresh
 

Keep Current with Tomasz Kula

Tomasz Kula Profile picture

Stay in touch and get notified when new unrolls are available from this author!

Read all threads

This Thread may be Removed Anytime!

PDF

Twitter may remove this content at anytime! Save it as PDF for later use!

Try unrolling a thread yourself!

how to unroll video
  1. Follow @ThreadReaderApp to mention us!

  2. From a Twitter thread mention us with a keyword "unroll"
@threadreaderapp unroll

Practice here first or read more on our help page!

Did Thread Reader help you today?

Support us! We are indie developers!


This site is made by just two indie developers on a laptop doing marketing, support and development! Read more about the story.

Become a Premium Member ($3/month or $30/year) and get exclusive features!

Become Premium

Don't want to be a Premium member but still want to support us?

Make a small donation by buying us coffee ($5) or help with server cost ($10)

Donate via Paypal

Or Donate anonymously using crypto!

Ethereum

0xfe58350B80634f60Fa6Dc149a72b4DFbc17D341E copy

Bitcoin

3ATGMxNzCUFzxpMCHL5sWSt4DVtS8UqXpi copy

Thank you for your support!

Follow Us on Twitter!

:(