11

I'm trying to apply styles to the ngbTooltip component in my Angular 2 app. I apply the directive as:

<div [ngbTooltip]="tooltipText">
    Element text
</div>

But since Angular 2 applies style scoping, I can't directly style the .tooltip class in my component's template.

How can I give the tooltips for this specific component a custom styling?

EDIT:

I have a scss stylesheet that's attached to my component. My styles (simplified) are:

.license-circle {
    width: 10px;
    ... other styles
}

/deep/ .tooltip {
    &.in {
        opacity: 1;
    }
}

But then my rendered styles look like:

<style>
.license-circle[_ngcontent-uvi-11] {
  width: 10px; }

.tooltip.in {
  opacity: 1; }
</style>

Which makes me believe the tooltip styles are being un-encapsulated (instead of just piercing this component's children.

Note: I tried :host >>> .tooltip and it didn't work, so I ended up using /deep/.

Thanks!

Martín Coll
  • 3,368
  • 3
  • 37
  • 52
  • http://stackoverflow.com/questions/34542143/load-external-css-style-into-angular-2-component/34963135#34963135 might help (shadow piercing) – Günter Zöchbauer Sep 27 '16 at 05:25
  • It worked, thanks! But I'm worried these piercing styles will be global. – Martín Coll Sep 27 '16 at 16:35
  • What do you mean with "global"? If you start the selector with `:host ...` then they are only applied to this component and it's children. – Günter Zöchbauer Sep 27 '16 at 16:36
  • My style doesn't seem to be scoped for that element ```css [_nghost-epe-11] { display: -webkit-box; display: -ms-flexbox; display: flex; -webkit-box-pack: center; -ms-flex-pack: center; justify-content: center; } .license-circle[_ngcontent-epe-11] { .... } .tooltip.in { opacity: 1; } .tooltip .tooltip-inner { padding: 0.5rem; ... } ``` – Martín Coll Sep 27 '16 at 17:20
  • Can you please edit your question and add the code that demonstrates what you tried? – Günter Zöchbauer Sep 27 '16 at 17:20
  • arg, I'll updating my question. give me 5 – Martín Coll Sep 27 '16 at 17:22

2 Answers2

10

As mentioned in the comment, the selectors should start with :host

:host .license-circle {
    width: 10px;
    ... other styles
}

:host /deep/ .tooltip {
    &.in {
        opacity: 1;
    }
}
Günter Zöchbauer
  • 623,577
  • 216
  • 2,003
  • 1,567
4

For angular version 8.3, ::ng-deep is recommended

::host .license-circle {
  width: 10px;
  ... other styles
}

:host ::ng-deep .tooltip {
  &.in {
    opacity: 1;
  }
}

One thing that we need to remember is that ::ng-deep, /deep/ & >>> have been deprecated in angular 9. So, finding some other long term solution would be a good thing to do at this point. Check out the docs for more.

Junaid
  • 4,682
  • 1
  • 34
  • 40
Brady Huang
  • 1,852
  • 20
  • 23