Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change link color of the current page with CSS

How does one style links for the current page differently from others? I would like to swap the colors of the text and background.

HTML:

<ul id="navigation">     <li class="a"><a href="/">Home</a></li>     <li class="b"><a href="theatre.php">Theatre</a></li>     <li class="c"><a href="programming.php">Programming</a></li>  </ul> 

CSS:

li a{     color:#A60500; }  li a:hover{     color:#640200;     background-color:#000000; } 
like image 689
Josh Curren Avatar asked Mar 07 '10 18:03

Josh Curren


People also ask

How would you change the Colour of link?

To change the color of hyperlink text, click Hyperlink, and then click More Colors. To change the color of the followed hyperlink text, click Followed Hyperlink, and then click More Colors.


1 Answers

With jQuery you could use the .each function to iterate through the links with the following code:

$(document).ready(function() {     $("[href]").each(function() {         if (this.href == window.location.href) {             $(this).addClass("active");         }     }); }); 

Depending on your page structure and used links, you may have to narrow down the selection of links like:

$("nav [href]").each ... 

If you are using URL parameters, it may be necessary to strip these:

if (this.href.split("?")[0] == window.location.href.split("?")[0]) ... 

This way you don't have to edit each page.

like image 152
Taraman Avatar answered Oct 08 '22 04:10

Taraman