CSS rollover buttons, sans JavaScript

Written by That Web Guy on 31st May 2009. 5 comments

CSS rollover buttons, sans JavaScript
  • Avant Innovations
  • Advertise Here

While scoping for a new designer I recently read through over 50 resumes and consequently looked at over 50 online portfolios. What struck me as frankly surprising (shocking) was the high number of designers who still use oodles of JavaScript to create a simple image hover effect, usually in the navigation.

I have a simple rule: If it can be accomplished with CSS, then do it with CSS. Unless it breaks standards or semantics of course. So here is my simple tutorial preserved for prosperity in the hope it will one day reach the right people.

How it works

We are going to use a single image to create 3 buttons states: default, hover and active. Basically we are going to position the background image for each state with CSS. The image (button.gif) I've created for this tutorial looks like this:

button

The dimensions of this image are critical, in that all 3 states need to be the exact same width. Each button state in this example is 120 pixels wide, which means we also have to make it known in the CSS.

Start with a simple menu list like...

<ul id="menu">
<li><a href="">Item 01</a></li>
<li><a href="">Item 02</a></li>
<li><a href="">Item 03</a></li>
</ul>

...with this CSS.

#menu li {
list-style:none;
text-align:center;
float:left; /* Remove this float if you prefer a vertical menu */
}

#menu li a {
width:120px;
height:30px;
padding:10px 0 0 0;
display:block;
background-image:url(button.gif);
background-position:left;
text-decoration:none;
color:#fff;
}

Notice we have positioned the background image left for list items which are 120px wide, which means only the left (blue) part of it will be visible.

Changing states

Now we want to change the position of the background image for 2 more states, hover and active. Add this CSS:

#menu li a:hover {
background-position:center;
}

#menu li a:active {
background-position:right;
}

Now when you hover over a menu item the background position shifts to the center showing only the green section, and when the mouse is active the background position shifts to the right to only show the orange section.

Too easy huh? Test it out for yourself here.

Why one image and not three?

The exact same effect can be achieved with 3 separate images, but each would have to be referenced accordingly in each of the 3 CSS states, but using a single image is better because it means you only have one http request as opposed to 3.

button_states.zip (8k)

Is this worth sharing?

That Web Guy

About That Web Guy

That Web Guy (Mikey to his friends) is a veteran web designer based in Perth, Western Australia, and currently Design Director at Perth Web Design. When he's not XHTML'ing or messing around in Photoshop, Mikey can usually be found preaching web standards evangelism onto unsuspecting victims.

Feel free to send That Web Guy a message some time, follow him on Twitter, or make a donation.

Comments

Not a Member

Davo

A good lesson to those still using MM Script

Sunday 31st May 2009 | 10:04 AM Reply Comment URL Back to top

@todayinart

@todayinart

Another one that I like to use, especially when I have a cool background, is to change the opacity of the menu item.

#menu li a:hover {
/* for IE */
filter:alpha(opacity=75);
/*for IE8 - though it seems to work without the quotes still*/
filter: "alpha(opacity=75)";
/* CSS3 standard */
opacity: 0.75;
}

Tuesday 2nd June 2009 | 09:19 PM Reply Comment URL Profile Back to top

Not a Member

Greg Molyneux

Great little tip Mikey. I do my best to avoid Javascript where possible, but I'm still quite new at design some I'm just trying to hone my CSS skills a little bit at a time while focusing on doing things the right way.

I really dig(g) the new site by the way!

Friday 5th June 2009 | 03:52 AM Reply Comment URL Back to top

owen

owen

I'm using this one! Cool trick to remove the messy Javascripts. Hope you also include drop down menu tutorial. Thanks

Friday 12th June 2009 | 11:59 PM Reply Comment URL Profile Back to top

Not a Member

Edwardo

I wasn't even aware people still used javascript for a simple effect like that. Liking your site very much web guy.

Sunday 28th June 2009 | 09:34 PM Reply Comment URL Back to top

FYI: You are currently not logged in. It's cool though - you can still comment. But only members get a profile page, access to the download section and they can pimp their own web sites. Feel free to register or Login now!

Your name:

Your comments:

Note: HTML tags are automatically stripped from comments.

Are you human?
Turing

Sorry, I have to ask. So what sort of animal is this? (Hint: you don't have to be perfectly specific)

Back to top

Login to That Web Guy Blog

Login

Not registered? | Forgot your Password? Cancel Login