Create a CSS image sprite based feature box

Written by That Web Guy on 29th December 2009. 9 comments

Create a CSS image sprite based feature box
  • Avant Innovations
  • Rusty Lime
  • Advertise Here

Inspired by some of the excellent feature boxes I've seen on countless sites, I thought it about time I made my own but only using CSS and the excellent image sprite technique. Not familiar with image sprites? Here's a quick primer.

Note that this feature box would still work perfectly fine if you were to have each image separate, but less images means less HTTP requests and a less 'laggy' responsiveness for the user. Here's what it looks like:

css feature box

So can see a working demo here.

It's comprised entirely of only 2 images and is pretty simple. Your semantic HTML looks like this...

<ul id="features">
<li class="design"><a href="">Design</a></li>
<li class="programming"><a href="">Programming</a></li>
<li class="cms"><a href="">Content Management Systems</a></li>
</ul>

...and the CSS:

/*** Feature Box *****************************/
#features {
width:740px;
height:300px;
margin: 0 auto; /* Center the box in the page - can be safely removed if desired */
background:url(images/images.jpg) no-repeat 300px 0;
border:solid 1px #e6e6e6;
}

#features:hover {
background:url(images/images.jpg) no-repeat 300px -600px;
}

#features li {
height:100px;
width:350px;
list-style:none;
text-indent:-9999em;
}

#features li a {
height:100px;
width:350px;
display:block;
}

/*** Design *****************************/
#features .design {
background:url(images/feature_sprite.png) no-repeat 0 0;
}

#features .design a:hover {
background:url(images/feature_sprite.png) no-repeat 0 -300px;
width:740px;
}

#features .design:hover {
background:url(images/images.jpg) no-repeat 300px -300px;
width:740px;
}

/*** Programming *****************************/
#features .programming {
background:url(images/feature_sprite.png) no-repeat 0 -100px;
}

#features .programming a:hover {
background:url(images/feature_sprite.png) no-repeat 0 -400px;
width:740px;
}

#features .programming:hover {
background:url(images/images.jpg) no-repeat 300px -400px;
width:740px;
}

/*** Content Management Systems *****************************/
#features .cms {
background:url(images/feature_sprite.png) no-repeat 0 -200px;
}

#features .cms a:hover {
background:url(images/feature_sprite.png) no-repeat 0 -500px;
width:740px;
}

#features .cms:hover {
background:url(images/images.jpg) no-repeat 300px -500px;
width:740px;
}

So what's going on here?

Each list item requires a class in order to position the background image. I've used .design, .programming and .cms for this demo.

Starting with .design the background image position is simply 0 and 0 (0 pixels left and 0 pixels top). On design a:hover the background image is moved -300px to show a different part of the image giving the illusion that a different image has been loaded. This same method is applied for the other 2 classes while moving the background image even further upwards to reveal the appropriate part.

For a little more bling I decided to add a small enhancement. A second background image is in the right of the feature box which also changes position when the mouse passes over one of the 3 list items. There is a slight complication with this though. Because we can't move 2 different background images with a single a:hover, we have to change the right image position with design:hover, programming:hover and cms:hover respectively.

Sounds confusing? It will make more sense when you look at the full CSS.

Yours to modify, use or improve as you see fit. Tested and works fine in all the browsers that matter, and Internet Explorer 7 & 8 :-)

css_feature_box.zip (492k)

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 Senior Designer at Titan Interactive. Mikey's work has been featured on many CSS showcase web sites and when he's not XHTML'ing or messing around in Photoshop, he can usually be found preaching web standards evangelism upon unsuspecting victims.

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

Comments

A Novice

A Novice

I don't mind that. FYI it almost works in IE6 but I know you said only the browsers that matter :-P

Thanks for sharing.

Tuesday 29th December 2009 | 10:32 AM Reply Comment URL Profile Back to top

Not a Member

Andrew

Thanks I'll use that. Keep up the good work.

Tuesday 29th December 2009 | 12:36 PM Reply Comment URL Back to top

Not a Member

Diso

Worked perfectly! Thanks for including the photoshop file that made it easy.

Wednesday 30th December 2009 | 05:47 AM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Diso

You're welcome. I'm happy to know it worked out for you.

Wednesday 30th December 2009 | 05:50 AM Reply Comment URL Profile Back to top

Jasmine

Jasmine

I love this method. Now taking it a step further and including the entire text in the buttons. Have I ever told you I love your work???

Wednesday 30th December 2009 | 06:02 AM Reply Comment URL Profile Back to top

Not a Member

Andrew

Just letting you know I've used this on a site yesterday and it worked great. I'll post a link once it's live.

Thanks Web Guy.

Wednesday 30th December 2009 | 08:19 PM Reply Comment URL Back to top

That Web Guy

That Web Guy

Responding to this comment by Andrew

Excellent. Yeah be sure to add some link love here when it's online.

Wednesday 30th December 2009 | 08:26 PM Reply Comment URL Profile Back to top

ScottFoley

ScottFoley

Excellent tutorial. I'm sure that will get plenty of use. :) Thanks.

Wednesday 30th December 2009 | 11:22 PM Reply Comment URL Profile Back to top

Josiah

Josiah

I love it mike! The instant reaction to user input means this feels like the website is reacting instantly and adds to the user perception of a snappy website.

Sunday 3rd January 2010 | 08:51 AM Reply Comment URL Profile 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