Computers and Technology
CSS Vertical Navigation
Finally! Let’s check out vertical navigation. This tutorial will cover different tips and tricks on creating the vertical navigation bar. You can read my article on Color Theory and about Color Scheme Designer for more info on picking colors for your navbar. Most navbars, as I’ve heard, depend on javascript for the more hardcore effects but I’ll show you how to create some dazzling, amazing navbars WITHOUT the knowledge of javascript. Which is great, because I don’t know javascript at all.
The List
First we need to set up the navigation using lists and divs with unique id’s we’ll use in the CSS part. This is what it should look like:
<div id=”navdiv”>
<ul id=”navlist”>
<li><a href=”link1.html”>link one</a></li>
<li><a href=”link2.html”>link two</a></li>
<li><a href=”link3.html”>link three</a></li>
</ul>
</div>
Here we set up a container “div” to which you can apply numerous properties. But anyways, we have a nice list set up. Here’s what the same navbar would look like if you wanted a sub-menu to appear:
<div id=”navcontainer”>
<ul id=”navlist”>
<li><a href=”link1.html”>link one</a><ul id=”subnavlist”><li><a href=”link1a.html”>link one a</a></li>
<li><a href=”link1b.html”>link one b</a></li>
</ul>
</li>
…..
</ul>
</div>
The whole process can be done without id’s but if you want to implement this in your website, it may cause some confusion.
The CSS
Let’s set up the CSS then. First, we’ll want to define the width of the container:
#navdiv {
160px;
}
Result so far:
Let’s talk about the navlist and set up the appropriate margins then:
#navlist {
margin-left: 0;
padding-left: 0;
list-style-type: none;
font-family: Arial, Helvetica, sans-serif;
border: 1px solid black;}
Result so far:
I’ve just basically reset some of the margin and padding values used as a default (it’s always good to include a general reset sheet at the beginning of the CSS file). I’ve also removed the bullet points from the side so that it’s an unordered list without any markings. I’ve set up the font-family (again, a reset) and put a border around our navigation.
Next, we’ll set up a couple values for the “a” element, that is all the hyperlinks involved will follow the style:
#navlist a
{
display: block;
padding: 3px;
background-color: #FFFFFF;
}
Result so far:
We’ve made it so that not only the WORDS of the hyperlinks will serve as a link but the whole block inside the division will be the hyperlink. It’s a sort of a nice effect. Check the final version to see what I mean.The padding of 3px is essential so that the navbar won’t be clumped together.
Next, we’ll tackle the a:link and a:visited:
#navlist a:link, #navlist a:visited
{
color: #0000CC;
text-decoration: none;
}
Result so far:
We used these two pseudo classes and look where it’s gotten us. The links are no longer underlined (a success!) and they will also not change color once clicked which, I think, is highly desirable in a navbar. One more effect to go and then we’ll move onto the sub-menu:
#navlist a:hover
{
background-color: #369;
color: #fff;}
Result so far:
Here’s a nifty effect. When you hover over the link, not only will it show up as a block (defined above) but it will also change color of the background as well as the text. If you delete the sub-menus from the list, you’ll get a nice clean navigation. If you keep the sub-menu items, you’ll get the result above.
Sub-Menu
Sub-menus, yay! Currently, only one sub-menu is supported which sucks but then again, who uses more than one sub-menu? Not me, that’s for sure. Let’s focus though. Here’s initial code concerning sub-menus:
#navlist li>ul{
display: none;
position: absolute;
width: 100px;
margin-top: -1.6em;
margin-left: 157px;}
Result so far:
First of all, this is how your navigation bar will look like without sub-menus. Second, this is what it will look like when the sub-menus are not shown.
Ah, so here we go. By using li>ul, we’re only defining lists listed under other lists, now that’s a mouthful. The “display: none” makes the sub-menu invisible for the moment. The position is absolute so when placing this on your website, make sure to change the values accordingly. The only part that will need changing is probably “margin-left”. If you set a different width for the original list, you’ll have to adjust for that. Let’s look at why we need the “margin-top” in the negatives. This is what a simple list looks like:
- one
- one A
- one B
- two
See what happened? The nested list has moved down and right. What we want though, is to have the list right NEXT to its parent list item like this:
- one
- one A
- one B
- two
The usual value is about -1.4em for margin-top to get the lists lined up but you’ll have to experiment if you added any padding, margins, and such. Like in the list we’re making, I had to adjust by putting -1.6 em. For margin-left, The value here is 40px.
Let’s make the sub-menu appear:
#navlist li:hover>ul {
display: block;
}
And that’s it. We’re finished, this last piece will make the sub-menu appear when the original list is hovered over. There are many ways to re-make this navigation bar to make it prettier but these are the basics. Try experimenting with borders, padding, backgrounds, and such and see what you get.
Final result:
Here’s the final code:
<head>
<meta http-equiv=”Content-Type” content=”text/html; charset=utf-8″ />
<title>Untitled Document</title>
<style type=’text/css’>
/* NAVIGATION */
#navcontainer {
width: 160px;
padding: 100px;
}#navlist
{
margin-left: 0;
padding-left: 0;
list-style-type: none;
font-family: Arial, Helvetica, sans-serif;
border: 1px solid black;}
#navlist a
{
display: block;
padding: 3px;
background-color: #FFFFFF;
}#navlist a:link, #navlist a:visited
{
color: #0000CC;
text-decoration: none;
}#navlist a:hover
{
background-color: #369;
color: #fff;}
#navlist li>ul{
display: none;
position: absolute;
width: 100px;margin-top: -1.6em;
margin-left: 158px;}
#navlist li:hover>ul {
display: block;
}</style>
</head><body>
<div id=”navcontainer”><ul id=”navlist”>
<li><a href=”link1.html”>link one</a>
<ul id=”navlist”><li><a href=”link1a.html”>link one A </a></li>
<li><a href=”linkoneb.html”>link one B </a></li>
</ul>
<li><a href=”link2.html”>link two</a></li>
<li><a href=”link3.html”>link three</a></li>
</ul>
</div>
</body>
| Print article | This entry was posted by Admin on October 8, 2009 at 17:30, and is filed under Design, HTML/CSS, Tutorials. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |
No comments yet.
Setting up Feedburner Feeds
about 5 months ago - 1 comment
Have you ever wondered about that pretty subscriber counter most blogs have on their pages? How do they know exactly how many people are following them? It’s called FeedBurner. I’ve been using it for a while now and it never occurred to me how much POWER such a service can have! Not only are you
Just a little stupid Javascript App
about 5 months ago - No comments
Mobile Websites (tips)
about 7 months ago - 7 comments
I’ve recently started working on some mobile websites. You’ll notice that davepcguy.com has its own mobile alternative as do some other sites. Mobile devices are growing in number and gone are those days when internet was viewed only on the computer screen. WAP is not even used either so that old school coding is gone
Only A Quote
about 8 months ago - 2 comments
We here at DavePCGuy have finally launched our first independent website, done completely without an outside sponsor! It’s called Only A Quote. Basically, what it is, is a database of quotes from different kinds of places (added one at a time to ensure quality) where the users only see a single quote with a random
CSS3 Lesson 3: Background Information is Essential
about 8 months ago - 4 comments
Let’s have a look at backgrounds. In the previous two tutorials, I’ve covered borders and opacity. In this tutorial, we’ll cover some of the basic CSS3 additions to the background properties. If you have no idea how to deal with backgrounds in CSS, read my tutorial on backgrounds pre-CSS3. CSS3 is gaining momentum in the
CSS3 Lesson 2: All About Borders
about 9 months ago - 3 comments
Okay, I’ve noticed some REALLY cool stuff on CSS3 online, so I thought to myself, how about I make my own experiment and teach you all about how to use this stuff? Here are a couple of resources I’m using to learn about CSS3: CSS3 Info CSS3 Validator Intro to CSS3 W3 Avenue Net Tuts
Success!
about 9 months ago - 2 comments
Okay, I have to thank all of our readers and people that have supported us along the way! It has been over three months now since we launched DavePCGuy.com Since our launch, we have published 47 distinct, separate, and awesome blog posts! Most of them have been PHP or CSS tutorials and it was quite
CSS Lesson 7: Border Radius and Opacity
about 9 months ago - 5 comments
Border Radius and Opacity I’ve noticed some hundred tweets on CSS3, every day. Check out the CSS3 Watcher for random updates. They actually mention really awesome and useful tutorials. So now, let’s look at some basic features that actually work cross-browser (with some tweaks). You can check some of the usage on two version of






