How to Style Current Page Navigation Link

0


The above image is a print of the menu that I'm developing that got the style I put right in CSS. But only when I pass the mouse over the link, that is when I do :hover which was what I was doing in that print.

Now I want to configure for example:
I click on página 1 , there it will take me to the página 1 and the background of that same link will be highlighted / highlighted in green as in the image example above, but not in the Geral page, only in the current page since I am not on page Geral , and vice versa. How do I?

    
asked by anonymous 21.07.2015 / 16:57

1 answer

3

If you're creating page by page, you can use this advantage to do this.
For example, let's say 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 class responsible for CSS style for this featured link:

.paginaAtual {
    color:#fff;
    background-color:#71953E;
}

So now we will do the following - In the index.html page the 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 about.html page, 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 are 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>

ThislineofcodeabovewillimplementthejQuerylibraryhostedbyGoogle,butcouldalsobehostedbyyouonyourserver.Afterthatwewillcreateourmenu:

<divid="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.

    
23.07.2015 / 06:02