Simple Hover Over Navigation Bars
If something can move, it should move. Something that changes state when the user hovers or goes near it adds a little sparkle to an otherwise lifeless page. It makes the user feel more involved with their browsing experience and adds a little interactivity where there would otherwise be none.
Using Stylesheets and links, we can drastically improve the quality of our navigation. Plain links look acceptable under specific circumstances, but making their background change when we hover over them can really make all the difference.
In this tutorial we're going to show you various examples of making navigational links (such as tabbed navigation) a little more exotic. Throughout the tutorial we'll be
editing a sample script which will be comprised of a box with the collection of links at the top. To keep things simple we're going to put as little elements on our page
as possible.
We'll be using a wrapper box called "wrapper" and a collection of links enclosed within a wrapper called "links-wrapper". Set this up now, or replace the above
mentioned with your own code. Here is our sample code to begin with:
<div id="wrapper">
<div id="links-wrapper">
<a href="/">Hyperlink One</a>
<a href="/">Hyperlink Two</a>
<a href="/">Hyperlink Three</a>
<a href="/">Hyperlink Four</a>
</div>
</div>
Making Links Stand Out
#links-wrapper a {
display: block;
}
The #links-wrapper a { is opening formatting for every link contained within the links-
wrapper ID.
You might've noticed that nothing's changed. Visually, nothing has changed. The links are now displaying as blocks but still using the default link format properties;
this means that, until we change them, they'll look like any other link.
We can add a lot of different formats to our links, but for the purpose of this tutorial we're going to keep it simple. We're going to change their background to black,
change the font colour to white and make the colours reverse when we hover over.
#links-wrapper a {
display: block;
background: #000;
background: #000; is setting the background colour of our links to black.
color: #fff;
color: #fff; is setting the font colour of our links to white. Altogether, our code looks like this now:
<style type="text/css">
#links-wrapper a {
display: block;
background: #000;
color: #fff;
}
</style>
<div id="wrapper">
<div id="links-wrapper">
<a href="/">Hyperlink One</a>
<a href="/">Hyperlink Two</a>
<a href="/">Hyperlink Three</a>
<a href="/">Hyperlink Four</a>
</div>
</div>
Now refresh your page and your links should look like this:
Important!
Be sure to add a new ID below your existing one. We don't want to overwrite any previously defined classes!
Adding it into our stylesheets is pretty straightforward. We simply add it like we did the first ID and reverse the colours around:
Be sure to add a new ID below your existing one. We don't want to overwrite any previously defined classes!
#links-wrapper a {
display: block;
background: #000;
color: #fff;
}
#links-wrapper a:hover {
display: block;
background: #fff;
color: #000;
}
As you can see above, we swapped the two colours around so that they reverse when we hover over. Combining a:hover with
our ID name tells every link that's hovered over within this wrapper to apply the formatting defined.
When you hover over a link, your page should now look like this. I've hovered over "Hyperlink Three":
padding: 4px;
The above code will apply a padding of 4 pixels to every side of our link. We'll combine this with both of our link IDs to make sure we've not got "jumping" links.
Important!
"Jumping Links" occur when links, and hover over on those links, have different width, height, padding or margin values. When you hover over a link, the value increases or decreases, which increases or decreases the size of the link. This causes links to "jump", hence the name. Make sure you set links and their hover effects with the same values, unless going for a specific effect.
Our overall code looks like this now:
"Jumping Links" occur when links, and hover over on those links, have different width, height, padding or margin values. When you hover over a link, the value increases or decreases, which increases or decreases the size of the link. This causes links to "jump", hence the name. Make sure you set links and their hover effects with the same values, unless going for a specific effect.
<style type="text/css">
#links-wrapper a {
display: block;
padding: 4px;
background: #000;
color: #fff;
}
#links-wrapper a:hover {
display: block;
padding: 4px;
background: #fff;
color: #000;
}
</style>
<div id="wrapper">
<div id="links-wrapper">
<a href="/">Hyperlink One</a>
<a href="/">Hyperlink Two</a>
<a href="/">Hyperlink Three</a>
<a href="/">Hyperlink Four</a>
</div>
</div>
Now we want to make our links display inline with each other, so that they don't stretch to the whole of the page and drop a line after each other. To do this, we're
going to introduce the float definition to our page. Float essentially tells an element to "float" to the left or right of our page. Like
image alignment, which you probably learnt with basic HTML, it allows the text to fill the gaps either side of our floating element.
We want to make the links float to the left of our page, so we're going to define it as follows:
#links-wrapper a {
display: block;
float: left;
padding: 4px;
background: #000;
color: #fff;
}
#links-wrapper a:hover {
display: block;
float: left;
padding: 4px;
background: #fff;
color: #000;
}
With the introduction of float: left; our links will now float over the left of the page. The best thing is, they'll not auto-stretch to
the size of our monitor now either. Our navigation is starting to take shape:
font-family: Verdana;
This isn't the best method of defining fonts, but this is a basics tutorial and we need to know the technique before we can do it "the proper way". With this added to
our code, our links will now display using the Verdana font rather than the user's browser default.
Our final code looks like this:
<style type="text/css">
#links-wrapper a {
display: block;
font-family: Verdana;
float: left;
padding: 4px;
background: #000;
color: #fff;
}
#links-wrapper a:hover {
display: block;
font-family: Verdana;
float: left;
padding: 4px;
background: #fff;
color: #000;
}
</style>
<div id="wrapper">
<div id="links-wrapper">
<a href="/">Hyperlink One</a>
<a href="/">Hyperlink Two</a>
<a href="/">Hyperlink Three</a>
<a href="/">Hyperlink Four</a>
</div>
</div>
We've now got the basics for a horizontal navigation going with a simple hover over effect. Our next tutorial will look at furthering what we've learnt here and using
background images in a creative fashion to make our navigation links more intricate.
Our Readers have Posted 1 Comments.
Click here to add yours!
Added 3:19pm, Sunday 17th May, 2009.
1 Comments.