How to set color of active item in mat-nav-list in angular 6

In Angular Materials 9.0 you have to change color for following class:

a.mat-list-item.active { color: #ccc; }

This can be done if you're using Angular Router with the routerLinkActive attribute.

From the documentation for the RouterLinkActive directive:

Lets you add a CSS class to an element when the link's route becomes active.

Description

This directive lets you add a CSS class to an element when the link's route becomes active.

Consider the following example:

<a routerLink="/user/bob" routerLinkActive="active-link">Bob</a>

When the URL is either /user or /user/bob, the active-link class will be added to the <a> tag. If the URL changes, the class will be removed.

The code below showcases a typical use case:

<mat-nav-list>
  <a mat-list-item routerLink="/home" routerLinkActive="active-list-item">Home</a>
  <a mat-list-item routerLink="/settings" routerLinkActive="active-list-item">Settings</a>
  <a mat-list-item routerLink="/about" routerLinkActive="active-list-item">About</a>
</mat-nav-list>
.active-list-item {
  color: #3F51B5 !important; /* Note: You could also use a custom theme */
}

A couple of notes:

  • You can change active-list-item to whatever class name you would like to be applied.
  • The !important declaration in the second code snippet is used as the list item styles take precedence over your custom styles.

Here's a Stackblitz.


hope I'm not too late for this one. I had same problem with mat-nav-list and mat-menu so with few lines of css I could make it function as I wanted it, try it and see if it fits your needs, scss below:

[class^='mat-list-'] {
  &:hover:active {
    background: rgba(255, 255, 255, 0.04);
  }
  &:focus,
  .mat-list-text {
    background: none !important;
  }
}

[mat-menu-item].is-active,
[mat-list-item].is-active {
  background: hsla(0, 0%, 100%, 0.1) !important;
}

Now I have custom dark theme so you might want to set colors after your choice. Just add routerLinkActive="is-active" and you are set to go.

/Best regards