简体   繁体   中英

How to style ng-bootstrap components in Angular 2

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!

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

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

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM