Follow kevinlearynet on Twitter!

kevinleary.net

Highlighting The Current Page With CSS & jQuery

Here’s a common web development scenario: How do we highlight the current page in a navigation seamlessly and dynamically? The answer is jQuery.

See a Live Demo

My recipe for this common scenario involves a mixture of CSS and JavaScript with a dash of jQuery.

XHTML

<ul id="navigation">
	<li><a href="about-us.html">about us</a></li>
	<li><a href="our-products.html">our products</a></li>
	<li><a href="blog.html">blog</a></li>
	<li><a class="active" href="login.html">login</a></li>
	<li><a class="active" href="contact.html">contact</a></li>
</ul>

JavaScript / jQuery

<script type="text/javascript">
$(function(){
	$page = jQuery.url.attr("file");
	if(!$page) {
		$page = 'index.html';
	}
	$('ul.navigation li a').each(function(){
		var $href = $(this).attr('href');
		if ( ($href == $page) || ($href == '') ) {
			$(this).addClass('on');
		} else {
			$(this).removeClass('on');
		}
	});
});
</script>

Usage

You’ll need a copy of jQuery, along with a copy of the jQuery URL Parser plugin.

After you include jQuery, and the URL Parser add the JavaScript snippet above to the <head> section of your document.

How it works

What we do here is simple, we grab the current name of the page, for example about.html. Then we check out ul.navigation <ul> to see if any of the anchors contain an href attribute with that value. If any of the links are a match we flag the parent of the anchor with a CSS class. In this case I’ve chosen current to keep things self explanatory.

Notes

In this case I chose to add the “current” class to the <li> rather than to the anchor. Usually I will create a navigation using CSS sprite techniques, and this allows me greater control over the presentation of that element. This can come in handy for rounded corners or tabs.

Resources

This website uses IntenseDebate comments, but they are not currently loaded because either your browser doesn't support JavaScript, or they didn't load fast enough.

64 Comments

  1. Christopher Seamon / 9.12.08 / 10:40 AM

    Nice post Kevin. I have been leaning towards wanting to learn more about PHP (I am trying to learn more about back-end coding) and what I could do with it and stumbled upon your site.

    I am a big time advocate of using Jquery and really like that you have a lot of information on it within your blog. Thanks for the good resources.

  2. kevin / 9.12.08 / 10:50 AM

    No problem. The first time I began working with PHP was inside Wordpress, and curiously led me to push my limits.

    If your interested in going further with PHP in your work I’d recommend checking out PHP Solutions: Dynamic Web Design Made Easy. I’m a sucker for friends of ED books…

  3. revive / 7.6.09 / 9:06 AM

    So, what about when someone first loads this page.. and the href is "/" and not including one of the linked page names.. none of the menu items will be hightlighted.. do you have a way to select a specific menu item.. say the first one, but using an id on the li, and adding a class to it if the rest of the 'equation' equals false??

  4. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  5. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  6. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  7. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  8. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  9. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  10. kevinlearynet / 7.7.09 / 3:16 AM

    Good point revive,

    I've actually been using a similar revised method to this lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  11. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  12. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  13. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  14. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  15. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  16. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles that situation well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  17. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  18. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  19. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  20. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  21. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  22. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript with jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  23. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  24. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  25. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  26. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  27. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  28. kevinlearynet / 7.7.09 / 3:17 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jquery.url plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  29. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  30. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  31. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  32. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  33. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  34. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  35. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  36. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL parsing. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  37. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  38. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  39. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  40. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  41. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  42. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  43. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  44. kevinlearynet / 7.7.09 / 3:18 AM

    Good point revive,

    I've actually been using a similar but different setup to handle this sort of thing lately. It handles the situation you've described well, and doesn't require PHP at all, just XHTML and a bit of JavaScript / jQuery. I've setup a basic demo for you to check out:

    http://www.kevinleary.net/wp-samples/jquery-curre...

    The jQuery URL Parser plugin replaces the need for the PHP URL functions. I'd love to get your feedback on this, I'll likely be posting a new entry on it shortly.

    Thanks!

  45. revive / 7.7.09 / 12:08 PM

    Hey Kevin,
    thanks for the reply.. and the additional option for dealing with these active menu links.. great work ! I ended up having problems when going to the page originally, since there is no link that matches the root URL, nothing would 'highlight', so to speak..so, I went with a purely CSS solution, and assigned ID's to each pages body tag.. then assigned ID's to each

  46. in my nav file (that gets included into each page dynamically.. styled them with CSS.. and wala.. every page is highlighted correctly, root directory and all.. AND.. one less JS file to load.. so.. that's were I ended up :D

    thanks again for the info on this.. I am sure another project will arise where the CSS solution won't be suitable and this just might be what's needed..

    Cheers

  • revive / 7.7.09 / 12:09 PM

    PS.. the info where I got the idea for the CSS only solution is here (they also show a JS solution that I preferred yours over):

    http://www.richnetapps.com/automatically_highligh...

  • kevinlearynet / 7.7.09 / 1:48 PM

    CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.

    Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.

    <code>
    <?php
    // Body ID
    function current_dir() {
    $path = dirname($_SERVER['PHP_SELF']);
    $position = strrpos($path,'/') + 1;
    return substr($path,$position);
    }
    function current_page() {
    if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
    return basename($_SERVER['PHP_SELF'], '.php');
    } else {
    return 'index';
    }
    }
    $bodyID = current_dir().'-'.current_page();
    ?>
    <body id="<?= $bodyID; ?>">
    </code>

    Thanks for the feedback!

  • kevinlearynet / 7.7.09 / 1:48 PM

    CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.

    Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.

    &lt;?php
    // Body ID
    function current_dir() {
    $path = dirname($_SERVER['PHP_SELF']);
    $position = strrpos($path,'/') + 1;
    return substr($path,$position);
    }
    function current_page() {
    if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
    return basename($_SERVER['PHP_SELF'], '.php');
    } else {
    return 'index';
    }
    }
    $bodyID = current_dir().'-'.current_page();
    ?&gt;
    &lt;body id="&lt;?= $bodyID; ?&gt;"&gt;

    Thanks for the feedback!

  • kevinlearynet / 7.7.09 / 1:49 PM

    CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.

    Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.

    <?php
    // Body ID
    function current_dir() {
    $path = dirname($_SERVER['PHP_SELF']);
    $position = strrpos($path,'/') + 1;
    return substr($path,$position);
    }
    function current_page() {
    if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
    return basename($_SERVER['PHP_SELF'], '.php');
    } else {
    return 'index';
    }
    }
    $bodyID = current_dir().'-'.current_page();
    ?>
    <body id="<?= $bodyID; ?>">

    Thanks for the feedback!

  • kevinlearynet / 7.7.09 / 1:50 PM

    CSS is definitely a great solution for smaller situations, I've found that it works well for 5-6 navigation links. If you every find yourself using multiple navigation areas than the above JS solution may be of use.

    Here's a little something you may find useful that I used on enernoc.com. This code will dynamically create body ID's for you based on the directory and page.

    <?php
    // Body ID
    function current_dir() {
    $path = dirname($_SERVER['PHP_SELF']);
    $position = strrpos($path,'/') + 1;
    return substr($path,$position);
    }
    function current_page() {
    if(basename($_SERVER['PHP_SELF'], '.php') !== 'index') {
    return basename($_SERVER['PHP_SELF'], '.php');
    } else {
    return 'index';
    }
    }
    $bodyID = current_dir().'-'.current_page();
    ?>
    <body id="<?= $bodyID; ?>">

    Thanks for the feedback!

  • revive / 7.7.09 / 2:09 PM

    Hey Kevin..
    That's great! Thanks again! Question for you on this code, and the JS code for that matter.. the main issue I had with the JS code was it not highlighting ANY menu items when the root URL was visited, since the url didn't match any of the links <a href's … how does this body ID tag maker 'see' the base URL ??
    will it return a null value for that homepages body tag??

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like that URL is broken:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like that URL is broken:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like that URL is broken:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like that URL is broken:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like that URL is broken:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like that URL is broken:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like this URL is broken. Try this:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like this URL is broken. Try this:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like this URL is broken. Try this:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like this URL is broken. Try this:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like this URL is broken. Try this:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.16.09 / 4:48 PM

    Looks like this URL is broken. Try this:

    http://www.kevinleary.net/wp-samples/jquery-curre...

  • kevinlearynet / 7.20.09 / 8:35 PM

    I've modified this post, the solution no longer uses PHP, just JavaScript and CSS.

  • Leave a comment

    will not be published

    Wrap code blocks with <pre lang="LANGUAGE" line="1"> and </pre> where LANGUAGE is a GeSHi supported language syntax.