Discover & Rate New Music Check out ChartVote. Promote the music you like.


Simple CSS Image Rollover

Back when I first started doing web development I used Javascript Image objects and onMouseOver and onMouseOut to make image rollovers for web pages. This worked well, but was kind of clunky and required 2 separate images.

Now I use CSS with only one image to do these same rollovers. You are basically just shifting the background image's X coordinate to give the rollover effect.

The following browsers support this:
IE 5.5+, Firefox 1.07+, Opera 7.23+, Mozilla 1.7.12+, Netscape 6.02+, Safari 2.0+, Konqueror 3.4.3+

Example

The Image

CSS

CSS:
  1. a.srollover {
  2.     display: block;
  3.     width: 22px;
  4.     height: 22px;
  5.     background: url("close.gif") 0 0 no-repeat;
  6.     text-decoration: none;
  7. }
  8.  
  9. a:hover.srollover {
  10.     background-position: -22px 0;
  11. }

HTML

HTML:
  1. <a class="srollover" href="#">&nbsp;</a>


37 Comments

  1. Peter Harkins on October 31st, 2006

    If IE has its caching set to "check every time", you'll see a very unpleasant flicker onmouseover using this technique. Google around a little, it's a well-known and easily-reproduced bug.

  2. Noah Winecoff on October 31st, 2006

    Thanks for the heads up on this issue Peter.

  3. johnie1 on October 31st, 2006

    i love this technique (:

  4. J. Nunn on November 1st, 2006

    Nice--never thought of doing it this way. I'll be integrating this into my projects soon.

  5. xxdesmus on November 1st, 2006

    This is not a new technique by any means, in fact it's been around for probably a year or more. As Peter mentioned: it's nice, but hardly without faults.

  6. Noah Winecoff on November 1st, 2006

    xxdesmus,

    I don't believe anyone said it was a new technique. Thanks for the comment!

  7. niko on November 1st, 2006

    @Peter Harkins:
    There exists a workaround for this problem:
    http://dean.edwards.name/my/flicker.html
    which is working fine here...

  8. Image rollover using CSS at ASP.NET Tricks on November 2nd, 2006

    [...] You can review the full artcile with example how to achieve this, here. button, css, image rollover, javascript, OnMouseOut OnMouseOver [...]

  9. Pieter-Jan Savat on November 2nd, 2006

    Nice, but I'll stick with what I use. Because I found when
    you click your mouse and drag outside the image area
    and repeat this quickly a couple of times, you'll see the black
    image with a white vertical stripe. (using Firefox 2.0)

  10. Derrick on November 3rd, 2006

    This has been around for more like 3 years. It's cool though.

  11. Spencer on November 6th, 2006

    This is so elegant.. it'd be great if I could do a horizontal menu with it, but I can't seem to make it happen... thinking....

  12. Tom Molskow on December 14th, 2006

    Greeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeaaaaaaaaaaaaaaaaaaaaaattttttttttttttttttttt!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

  13. julien on December 17th, 2006

    For the flicker bug, if you have a parent tag, you can set the image as a background to it, so during the little flicker, the Hover image will show up even in IE.

  14. hannah on February 18th, 2007

    this is cool , but i cant seem to get multiple buttons to sit horizontally? any clues!

  15. hannah on February 18th, 2007

    ok im retarded

    nav li {

    float: left;

    }

  16. RParker on February 23rd, 2007

    The flicker is not a problem with this solution because this is a single file. The flicker only happens when using two images to do a mouseover. Larger buttons are somewhat delayed, not as clean as two seperate images, but much easier to maintain and works across all major browsers.

  17. Todd on February 27th, 2007

    So, when I offset the top margin by -10px, in Firefox, the rollover effect only works when I roll over the bottom portion of the image (18 x 18). How can I work around this?

  18. Morph on March 9th, 2007

    Works great, but once the image/URL is clicked a nasty border is shown around the image. See the exampel above. Sort of ruins it all :(
    (Netscape)

  19. some guy on March 9th, 2007

    @ Morph: umm... doesn't the same thing happen with hyperlinks?

  20. SIMPLES ROLLOVER EM IMAGENS COM CSS » Pinceladas da Web - Reflexões sobre XHTML, CSS, PHP e WebStandards on March 19th, 2007

    [...] Veja um exemplo em funcionamento. Esse tutorial foi retirado do site: Find Motive. [...]

  21. Robert on May 4th, 2007

    @Pieter-Jan Savat

    "you'll see the black
    image with a white vertical stripe."

    That's probably because you end up highlighting (selecting) the text inside the anchor (the non-breaking space)

  22. Kumi on May 5th, 2007

    @ Morph: Do you know of a method of removing the "nasty border"?

    Because I sure don't. And I agree with "some guy" - the same thing happens with hyperlinks.

    And why would you need to click the picture anyway?

  23. Jeff on May 10th, 2007

    morph.. that's not happening with me

  24. mario on June 7th, 2007

    what would appear if the browser is older than the supported listed ..

    anythiing?

  25. Helge on August 23rd, 2007

    To Pieter-Jan Savat,

    The white vertical stripe comes probably because the cursor manages it to mark the text content because the solution is implemented as a link (tag a). I solved that problem by removing the space (nbsp;) from the link.

    Cheers

    Helge

  26. mouseover effekt funktioniert nicht - jswelt - Forum (Javascript, PHP, MySQL, AJAX, Webdesign) on September 4th, 2007

    [...] AW: mouseover effekt funktioniert nicht - Heute, 18:21 z.B. auf diese Art: Simple CSS Image Rollover - Find Motive Free Download & Internetguide [...]

  27. Matthew on October 10th, 2007

    I love this for its simplicity - but it has a major drawback for me. If the stylesheet becomes disconnected or if someone visits the site with different device with a non-standard browser (blackberry, treo, etc) the links don't show up at all! Web is supposed to be about delivering information, and without the stylesheet there's no information delivered...

  28. Matthew on October 10th, 2007

    Actually - on second thought - a workaround for this is to drop some link text into a span tag within the anchor that is hidden:

    a.srollover span {
    display:none;
    }

    Alternate Text

    Haven't tried it. But it makes sense to me now...

  29. Blue Harvest » Blog Archive » Frustration, Terror, and Mayhem... on November 7th, 2007

    [...] Possible solutionshere and here, both of which will require me to start over. [...]

  30. vy on January 8th, 2008

    How do you make multiple buttons with all horizontal aligned.

    Thanks

  31. Gimp on January 29th, 2008

    @Morph: To get rid of the nasty outline when clicking on a link:

    :focus {
    outline: 0;
    }

    Doesn't work in all browsers, but it's probably the best you can do.

  32. 10 Site ve örnek uygulamalar ile CSS on February 3rd, 2008

    [...] CSS ile rollover yapım teknigi [...]

  33. Bobby on April 7th, 2008

    If you are trying to use this to make a horizontal menu replace this:
    display: block;

    With this:
    display: inline-block;

    That should do the trick.

  34. Devon on April 10th, 2008

    Good effect but buggy in IE8 Beta.

  35. Simon on April 12th, 2008

    IE8 Beta? Report it as a bug to the IE team then.

  36. @vy on May 26th, 2008

    I need to know the same thing, didnt figured it out yet. anyone?

  37. Karin on June 18th, 2008

    Great tip! Works like a charm! Thanks for sharing!

Leave a Reply

RSS Feed



Recommended Sites