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>
Related posts:
No comments yet.
When your project fails…
January 31, 2010 - 10:04
Tags: blog, php, web design, webpage
Posted in General Articles | 8 comments
Mobile Websites (tips)
January 11, 2010 - 18:28
Tags: css, Design, HTML/CSS, mobile, php, webpage, website
Posted in HTML/CSS, PHP and Scripting, Tutorials | 4 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
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 button [...]
CSS3 Lesson 3: Background Information is Essential
December 16, 2009 - 21:34
Tags: css, Design, formatting, lesson, webpage, website
Posted in HTML/CSS, Tutorials | 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
December 3, 2009 - 10:38
Tags: border, css, formatting, internet, lesson
Posted in HTML/CSS, Tutorials | 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 Plus tutorial
I’ll start with the topic [...]
Success!
December 1, 2009 - 11:20
Tags: webpage, website
Posted in Personal | 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 a [...]
CSS Lesson 7: Border Radius and Opacity
November 19, 2009 - 16:46
Tags: background, css, lesson, text, website
Posted in HTML/CSS, Tutorials | 4 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 [...]
CSS Trick: Disappearing Textbox
October 30, 2009 - 11:21
Tags: css, formatting, project, text, web extras, webpage, website
Posted in HTML/CSS, Tutorials | 4 comments
Remember that trick to show a sub-menu using CSS in my older tutorial? Well, I had an idea for a project of mine, and with a little help from my dear friend Raphael. Last night, I launched my little website project called “A Night Story” and made some tweaks. Ignore the story, it’s just a [...]
CSS Lesson 6: Intro to Divs
October 25, 2009 - 17:36
Tags: css, Design, divs, formatting, lesson, text, webpage, website
Posted in HTML/CSS, Tutorials | 5 comments
Alright, alright. So I skipped all that fuss with horizontal navbars but that stuff is easy so I’ll create a short article on it later.
Let’s start with the <div> tag. It’s what I’ll be covering. Making use of div’s with CSS. You’ll need to know the following before we start the lesson:
CSS Lesson 2
CSS Lesson [...]
CSS Lesson 4 Lists
September 30, 2009 - 14:24
Tags: css, formatting, lesson, lists, navbar, webpage
Posted in HTML/CSS, Tutorials | 2 comments
Lists
HTML
CSS
Lists are an important part of CSS, they are used to primarily make navbars. I’ll cover some basics with you and how to set up your list. In my NEXT tutorial, I’ll show you how to make VERTICAL navbars. I know, I know, you’re asking, “Why are you splitting it up?” Well, mostly because [...]







