Download Vertical_Nav_Menu.zip
A basic navigational menu can be created from a list (ordered or unordered) by making each list item a link and then "stylize" the whole list to make it behave like a menu.
Notice the systematic steps in the accordion below:
First, we will start by creating a simple list of paragraphs.
<body>
<p>Home</p>
<p>Products</p>
<p>Services</p>
<p>Contact Us</p>
<body>
<ul>NOTE: Notice how the "p" elements were converted to "li" elements and wrapped within a "ul" element.
<li>Home</li>
<li>Products</li>
<li>Services</li> <li>Contact Us</li>
</ul>
Next, let's convert the list to a list of links by applying an anchor tag <a> to the text in each of the list item (highlighted in bold).
NOTE: We could have named the first line home.html; however, it is best practice to name the "home page" index.html.
<ul>
<li><a href="index.html">Home</a></li>
<li><a href="product.html">Products</a></li>
<li><a href="services.html">Services</a></li>
<li><a href="contact_us.html">Contact Us</a></li>
</ul>
Now, let's address the links themselves by creating a contextual selector (e.g., #nav a) to target the anchor element (<a>):
/* ---------- STLYLE LINKS ---------- */ #nav a {
text-decoration: none;
color: white;
padding: 0 15px;
line-height: 2.1;
border-bottom: 1px solid white;} </style>
/* ---------- STLYLE LINKS ---------- */ #nav a {NOTES:
text-decoration: none;
color: white;
padding: 0 15px;
line-height: 2.1;
border-bottom: 1px solid white;
display:block; text-align:center;
}
NOTE: Notice the padding values of 0 and 15px. When a value is zero, there is no need to specify a unit because it does not really matter if the unit is in pixels, inches, etc. since the value is zero. Also, when only two values instead of four are written for padding, the first value refers to top or bottom and the second value refers to left or right of the box element.
Now, let's enhance our menu by creating a hover effect when the user moves the mouse of a menu item. To create a hover effect, we will apply a hover pseudo-class to the <a> tag inside the nav area
/* ---------- STLYLE LINKS HOVER ---------- */ #nav a:hover { background-color: #006;} </style>
/* ---------- STLYLE LINKS HOVER ---------- */
#nav a:hover { background: #006;
border-left: 5px solid red; }
</style>
/* ---------- STLYLE LINKS ---------- */
#nav a {
text-decoration: none;
color: #FFF;
padding: 0 15px;
line-height: 2.1;
border-bottom: 1px solid red;
display:block;
/*text-align:center;*/
}
#nav:first-child {
border-top: 2px solid red;
}
/* ---------- STLYLE LINKS ---------- */
#nav a {
text-decoration: none;
color: #FFF;
padding: 0 15px;
line-height: 2.1;
border-bottom: 2px solid white;
display:block;
/*text-align:center;*/
}
#nav:first-child {
border-top: 2px solid white;
}
An "active" class can be add to a link to let the user know which page is currently active.
/* ---------- ADD ACTIVE LINK ---------- */ .active {background-color: #006; color: white;} </style>
<li class="active"><a href="index.html">Home</a></li>
/*----- STYLE LIST ------- */NOTES: The following rules do the following:
#nav {
list-style-type: none;
background-color: #09F;
width:180px;
padding:0; margin:0;
/*border: 1px solid black;*/
position: fixed;
height: 100%;
overflow: auto;
}
TIMESAVING TIP: Once you have some or all of the CSS rules you need to create a functional menu, you can create a snippet of the CSS code and then reuse the code again in a new web site or new web site file by simply inserting the snippet in the style element or an external style sheet or create a new style sheet altogether: The advantage of this is that once you have code that work you don't have to type it again or figure out how it was done again.
/*----- STYLE LIST ------- */
#nav {
list-style-type: none;
background-color: #09F;
width:180px;
padding:0; margin:0;
/*border: 1px solid black;*/
position: fixed;
height: 100%;
overflow: auto;
}
/* ---------- STYLE LINKS ---------- */
#nav a {
text-decoration: none;
color: #FFF;
padding: 0 15px;
line-height: 2.1;
border-bottom: 1px solid white;
display:block;
/*text-align:center;*/
}
#nav:first-child {
border-top: 1px solid white;
}
/* ---------- STLYLE LINKS HOVER ---------- */
#nav a:hover { background: #006;
/*border-left: 5px solid red;*/ }
/* ---------- ADD ACTIVE LINK ---------- */
.active {background-color: #006; color: white;}
To let the user know what page he or she is currently on, we will write some code the "detect" what page the browser is viewing and highlight the corresponding menu accordingly.
<ul id="nav">
<li id="nav_home"><a href="index.html">Home</a></li>
<li id="nav_products"><a href="products.html">Products</a></li>
<li id="nav_services"><a href="services.html">Services</a></li>
<li id="nav_contact_us"><a href="contact_us.html">Contact Us</a></li>
</ul>
/* ---- For DW Template---- */NOTES:
#body_home #nav_home a,
#body_products #nav_products a,
#body_services #nav_services a,
#body_contact_us #nav_contact_us a
{background-color:#00F;
font-weight:bold;
cursor:auto;}
</style>
<!-- TemplateBeginEditable name="head" -->
<!-- TemplateEndEditable -->
<!-- TemplateParam name="id" type="text" value="body_home" -->
</head>
<body id="@@(id)@@">
<body id="body_products">