Picnic Website Code Tutorials

Highlight Current Page Plus Tutorial

Below I've compiled the three easiest ways to highlight the current page that your on. All three are coded clean and very simple to implement into your own project. The method that you choose to use simply comes down to the project at hand and your prefered coding style. Each are pretty self explanatory, so I'll just let the code explain itself. That's usually the way I learn best anyway!

Option #1

View Demo

This one is the easiest and most basic of the three options listed. If you have a small site (1 to 5 pages) and your running .html extensions, and/or you don't want or need to use PHP Includes, then this method is the one for you!

#nav {
width:150px;
list-style:none;
}
#nav a {
color:black;
text-decoration:none;
}
#nav a:active, #nav a:focus, #nav a:hover {
color:red; 
}
#nav #current {
color:red; 
}
#nav #current:active, #nav #current:focus, #nav #current:hover {
color:blue;	
}
		
<body>
  <ul id="nav">
    <li><a href="#" id="current">HOME</a></li>
    <li><a href="#">ABOUT</a></li>
    <li><a href="#">CONTACT</a></li>
  </ul>
</body>
		

Option #2

View Demo

If you use, or want to use PHP Includes on your website, then the simple code shone above will not suffice. Therefore, you'll need to get a little more creative and use this method (Option #2) or the next method (Option #3) instead. This method shone here, is usually my code of choice for the job at hand!

#nav {
width:150px;
list-style:none;
}
#nav a {
color:black;
text-decoration:none;
}
#nav a:active, #nav a:focus, #nav a:hover {
color:red; 
}
#home #homenav, #about #aboutnav, #contact #contactnav {
color:red; 
}
#home #homenav:hover, #about #aboutnav:hover, #contact #contactnav:hover {
color:blue; /* add :active and :focus styles here also */
}
		
<body id="home"> <!-- the body needs a unique id -->
  <ul id="nav"> <!-- each anchor needs a unique id -->
    <li><a href="#" id="homenav">HOME</a></li>
    <li><a href="#" id="aboutnav">ABOUT</a></li>
    <li><a href="#" id="contactnav">CONTACT</a></li>
  </ul>
</body>
		

Option #3

View Demo

And the final way of going about highlighting the current page's navigation link is to use a bit of PHP. A little explanation: This bit here in bold <?php $pageName = "Home"; ?> (which tells the PHP code which page your currently on) can be placed just about anywhere on the page, that is, as long as it's sitting above the #nav using it. In this case, and partly for simplicity purposes, I just placed it inside the body tag as you can see. However, and unless proven otherwise, I think I like it there! And I'd like to thank Ben Falk and John Beatrice for helping me to get this last one working.

#nav {
width:150px;
list-style:none;
}
#nav a {
color:black;
text-decoration:none;
}
#nav a:active, #nav a:focus, #nav a:hover {
color:red; 
}
#nav #current {
color:red; 
}
#nav #current:active, #nav #current:focus, #nav #current:hover {
color:blue;	
}
		
<body<?php $pageName = "Home"; ?>> 
  <ul id="nav"> 
    <li><a href="#" <?php if ($pageName=="Home") echo 'id="current"'; ?>>HOME</a></li>
    <li><a href="#" <?php if ($pageName=="About") echo 'id="current"'; ?>>ABOUT</a></li>
    <li><a href="#" <?php if ($pageName=="Contact") echo 'id="current"'; ?>>CONTACT</a></li>
  </ul>
</body>
		

Need help adding this or other code to your website? Post it here in the forum! Forum

Sponsors

Top Donators

Friends of Mine