DivitoDesign

Everything for the Web Professional

The MouseClick Effect With CSS

June 27, 2008 9 Comments →

In this article you will learn about the MouseClick effect and you’ll learn how to use this in your own website or blog. It isn’t too hard to understand and nice to know.

What is a MouseClick effect?

The MouseClick effect is everything you will see when you click on a link item. In JavaScript, this is a piece of cake when you use a property like onMouseClick or onMouseDown. Today, we will try to obtain the same effect using CSS.

We have to style the a:active selector to get the desired effect.

An example

We will begin with a normal, unstyled link. That link has a blue color and a underline, a red color when you have selected the link, a purple color if you have already visited the link. And when you roll your mouse over the link, the underline will be removed.

Just a normal link tag:

<a>Link</a>

And now the CSS:

a {
color: #2665aa;
text-decoration: underline;
}
a:hover {
text-decoration: none;
}
a:active {
color: #666666;
}

As you can see, it is really simple to change everything via your CSS. Most likely, you already know this. If not, you should read some other articles regarding css which can be found in the CSS tag. Some more in-depth information follows next.

The MouseClick example

To give this technique a real example we use an un-ordered list with preloaded images. Take a look below.

First some basic html, a normal un-ordered list:

HTML:

<ul>
	<li><a>Item</a></li>
	<li><a>Item</a></li>
	<li><a>Item</a></li>
	<li><a>Item</a></li>
</ul>

CSS:

ul {
list-style: none;
}
ul li {
display: inline;
margin: 0;
padding: 0;
}
ul li a {
float: left;
display: block;
width: 81px;
height: 26px;
color: #333;
text-align: center;
background-image: url(NavigationBackground.png);
background-repeat: none;
background-position: 0 0;
}
ul li a:hover {
background-position: 0 -26px;
color: #cccccc;
}
ul li a:active {
background-position: 0 -52px;
color: #cccccc;
}

You’re might ask yourself this question: Where are the different background images?

We actually use only one image to split normal background, hover background and MouseClick background. It is all about positioning that image to show the right background in the right place. The image can be found below:

Navigation Background
In the CSS provided to you in the MouseClick example, you will see the different a:selectors getting styled. With the background-position our image gets positioned.

If you want to see a real example regarding the MouseClick effect, please visit the following link.

MouseClick effect – Example

Conclusion

This effect is a nice little addition to your website, which is easy to use. If you have any more questions, you could contact me via the comments.

Thanks for reading.

Information about the Author:

Name: Frank Verhoeven

URL: http://www.frank-verhoeven.com/

Description

Interesting Posts

CSS

9 Comments

  1. Great Job………
    It’s very helpful
    thanks

  2. Samantha says:

    I like the hovering effect and I believe I understand how is done. Thanks

  3. yolly says:

    I had hard time thinking about this, that’s why I’m here. Please, what is the code to use to enable a drop down menu after I click my mouse?

    I am presently doing the layouting of my website. Any help is highly appreciated.

  4. Sylt says:

    The site you pinpointed sir Stefan Vervoort also help me in my dilemma. Thank you for the given direction. Might somehow read in full.

  5. Mobbing says:

    thanks for the input and the link from Stefan Vervoort.

    This will help me to build spare eyecatcher for my wiki. In the moinmoin you can add raw-html very simple, but my knowlegde about css isnt deep enough :(

  6. Web Design says:

    I have seen a mouse over script, used with CSS. I can’t recall exactly, but it was dealing with some alpha transparency which was set to -1. However what it does was to fade the image on mouse pointer sort of reduced transparency. I can do that in flash, but I want to know if something similar can be done using CSS or java script for that matter. It has to be smooth. Thanks

  7. It is realy nice. You provides some useful tips about css. thanks

  8. Rakeback says:

    Excellent. I always wondered how they do it in navigation menus, now it is all clear like crystal.

Please Respond and Give your Opinion

We use Gravatar to handle our avatars. Get one there!