Visit DynamiX Web Design! E-Mail DynamiX Web Design!

css: creating an image “hover” effect using only css



Most of us have at one time or another have had to (or tried unsuccessfully to) alternate an image on mouse over. Most commonly, this is effect is used to provide a response when a visitor runs their mouse over a button, like the one below:

Now, this was very easy to create, using just the following CSS:
.buttonexample{background-image: url(images/button.gif); width:91px; height:28px;}
.buttonexample:hover{background-image: url(images/button-over.gif);}
and the following html:
<div class="buttonexample"></div>

If you are just hoping to make a standard image change on mouseover, without having that image link to anything, then you are almost done. To prevent the hover image from “flashing” before loading, simply follow the steps listed on our sister article: A quick look: preloading images with css.

If, however you would like to have a mouseover effect on a linked image, it’s just a little bit more complex. In addition to the code above, we will add one more line to make this:
.buttonexample{background-image: url(images/button.gif); width:91px; height:28px;}
.buttonexample:hover{background-image: url(images/button-over.gif);}
.buttonexample a{display:block;width:91px;height:28px;}
and the following html:
<div class="buttonexample"><a href="#"></a></div>

This is the simplest method, but is not compatible with IE 5.5/6. This is because IE 5.5 and 6 do not recognize the “hover” attribute when applied directly to a DIV. To make this button compatible, we’ll just need to define the same variables as we did on the standard div, but for the “a” tag, like this:
.buttonexample a{background-image: url(images/button.gif); display:block;width:91px;height:28px;}
.buttonexample a:hover{background-image: url(images/button-over.gif);

Now, we have this link. Remember to change the width and height of this link to correspond with the size of your image.

Compatibility:
This method has been tested in and is compatible with Internet Explorer 5.5 and 6 (see above), 7, Firefox (PC and Mac), Netscape and Safari (PC and Mac).

That’s all folks!

If you have an idea or article that you would like to contribute, send it on! We’re always looking for good, quality articles. Note that we will not republish an article that has been published elsewhere, so keep it original!

Tags: , , ,

If you find our articles useful, "like" us!

Pass This Article On! Tiny Url: http://tinyurl.com/ddd6ww

9 Responses to “css: creating an image “hover” effect using only css”

  1. MattNo Gravatar Says:

    Nice article, you should check this one out as well, if you have not already.

    http://www.alistapart.com/articles/sprites

  2. DynamiXLabsNo Gravatar Says:

    Thanks for the heads up, and for the compliments! You have a great looking web site, by the way.

  3. MattNo Gravatar Says:

    Ah, no problem, and I appreciate the complement. I enjoy your site too, nice work. We should trade links?

  4. ciciNo Gravatar Says:

    Er,I following your steps,but it just work in "Firfox".How to make it work in IE6.0?

  5. ciciNo Gravatar Says:

    I feel very confused!!why this effect on your website is find.However,in my computer,it dosen't work ???
    It is my writing code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"&gt;
    <html xmlns="http://www.w3.org/1999/xhtml"&gt;

    <head profile="http://gmpg.org/xfn/11"&gt;
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <meta name="generator" content="WordPress 2.8" />
    <title>无标题文档</title>
    <style type="text/css">
    .ba
    {
    width:85px;
    height:25px;
    background-image:url(image/baoti1.gif);
    }

    .ba:hover
    {
    background-image:url(image/baoti2.gif)
    }
    .preload{display:none;}

    </style>
    </head>

    <body>
    <div class="ba"></div>

    </body>
    </html>

    ====it just work in FireFox=====

  6. DynamiXLabsNo Gravatar Says:

    Hi Cici,

    Towards the bottom of this article, we clarify how to have this function properly in Internet Explorer 6. Based on your example, you will want to change this:
    .ba
    {
    width:85px;
    height:25px;
    background-image:url(image/baoti1.gif);
    }

    .ba:hover
    {
    background-image:url(image/baoti2.gif)
    }

    to this:
    .ba a
    {
    display:block;
    width:85px;
    height:25px;
    background-image:url(image/baoti1.gif);
    }

    .ba a:hover
    {
    background-image:url(image/baoti2.gif)
    }

    and in your HTML, change this:
    to this:
    <div class="ba"></div>
    Hope this helps!

  7. KentaroNo Gravatar Says:

    I got some problems with this one. I can’t get the hover image to show, however everything else works just fine.

    Here’s my code:

    CSS:

    .button-middle a{background-image: url(Button.png); display: block; width: 151px; height: 42px;}
    .button-middle a:hover{background-image: url(Button hover (middle).png);}

    XHTML:

    Any help would be appreciated!

  8. KentaroNo Gravatar Says:

    I'm having bit of a problem with my button. I can't get the hover image to show, however everything else works just fine.

    Here's my code:

    CSS:

    .button-middle a{background-image: url(Button.png); display: block; width: 151px; height: 42px;}
    .button-middle a:hover{background-image: url(Button hover (middle).png);}

    XHTML:

    <div class="button-middle"></div>

  9. dynamixlabsNo Gravatar Says:

    Hi Kentaro!

    It may be that the comment editor broke your CSS, but otherwise it looks like the spacing in the image named "Button hover (middle)" may be the culprit. Have you tried resaving the image as Button-hover-(middle).png and adjusting your CSS accordingly?

We want your feedback! Leave us your thoughts below.






DynamiX Web Design

DynamiX Labs is Copyright © 2012 DynamiX Web Design, LLC.
DynamiX Labs is proudly powered by WordPress

Entries & Comments.  |  thanks & about.


The DynamiX Mascot button submit it! Background
hire dynamix!