Single Image CSS Rollover Effects

CSS | PNG | Rollover | Image | No Javascript | Menus | Design

How To Use CSS To Make Rollover Effects Using A Single Image And No Javascript

TAGS: None


There are a lot of different ways to make rollover effects on menu items these days. Most of these involve two separate images with color changes between the two copies of the image. Most of them also involve using javascript to cause the images to swap when the reader hovers their mouse over the image.

Some designers, like myself, eschew this path because we like things light. An extra image and a bunch of extra javascript just don’t feel right. Many of us simply use CSS to style rollover effects on text elements like anchors or list items. That actually works pretty well, but has a serious limitation.

Sometimes you just don’t want the text on the page. For SEO reasosn, generally, you don’t want to use text in the anchors for the rollover links. In these cases, I’ve always reverted back to the two image, javascript rollover, but now I’ve stumbled on another way of doing this.

I really can’t believe I never thought of this before. I’m sure someone else has been doing it for ages, maybe you already know about it and I’m just behind the times here.

Basically the trick is to use a transparent .png image to create the “button” look for your rollover and then apply color changes to the container element when the user hovers over it.

Here’s what it looks like:

Here’s the html code

<div class="pngtest">
<ul>
<li><a href="#"><img src="home.png" alt="PNG Test Btn 1" /></a></li>
<li><a href="#"><img src="blog.png" alt="PNG Test Btn 2" /></a></li>
<li><a href="#"><img src="good.png" alt="PNG Test Btn 3" /></a></li>
</ul>
</div>

and the css

.pngtest {
margin: 0;
padding: 0;
}

.pngtest ul{
list-style-type: none;
margin: 0;
padding: 0;
}

.pngtest li {
margin:0;
padding:0;
width: 150px;
height: 25px;
display: inline;
float: left;
background: #660066;
overflow: hidden;
}

.pngtest li a:hover {
background: #fe6700;
}

Initially I used this code to highlight the list item behind the image, and that works in Firefox, but not in Internet Explorer.

.pngtest li:hover {
background: #fe6700;
}

Highlighting the anchor as in this code still works in Firefox, and it also works in Internet Explorer, so this is the way to go.

.pngtest li a:hover {
background: #fe6700;
}

The overflow: hidden; is there on line 20 because for whatever reason Internet Explorer insisted on adding space to the list items beyond the image borders even with all of the boundaries specified for ‘0′. This resulted in a band of about 5px tall colored below the image row. adding the hidden overflow statement removed this color band.

Of course, if you don’t need the button look, and you are just doing this to hide the text from the search spiders, you can always just use an image with the text on a transparent background. Then it will llok and behave like a traditional CSS/XHTML menu class, but the text won’t be there. You would probably want to additioanlly name the images with numbers instead of the text you are hiding. I gave these names just to make it more apparent what is happening in the code.

If you have a lot of readers using Internet Explorer 5.5 or older browsers, the transparent .pngs will not be transparent, but will have an ugly gray background. You can look for a png fix for this, but they are not always 100% and i don’t understand why some work sometimes but not other times, so I can’t recommend any. Hardly anyone visits my site using an old browser, so i have decided i really just don’t care enough for the extra effort involved. Especially since i don’t have a copy of the old browsers to test with and have to rely on annoying procedures like using ThumbShots for each test and tweak.

TAGS: None

9 Responses to “How To Use CSS To Make Rollover Effects Using A Single Image And No Javascript”


  1. webserviceengine (1 comments)
    on Jan 17th, 2010
    @ 01:59

    In some browsers these codes are not accepting?.any other way to perform


  2. flash (1 comments)
    on Jan 28th, 2010
    @ 16:21

    Thank you for the sharing! I am trying now to learn so much css, thanks for your help


  3. eldahman (1 comments)
    on Jan 31st, 2010
    @ 06:09

    Very good post keep going man


  4. Pariuri Mozzart (1 comments)
    on Feb 5th, 2010
    @ 07:34

    Thanks for an interesting post


  5. blogcosep (1 comments)
    on Feb 13th, 2010
    @ 05:20

    Nice one,, Thank you for sharing it.
    blogcosep´s last blog ..The Top Five Boxers of the Post-Pacquiao Era My ComLuv Profile


  6. organize matbaacılık (1 comments)
    on Feb 13th, 2010
    @ 11:04

    sevgili arkadaşım / dear friend,

    was a very good article.
    I am happy to see my site
    organize matbaacılık´s last blog ..Organize Nedir? My ComLuv Profile


  7. Airfone (1 comments)
    on Feb 15th, 2010
    @ 20:31

    Yes. These codes are not accepted in certain browsers. Anyway to use them in such browsers.


  8. Arya (1 comments)
    on Feb 23rd, 2010
    @ 09:29

    thanks for the code and tutorial. Its very helpfull.
    I hope i can try this one
    Arya´s last blog ..Feedline : The First Integrated Google Buzz Apps for iPhone My ComLuv Profile


  9. Mozie (1 comments)
    on Mar 6th, 2010
    @ 09:28

    Very cool Article, keep up the excellent work. I have to mention that your blog looks very cool. Contact me if you need help with your Web Design and graphics :)

© 2009 Dane Morgan | Experimental Blogger. All Rights Reserved.

This blog is powered by Wordpress and Magatheme by Bryan Helmig.

Bad Behavior has blocked 819 access attempts in the last 7 days.