If you’re creating page by page, you can use that advantage to do so.
For example, let’s imagine that we have 2 pages: index.html
and sobre.html
, and we want to highlight the link of the current page as we navigate from one to the other.
Well, first let’s create a CSS style responsible class for this featured link:
.paginaAtual {
color:#fff;
background-color:#71953E;
}
So now we’re going to do the following - On the page index.html navigation links will be created as follows:
<div id="menu">
<ul>
<li class="linkMenu paginaAtual"><a href="index.html">Index page</a></li>
<li class="linkMenu"><a href="sobre.html">Sobre</a></li>
</ul>
</div>
While on the page about.html links will be created like this:
<div id="menu">
<ul>
<li class="linkMenu"><a href="index.html">Index page</a></li>
<li class="linkMenu paginaAtual"><a href="sobre.html">Sobre</a></li>
</ul>
</div>
If the above example is not an option, here is an alternative to this:
In this second option, we will use Javascript, more precisely the jQuery library. To use the jQuery library if you’re not familiar with it, we need to implement it in <head>
of our document/website using the following line of code:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.3/jquery.min.js"></script>
This line of code above will implement the jQuery library hosted by Google, but it could also be hosted by you on your server. Then we will create our menu:
<div id="menu">
<ul>
<li class="linkMenu"><a href="index.html">Index page</a></li>
<li class="linkMenu"><a href="sobre.html">Sobre</a></li>
</ul>
</div>
$("a[href*='" + location.pathname + "']").addClass("paginaAtual");
This line of Javascript code above needs to be implemented within the tag
<script></script>
in your document.
linked
– Chun