Add custom class to Tabs – ngbTabTitleClass

Similar to #1349, I was handed a requirement that would be best solved by conditionally setting a class on the <li> generated by ngbTab, using the same type options as ngClass:

<ngb-tabset>
 <ngb-tab *ngFor="let tab of tabs">
  <template ngbTabTitle [ngbTabTitleClass]="tab.classes">tab.title</template>
  <template ngbTabContent>tab.content</template>
 </ngb-tab>
</ngb-tabset>

I’d expect this could be done by adding the input to ngbTabTitle and updating the ngbTabSet template:

@Directive({selector: 'template[ngbTabTitle]'})
export class NgbTabTitle {
  constructor(public templateRef: TemplateRef<any>) {}
  @Input() ngbTabTitleClass: string|string[]|Set<string>|{[klass: string]: any};
}
<li class="nav-item" *ngFor="let tab of tabs" [ngClass]="tab.titleTpl?.ngbTabTitleClass">
  <a [id]="tab.id" class="nav-link" [class.active]="tab.id === activeId" [class.disabled]="tab.disabled"
          href (click)="!!select(tab.id)" role="tab" [attr.aria-controls]="tab.id + '-panel'" [attr.aria-expanded]="tab.id === activeId">
    {{tab.title}}<template [ngTemplateOutlet]="tab.titleTpl?.templateRef"></template>
  </a>
</li>

3 thoughts on “Add custom class to Tabs – ngbTabTitleClass

  1. Similar to one of the examples, pasted here for your convenience:

    <ngb-tabset #t="ngbTabset">
      <ngb-tab id="configTab" title="Simple">
        <ng-template ngbTabContent>
          <p>Raw denim you probably haven't heard of them jean shorts Austin. Nesciunt tofu stumptown aliqua, retro synth
          master cleanse. Mustache cliche tempor, williamsburg carles vegan helvetica. Reprehenderit butcher retro keffiyeh
          dreamcatcher synth. Cosby sweater eu banh mi, qui irure terry richardson ex squid. Aliquip placeat salvia cillum
          iphone. Seitan aliquip quis cardigan american apparel, butcher voluptate nisi qui.</p>
        </ng-template>
      </ngb-tab>
      <ngb-tab id="tab-selectbyid2">
        <ng-template ngbTabTitle><b>Fancy</b> title</ng-template>
        <ng-template ngbTabContent>Food truck fixie locavore, accusamus mcsweeney's marfa nulla single-origin coffee squid.
          <p>Exercitation +1 labore velit, blog sartorial PBR leggings next level wes anderson artisan four loko farm-to-table
          craft beer twee. Qui photo booth letterpress, commodo enim craft beer mlkshk aliquip jean shorts ullamco ad vinyl
          cillum PBR. Homo nostrud organic, assumenda labore aesthetic magna delectus mollit. Keytar helvetica VHS salvia
          yr, vero magna velit sapiente labore stumptown. Vegan fanny pack odio cillum wes anderson 8-bit, sustainable jean
          shorts beard ut DIY ethical culpa terry richardson biodiesel. Art party scenester stumptown, tumblr butcher vero
          sint qui sapiente accusamus tattooed echo park.</p>
        </ng-template>
      </ngb-tab>
    </ngb-tabset>
    
    <p>
      <button class="btn btn-outline-primary" (click)="t.select('tab-selectbyid2')">Selected tab with "tab-selectbyid2" id</button>
    </p>

    using the “id” property on the ngb-tab component, setting it to whatever Id you want, to then be able to style it accordingly, you just need to add the relevant CSS(I use SCSS because it is 1000x easier) class, using:

    #configTab,
    #staffTab,
    #taskHoursTab {
      margin-right: 0.25em;
      color: #fff;
      background-color: #18BC9C;
      border-color: #18BC9C;
      &.active {
        border-color: #18BC9C;
        color: #18BC9C;
        background-color: #fff;    
      }
    }

    Where, #configTab, etc, all correspond with specific tabs that I named in the HTML file. I also have a version of this that just styles ALL tabs, with a generic style, that I can show you on monday. As my work laptop is at work at the moment.

  2. it worked for me by mentioning style globally as given below. Its not working in component classes.

    .nav-tabs .nav-link:hover {
    background-color: #FFFFFF !important;
    }
    .nav-tabs .nav-link:hover a {
    text-decoration: none;
    }

  3. Is there still no way to add class to ul? I already have styles that works based on the class in ul. I don’t want to rewrite the css again.