Create your menu like this in HTML:
<body id='Search' > <div id='menu'> <div class='bars'></div> <a href='/home/' title='Home'>Home</a> <a href='/community/' title='Community'>Community</a> <a href='/search/' title='Search'>Search</a> <a href='/contact/' title='Contact'>Contact</a> <div class='bars shift'></div> </div> </body>
Note that the body tag has a page id (as Cody suggests). Also note that this identifier must be the same as the value of the title attribute for the menu link.
Then use this CSS:
.bars {border-top:1px solid #CCC;border-bottom:1px solid #CCC;height:5px;} .shift {margin-top:-6px;} #menu {font-family:arial;font-size:12pt;text-align:center;} #menu a {display:inline-block;padding:7px;text-decoration:none;color:#000;} .active {border-bottom:5px solid red;}
Finally, add this jQuery to each page:
$(document).ready(function(){ var id = $('body').attr('id'); $('#menu a').each(function () { var title = $(this).attr('title'); $(this).removeClass('active'); if (title == id) $(this).addClass('active'); }); });
You can see a demonstration of live scripts here . (To check, just change the identifier of the body element and press run.)
EDIT: The advantage of this method compared to the Cody Grays method is that you do not need to change CSS every time you change HTML, when you add, remove or edit a menu item. On the other hand, it uses jQuery. However, if you want a clean css solution, just remove jQuery and the body element identifier, and instead directly apply the .active class to a specific link, depending on the specific page (similar to jackJoe's suggestion).
This can be easily changed if you must use <li>
elements.
source share