ASP & CSS: highlight selected menu item
When creating menus for a web site, it is a good practise to use an inclusion, so that we write the menu only once. When the menu is included on every page, we might need to highlight the selected option in the menu itself.
For example, we can have a menu like:
For example, we can have a menu like:
- Home Page
- Option 1
- Option 2
- Option 3
- Home Page
- Option 1
- Option 2
- Option 3
The menu
The menu is very simple in our example, but you will be able to apply the following concepts to any menu. Just for the sake of completeness we will consider the following structure:
The above HTML code should be in your document which will be included on every page.The ASP part
Now we need to know which is the name of the viewed page. To do so, we are going to use a simple ASP function. Add the following snippet in your menu page (just before or after the - list):
- that correspond to the page viewed.
Easy like that!The CSSNow you only need to create appropriate CSS rules. In our example I will only create a simple rule for the menuSelected class:
It is clear that you can play around with your styles the way you prefer..menuSelected {
font-weight: bold;
}
Hope you've found the post useful. As usual let me know what you think about it...
<%
Dim fileName
fileName =(getFileName)
%>
As you can see, the "fileName" variable will be the name of the viewed page. We are going to use that variable in order to apply a specific CSS rule to the menu options.Let's get back to the menu...
We need to change the
- list and make it look like:
That way, a div with class="menuSelected" will be added to the