DEV Community

Nhan Nguyen
Nhan Nguyen

Posted on

Angular routerLinkActive directive

Have you ever wondered how to highlight a specific HTML element, such as a link or a button, when a particular URL is selected? Something similar to this:

Image description

In Angular, there is a specific directive for this called routerLinkActive. Here is how you can use it:

Image description

The value set to routerLinkActive is the name of a CSS class (or several CSS classes) that gets applied to the HTML element when the current URL path matches the path of the routerLink directive on that same element. In other words, if the path is page1, then the button element has an active class applied to it.

Image description

As a result, all I did to style the above example was add that CSS class definition to my global CSS file:

Image description

And that's it! Note that the directive supports several other options documented here:

Image description


I hope you found it useful. Thanks for reading. 🙏

Let's get connected! You can find me on:

Heroku

Simplify your DevOps and maximize your time.

Since 2007, Heroku has been the go-to platform for developers as it monitors uptime, performance, and infrastructure concerns, allowing you to focus on writing code.

Learn More

Top comments (0)

Billboard image

The Next Generation Developer Platform

Coherence is the first Platform-as-a-Service you can control. Unlike "black-box" platforms that are opinionated about the infra you can deploy, Coherence is powered by CNC, the open-source IaC framework, which offers limitless customization.

Learn more

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay