Prevent Rollover Image Flicker the Proper Way
It’s annoying when you’ve got a beautiful button designed and find out it flickers when the mouse rolls over. Fortunately, there’s an easy way to fix this issue with a little bit of code.
What is an image flicker?
For those who don’t know what an image flicker is it’s basically caused when the button is rolled over by the cursor for the first time causing the second image to load. For a split second, while the second image is loading, there is a little flicker which basically looks like a blank spot filled with nothing. This issue is a common occurrence in all browsers.
Solving the problem
It’s simple, we’ll use one image defined as a CSS background.
Here’s the image:

Here’s the code:
ul li {
list-style: none;
}
This one is pretty self-explanatory. The list-style hides the bullet styling.
ul li a {
display: block;
width: 122px;
height: 41px;
background: url(‘btn_flicker.gif‘) no-repeat 0 -41px;
}
This code basically does all the major work for the button style. Setting the display to block allows the width and height properties to define the clickable area of the button. The background property is also set and this is where the one image is used. Be sure to add no-repeat in the background tag so that the image is not repeated. You’ll also want to set the position of the image within the background tag.
ul li a:hover {
background-position: 0 0;
}
The code above is the fun part. The background-position makes the image switch to the rollover state.
span {
display: none;
}
This hides the text link.
And we can’t forget about the HTML. Here’s the short snippet:
<ul>
<li><a href="#clickme"><span>Button</span></a></li>
</ul>
That’s it?! No more HTML or use of Javascript? Yep, that’s it. Keeping it simple is pretty easy.
Now, you’ll probably notice that some text was placed between the list element. The text needs to be there for accessibility reasons even though the text is hidden from the user. The link text allows search engines to pick up the name of the URL. Adding text to the button also allows users to view the link on a browser/mobile phone that may not support CSS.
Here’s what the button looks like when the CSS is disabled:

Pretty cool, huh? Check out the demo or download the source code to see the button in action or to experiment on your own.
View Demo and Source Code
User Comments
Man, this must be the best kept secret in the land of image flickering. It worked beautifully…thanks a million Aaron..
Great tip man! I have to admit I under utilize lists… =\
Tyler says:
Just used this tip man… it’s awesome! I’ve never actually used it before because I’ve been okay with the flicker, but the site I’m working on (it’s a secret for now) I want to be perfect
Tyler, I’m glad you were able to find it useful! Can’t wait to see how you used it!
Charlie says:
How do you make the button stay “on” after it has been clicked?
Aaron says:
Hey Charlie,
In response to your question, you can add a class in the href of the link. So something like class=”selected” would work.
In the CSS you would need something like: ul li a.selected { background-position: 0 0; }.
If that didn’t quite answer your question you could also add a:visited { background-position: 0 0; } in your CSS.
Let me know if this answers your question or if I can help you with anything else. Thanks!
Charlie says:
Thanks a lot Aaron! I’m not very experienced with this but I’m trying some rollover effects in my website. I will try your solution and see what happens.
Charlie says:
It works great. I used: a:visited { background-position: 0 0; } in css.
Another question, what happens if you want to use different images with different sizes (as buttons) for your navigation?
Thanks a lot!
Steven says:
It works perfectly. Thanks.

Scott says: