CSS Rollover Button
Everyone likes a good rollover button. It creates interactivity for the user (which younger website users like) but also gives an indication that the button is clickable (which is important for older and less experienced website users). There are a lot of ways of implementing a good rollover. Most people jump to Javascript to do this because they think that’s the only way. However, this requires you to write javascript that preloads your images so that there is no delay on the rollover for the first time. Well, I’ve found a much better solution using only CSS that is clean, easy to implement, won’t break, and works across all browsers. I love it so much that I use it for all of my buttons on CollegeProwler (my fulltime employer).
So now you must be asking, how does this wondrous CSS miracle work? It is actually quite simple. Using only one image, you will restrict the size of your button (to only show part of the image which will be set as the background), and then use the pseudo-class :hover to shift the background-position of that image into the hovered portion. Again, this allows your full image to be preloaded so there is no delay on rollover. This has been cross-browser tested in Firefox 2 and 3, Internet Explorer 6* and 7, Safari, Opera, and Chrome, and it works perfectly. Here is an example below including the full image, the image as it will appear/work, the HTML, and the CSS.
This is the HTML:
<a href=”link location here“>
Put a summary of your button here. A few words will suffice, imagine this as the “alt” text for your image/link
</a>
</div>
This is the CSS that made the image on the left into the button on the right.
.rollover a{
display: block;
text-indent: -9999px;
margin: auto auto auto auto;
cursor: pointer;
outline: transparent solid 0px;
}
#exampleButtonIDHere a{
height: 100px; This height is the visible portion of the button only
width: 200px; Put your image width here
background: url('Put your image location here') no-repeat left top;
}
#exampleButtonIDHere a:hover{
background-position: left -100px; This negative value should match the positive height from above
}
So basically, a div, with a background image of both buttons, and a height and width restricted to the size of only one of the default button, shifts on :hover to pushing the hover portion of the image up into the viewable portion of the div, with the default portion of the image pushed out. Simple right? I know! If you are going to have a lot of these buttons, I’d suggest a whole separate CSS document for them. You can also combine the .rollover with the #exampleButtonIDHere. I keep them separate because it reduces the length of the CSS document with lots of buttons.
Don’t forget the a tag inside the div tag, and include the a as part of your CSS style declarations. Each portion of that CSS is very important! The “display: block;” allows for the image to not be pushed out of it’s own div. The “text-indent: -9999px;” pushes the alt text out of the way, the “margin: auto auto auto auto;” eliminates any cross-browser issues with default settings, the “cursor: pointer;” forces every browser to display it as a link with the mouse icon changing, and the “outline: transparent solid 0px;” forces browsers to not outline the link when being clicked. If you have any trouble implementing this, let me know, because I completely swear by this amazing CSS trick. I’d also love to hear if you’ve had successes implementing this on your own website. Again, for great examples, check out CollegeProwler.
*Be careful, if you use png images for your buttons (which I do because they are far superior to gifs with transparency) then IE6 won’t allow your rollover to work. Most png fixes that I have found so far disable background-position properties in CSS in IE6. Annoying, but really not the end of the world. Either live with it for whatever percentage of your users are IE6 users, or use jpgs and gifs instead. I say the pngs are worth it for the better transparency quality and lower file size.
September 17th, 2009 at 2:08 PM
what if i wanted to make a few buttons and have them centered on the page? floating prevents this no?
September 17th, 2009 at 11:21 PM
Todd, floating does remove auto margins, but the CSS above doesn’t use floating. If for some reason you need to employ a float but also want your buttons centered, you’ll have to hardcode your margins.
October 9th, 2009 at 3:56 PM
Hi David-
I am trying to put 6 buttons side by side and having trouble. Can you help? Take it easy on me, I’m a newbe
October 9th, 2009 at 6:11 PM
Chad, I have to see an example of your code to be able to help. Basically, the code above would work for putting 6 buttons side by side, just add css to the div itself to float: left so that they line up next to each other.
November 14th, 2009 at 4:06 PM
Very simple and to the point.
I do have one issue and workaround. When I try this, the hit area that activates the roll over is the parent div (which is correct) but the hit area for the link is only the space taken up by the “a” tag, which depends on the size of the font, line-height and who knows what else; if it’s small, it may not be anywhere near the size of your button.
I simply set the padding of the specific “a” tag manually to fill the parent box. Any ideas on why this is happening. Maybe there’s something I could be doing to prevent the need for the fix.
Thanks for the example.
November 14th, 2009 at 11:46 PM
Mugur, the workaround shouldn’t be necessary. If you look at the CSS closely, it applies to the a tag, and not to the div surrounding it. The div is what contains the class and ID information that identifies the correct a tag to apply the CSS to, but technically you can bypass the need for the div and apply a class and/or ID to the a tag directly.
November 15th, 2009 at 12:09 AM
David,
Thanks for the reply; I double checked my css code and found the mistake. Simply forgot to target the A and was setting the properties for the DIV instead.