Web Designers' Edge – The Self-Taught Designer's Best Friend

The Hover Effect Without Javascript or Jquery

We all love javascript and jquery, right? It’s the stuff that helps you do cool stuff on your site, like rotators, animated elements, and a ton more. But the problem is, if you use Javascript and Jquery to do every effect on your site, it can be a little code-intensive. It also won’t work when someone has javascript disabled.

Enter the simple elements that use nothing but HTML and CSS, and maybe some images.

What I want to demonstrate is a simple way to get a hover effect on an image, without using jquery or javascript. The way this works, basically, is the image will be the background of a div element that will reside inside a link. Actually the div does not have to be inside a link, but it does if you want the image to link to some other url. The background image actually contains two copies of your image, with the second copy having whatever hover effect you want, such as slightly darker or whatever. In the CSS code, you have the background image positioned at the top of the div, and in a hover of the same div, you have the background image positioned at the bottom. When you hover, voila! And unlike the javascript version of hover, you don’t have to wait a split second for the hover to work, since the image is already loaded!

To the rightyou can see a screenshot of the effect I’m going to show you. On the same screenshot you can also see a green button. I used the same effect on that. The button is part of a jquery slider, so it’s not like I never use jquery :)

I’m going to be using the coffee cup that’s available here. Actually I recommend this set of twitter icons, they are probably the coolest you can find.

First, I open Fireworks and find the coffee cup image I downloaded. Then I resize it to the size I need for the footer of my website. For me that’s 96px wide and 71px high. Then, I copy the image and paste it over itself. Next, move the second copy so that the top of the second copy just touches the bottom of the first copy. Then “Fit Canvas” so that the two images are now visible. Next, I select the bottom image and modify the color by reducing the saturation to nothing. This makes it greyscale. See the screenshot to the right.

Then save the image and optimize it. I save it as a transparent .PNG file, but you can also save it as a .jpg if you don’t need transparency. I personally use PNG Gauntlet to optimize the .PNG.

Then I load it into the image folder on my website, and put the following code in the CSS file:


.twitter {
float: right;
width: 96px;
height: 71px;
background: url('images/social/twitter-cup.png') no-repeat bottom;
margin: 17px 44px 4px 0;
}
.twitter:hover {
background: url('images/social/twitter-cup.png') no-repeat top;
}

My div has a class ‘twitter’. Then simply put the div in your website where you want it to appear. Just call it the same class and it will work like a charm. You can also wrap it with a link if you want it to be hyperlinked.

And that’s it! The simplest (I believe) way to have cool hover effects on your website, that works instantly since the image is already preloaded when the page loads.

See the footer of my web design site for a sample: See The Coffee Cup Here

If you implement this little trick, please let me know by commenting here or tweeting it!

About the Author

Tim Norton started designing websites in his free time in 2005 and it soon became a major source of income for his family. He taught himself coding, graphics, and whatever else relates to designing great websites. Get in touch at 6Webdesign.

Like This? Please share it now!

  • Mike

    What happens if your browser doesn't have CSS? I'm being facetious.
    Ultimately, if they don't have JavaScript enabled, then they are clearly not that interested in “effects” and the added benefits that come alongside the basic content. So, I don't bother trying to still make things fancy for them. They are limited,/limiting themselves for a reason, and I think at that point it comes down to basic usability of the site itself.
    It's nice that you put this method out regardless, an keeping code down to a minimum is still and always should be an important fact of web-development.

    • timnorton

      True, I've felt that way too. but I think often if they have it disabled they just might be non-tech saavy and did it by accident. At any rate, we agree that minimal code is better …

  • http://reciprocity.be/ Keith S.

    You could make the hover simpler (and save another load of the image) by just using the background-position property thusly:

    .twitter:hover {
    background-position: top;
    }

  • Clyde Jones

    test

    • timnorton

      Good edits Clyde and Keith, another code savings. I appreciate it!

  • Pingback: Web Designers' Edge – The Self-Taught Designer's Best Friend

  • http://www.air-jordan-11.com/ air jordan 11

    Here elaborates the matter not only extensively but also detailly .I support the
    write's unique point.It is useful and benefit to your daily life.You can go those
    sits to know more relate things.They are strongly recommended by friends.Personally!http://kite8.net/

  • http://www.chaneloutletstores.com chanel

    It looks good,I have learn a recruit!
    Recently,I found an excellent online store, the http://www.chaneloutletstores.com are completely various, good quality and cheap price,it’s worth buying!
    write's unique point.It is useful and benefit to your daily life.You can go those chanel chanel outlet chanel store cheap chanel discount chanel chanel on sale chanel for sale chanel leather Chanel bags chanel handbags chanel purses chanel wallets chanel Luggage chanel travel bags chanel shoes chanel boots chanel Accessories chanel belts chanel jewelry chanel scarves Chanel glasses chanel sunglasses chanel clothing chanel dress chanel bikini thefer net Like-Acer tiny let-lands iamateacherithink
    sits to know more relate things.They are strongly recommended by friends.Personally
    I feel quite well.

  • http://viettelonline.com/ USB 3G

    Oh nice, thanks for share!