5

In a Bootstrap 3 navbar when a link is active, the link background color is changed to show that its the active link. This seems to be missing in Bootstrap 4. Is there a way to show this or do I need to override the active class?

The Bootstrap 3 navbar shown below. You can see the active home link is highlighted How do I show this in Bootstrap 4.

enter image description here

The angular 5 code below

  <div class="navbar-collapse" [ngbCollapse]="navbarCollapsed" id="navbarContent">
    <ul class="nav navbar-nav mr-auto">
      <li class="nav-item" [routerLinkActive]="['active']" [routerLinkActiveOptions]="{exact:true}">
        <a class="nav-link" [routerLink]="['/home']" (click)="navbarCollapsed = true">HOME</a>
      </li>
      <li class="nav-item" [routerLinkActive]="['active']">
        <a class="nav-link" [routerLink]="['/page1']" (click)="navbarCollapsed = true">Page 1</a>
      </li>
    </ul>
  </div>
BeesNees
  • 191
  • 2
  • 3
  • 13

4 Answers4

6

Bootstrap 4 doesn't have css for active class.

So you have to add css by your self

Simply add css like

.active {
   background:#4FA9DC; color:#000;
}
Leandro Bardelli
  • 10,561
  • 15
  • 79
  • 116
0

The [routerLinkActive] property defines the css class which will be appended to the element once it matches the defined route.

So in your case, the active class will be appended to the element. What you need to do is define a the active class in your css file, as @JavascriptHuppTechnologies suggested in the comments.

filipbarak
  • 1,835
  • 2
  • 17
  • 28
0

The .active class is being applied, but it won't be visible unless the Navbar has the navbar-dark or navbar-light classes which it used to determine the color of the active links...

  • Use navbar-dark for a light/white links on darker backgrounds
  • Use navbar-light for a dark/gray links on lighter backgrounds
Carol Skelly
  • 351,302
  • 90
  • 710
  • 624
0
<nav class="nav nav-pills">
  <a class="nav-link" [routerLink]="['your_path1']" [routerLinkActive]="'active'">item1</a>
  <a class="nav-link" [routerLink]="['your_path2']" [routerLinkActive]="'active'">item2</a>
  <a class="nav-link" [routerLink]="['your_path3']" [routerLinkActive]="'active'">item3</a>
</nav>

use nav-pills for background color and add the class 'active' for routerLinkActive

IAfanasov
  • 4,775
  • 3
  • 27
  • 42