Highlighting current page tab in navigation bar
Let your readers know where they are on your blog. How? By highlighting the tab of the page they are currently viewing. See the demo on my widget showcase blog.
We can detect the current page by using conditional if tag in the navigation bar HTML code. Normally we would have to use one set of conditional if tags for each link/tab.
But if your navigation tabs is made of a LinkList gadget, you can reduce the tags to just one set. Here’s how:
(Note: If you use a PageList gadget as a navigation bar, you can skip step 2. The gadget already has a built-in current page detection code).
1. Finding the navigation bar LinkList code
- Find the navigation bar’s widget Id (hint: the Id starts with “LinkList”, followed by a number). If you don’t know how to find the Id, learn how by reading How to find Blogger widget Id. For the sake of this tutorial we will address the Id as YourWidgetId.
- Go to Dashboard > Design > Edit HTML.
- Back up your template.
- Tick the Expand Widget Templates check box on top right of the HTML window.
- Look for the navigation bar LinkList code by searching for the widget id you identified in step 1 (Use Ctrl+F). The code should look something like this:
<b:widget id='YourWidgetId' locked='false' title='' type='LinkList'> <b:includable id='main'> <b:if cond='data:title'><h2><data:title/></h2></b:if> <div class='widget-content'> <ul> <b:loop values='data:links' var='link'> <li><a expr:href='data:link.target'><data:link.name/></a></li> </b:loop> </ul>
2. Modifying the LinkList code
- Replace code line 7 with this code:
<b:if cond='data:blog.url==data:link.target'> <li class='selected'><a expr:href='data:link.target'><data:link.name/></a></li> <b:else/> <li><a expr:href='data:link.target'><data:link.name/></a></li> </b:if>
- The final code will look like this:
<b:widget id='YourWidgetId' locked='false' title='' type='LinkList'> <b:includable id='main'> <b:if cond='data:title'><h2><data:title/></h2></b:if> <div class='widget-content'> <ul> <b:loop values='data:links' var='link'> <b:if cond='data:blog.url==data:link.target'> <li class='selected'><a expr:href='data:link.target'><data:link.name/></a></li> <b:else/> <li><a expr:href='data:link.target'><data:link.name/></a></li> </b:if> </b:loop> </ul>
3. Highlighting the tab
If you are using a Designer Template you probably won’t need this step since the existing code for tab highlighting is applied to all below-the-header tabs regardless if it’s a LinkList or PageList. Go test the tabs first, if they don’t highlight, proceed with the step below.
To style the current page tab, you simply add a styling rule in CSS, like is:
#YourWidgetId .selected {
background-color: #fff;
border-bottom: none;
/* add more styling declarations here */
}
And place it before ]]></b:skin> line in your template code. Replace YourWidgetId with your actual LinkList or PageList widget Id.
- Sample CSS code (full code)
This is the CSS code I use to style the navigation bar in the above picture.
/* LinkList/PageList navigation tabs styling ----------------------------------------------- */ #YourWidgetId { margin: 0; padding: 0 10px; text-align: left; height:30px; width:640px; /* change to match your header's */ position: relative; background: #336699; border-bottom:1px solid #ee4449; _border-bottom:none; _overflow:hidden; } #YourWidgetId ul { margin: 0; padding: 0; list-style-type:none; } #YourWidgetId li { display: inline; float:none; margin: 0; padding: 0; } #YourWidgetId a { line-height:31px; padding: 6px 6px; text-decoration: none; font-size:105%; color: #eee; background-color: #336699; border-right: 1px solid #5588bb; } #YourWidgetId a:hover { color: #ddd; background: #5588bb; } *>#YourWidgetId .selected a { border-top:1px solid #ee4449; border-right:1px solid #ee4449; border-left:1px solid #ee4449; border-bottom:1px solid #fff; padding-top:10px; } #YourWidgetId .selected a { background-color:white; color:#333; font-weight:normal; }
Enjoy!
13 comments to "Highlighting current page tab in navigation bar"
Nice trick! Thanks for the code :)
Hello,
I tried using this code with a PageList gadget and I couldn't make it work:(
Do I need to change anything else?
Thank you!
@.
In step 3, use #YourWidgetId .active instead of #YourWidgetId .selected
Hi there,
Finally i managed to make it work by adding by adding a new class (i think that is what it is) .
I put it in line 8 of the code in step 2 :
then i made a new #linkbar h4
It probably is not the smoothest solution but at least it works. for now:)
thank you for your help! It is good to have some backup:)
@.
Glad to know you've made it work :)
Excellent tip, you have been really helpfull, thanks a lot!!!
Brand new blogger here. Not sure if this post would solve my problem - - trying to make my page link in the right side bar change color to denote the current page, NOT bold the current page (as it does currently). Would there be additional steps above to the process above to make this happen? Here's my (very sparse!) blog: http://thisnorthwoodsnest.blogspot.com/
@jkateb
Go to Design > Template Designer > Advanced > Add CSS and add this code:
.PageList li.selected a {
font-weight: normal;
color: PUT COLOR CODE HERE;
}
@Greenlava
Love it - - exactly what I wanted! Your posts/replies in various forums have helped me so much to get my blog exactly how I want it. Thanks for sharing all your knowledge!
I'm having some trouble with this. I can't get my selected link to highlight for the life of me. Does this trick still work?
@Scott
Yes it still works.
hey there
I have a PageList and I cannot make it work. I also tried .active instead of .selected. Any more ideas?
@Γιωργος Ταγαρης
I don't find any PageList gadget on your blogs.
If your question is unrelated to this article, please use our Facebook page.
When posting HTML/XML code, replace < and > symbols with [ and ] respectively. The reverse applies when copying code from my replies here.