Laith Zraikat

I Innovate, Therefore I Am.

CSS Series #1: Simple navigation menu with rollover effects

I have decided to start writing a series of articles on CSS because of the many questions I get from friends and fellow developers seeking answers to specific questions like this one.

Creating rich menus has always been a major issue for most web developers. I remember when I first started doing web design back in 97, my jaw used to drop each time I saw a sliding menu or one with rollover effects. Back then if you wanted a really rich menu, you had to use Java palettes, which was rocket science to most designers.

Fortunately today, a wide array of rich interface elements are possible using only CSS and a little JavaScript which can be learned in minutes.

A common question people ask is: How do I create a tabbed menu with rollover effects?

The approach I like most is one that employs CSS, very little JavaScript, and of course HTML. I will start by explaining the HTML components of our menu and then move on to the CSS part.

The magical HTML List:

Almost every designer uses the ordered list <ol>, or the un-ordered list <ul> at least a few times in every project. Most people think they should use it only when they want to create numbered lists or bullet lists. Well, that was true back in the days when the Yahoo homepage had a gray background and Amazon.com had the jungle theme.

Today, it is very important to know what kind of markup you need to use and why. You will come across a variety of ways to achieving he same result and it will be up to you to decide which is best for your case. Having said that, I think there is one rule of thumb that should apply to all cases: Always use semantic markup.

Semantic means "descriptive", hence "descriptive markup". To demonstrate, take the following site menu. The first example does not use any semantic markup:

HTML:

Site Menu
<blockquote>
<a href="/">Home</a><br/>
<a href="/">Overview</a><br/>
<a href="/">Services</a><br/>
<a href="/">Contact</a>
</blockquote>

Notice how the list effect is achieved by using <blockquote> and <br>, which were not made specifically for formatting a list of items. Anybody can use these two tags to display a whole lot of things, none of which are item lists.

The second example shows semantic markup that represents the site menu:

HTML:

Site Menu
<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/">Overview</a></li>
<li><a href="/">Services</a></li>
<li><a href="/">Contact</a></li>
</ul>

If we use the first method, only a human will be able to figure out that this code represents a hierarchal list of items. Another down side is that the list may be rendered differently on different platforms, so your list or menu in this case may not be comprehensible on a mobile phone for example.

Another advantage of using the html list is that you can represent a hierarchy of items in the form of nested lists very easily by starting a new list inside any of the main list items. If you wanted to have sub-lists using the first method, you will make a big mess out of your html, and it will just look stupid.

Arrange it horizontally:

CSS:

/*Setting the display for the list items to inline will arrange them horizontally*/
#menu li{
display: inline;
}

Note: You notice that I use #menu as a prefix for all my styles. This is because my main menu ID is "menu". This causes my styles to only affect the html elements inside "expenses". You don't have to use a prefix unless you have more than one menu and you want to have different styles and behavior for each. However, I always like using it as a standard. You can, however, substitute "#menu" with "ul" in your css and it will work just fine.

Note: I used "/**/" in the first css example above. Anything you write between "/*" and "*/" is considered a comment and will not affect your css. It's just to remind you and let other designers know what this style is for.

Give it some style:

We need to apply some styles to our menu to make it look nicer.

CSS:

/*Sets the style for the main menu*/
#menu {
margin: 0px
margin-top: 15px;
padding: 0px;
width: 500px;
}

#menu li {
display: inline;
margin: 0px;
}

#menu li a {
padding: 6px;
border-bottom: 0px;
background-color: #9c0;
text-decoration: none;
color: #fff;
border: 1px solid #666;
}

Note: Those of you accustomed to setting colors in your web page by manually inserting its hex code may find it strange that I used only 3 digits instead of the usual 6 for my colors. Abbreviation is possible when the two digits for each of the basic colors (red,green,blue) are the same, by representing every color with one digit. For example: #99cc00 can be referred to as #9c0, #ff9900 --> #f90, #ffffff --> #fff…..etc. This form is supported in both IE and FF.

Roll it over:

Imagine you could talk to the browser, and tell it to create a rollover effect. You would say: "when the user puts the mouse pointer over the link, please change its background color to white and text color to grey". Here's how we say it in CSS:

#menu li a:hover {
background-color: #fff;
color: #666;
}

Note: "hover" is called a Selector. A selector is the condition which is matched, will trigger the changes specified in the style class to the element which it is related to- in this case the <a> tag in our menu.

Tab it:

You may want to create a tab effect by presetting one of the links to be selected when displayed on a certain page…"Services" for example. You can do that by defining a new css class, for example "selected", and then assigning it to the link you wish to display as selected:

CSS:

#menu li a:hover, #menu selected {
background-color: #fff;
color: #666;
}

HTML:

<ul id="menu">
<li><a href="/">Home</a></li>
<li><a href="/">Overview</a></li>
<li><a href="/" class="services">Services</a></li>
<li><a href="/">Contact</a></li>
</ul>

And you have a menu:

 

Can you smell the morning breeze?


Sunrise
Originally uploaded by laithz.
What is more amazing than an amazing sunset is a sunrise. I guess there's something more poetic in watching the birth of a new day than watching it die.

Even the buildings with the water tanks and satellite dishes look good.

Gone in 60 Milliseconds

As you call can see, I have accidentally deleted my blog,... you don't want to know the feeling...

You may be wondering how this happened,... well, at Jeeran we do a lot of testing all the time to make sure the system is running smoothly especially after a major update. So, tonight I happened to be fiddling around using a testing blog to try out the new features we have just released -one of which is "TAGGING"- and after I was done I wanted to delete the blog, so I used the quick and easy "Delete Blog" feature that we have -I really don't think anyone other than testers uses it - and in the split second that my finger clicked the mouse button, a feeling of terror ... I actually shivered. Then I heard a loud shout in my head "NOOooo...." and I heard it fade as if falling in a pit.....

At times like these you wish and pray and hope that your ADSL suddenly goes down -as it does when you don't want it to- but no, this time its lightning fast, not a glitch. I clicked the stop button knowing it was too late... In my head I could picture the bits of data forming the fateful http request racing through the wire, crossing thousands of miles of land and ocean, passing people, trees, fish and possible birds, and finally arriving at their destination, happy to collide with router to be carried to their final destination, where they deliver their message and be free at last... I could picture the software analyzing the message, figuring out what it has to do, and laying down the action plan which will send my blog to its doom.... Throughout this I could still hear the scream in my head slowly fade away... and then there was silence.. I knew it was over...

In just Milliseconds, my blog was gone... it was so fast I'm more amazed than sad or angry. I am amazed at how fast these technologies are, how we sometimes take it for granted when it takes us a minute to download a 3Mb song -some still think that's slow- but mostly, I am amazed at how fast and complex the human brain is that it can come up with all these thoughts, imagery, and feelings in such a negligible time-span.

In the end, I will not despair, nor should you feel sorry for me because Jeeran is a smart company and smart companies use backup. So, my blog will be restored soon, and I hope you all learned a valuable lesson from my experience, if you haven't it's okay, you will some day out of your own basket :)



<<Home