Within a large application, I am managing a micro site and looking to implement tertiary navigation with an active state for pages. This involves inserting a single html blob into multiple pages.
My goal is to use an active
class to highlight the page in the tertiary navigation that the user is currently on.
<nav>
<ul>
<li class="active"><a href="#">Page 1</a></li>
<li><a href="#">Page 2</a></li>
<li><a href="#">Page 3</a></li>
</ul>
</nav>
Since I lack control over the backend application but have full control of html, css
, I aim to avoid solutions involving jQuery to detect urls.
The CSS approach would be:
nav li:target {background:#ccc}
Instead of:
nav li.active {background:#ccc}
I have observed similar concepts applied to anchor
tags like this.
The inquiry is whether it's possible to achieve this for different urls and how to properly utilize the :target
class.
Based on feedback, this is my current implementation:
<nav>
<ul>
<li><a id="page1" href="page1.html">Page 1</a></li>
<li><a id="page2" href="page2.html">Page 2</a></li>
<li><a id="page3" href="page3.html">Page 3</a></li>
</ul>
</nav>
However, this method does not work consistently across multiple pages. For a functioning project example, refer to this GitHub repository.