
You might have been thinking about using Adobe Flash (in case you didn’t know, Adobe bought Macromedia) to create a .swf file which allows you to display dazzling rollover effects. That’s perfectly fine, but when it comes to faster loading time, simplified effects and crossbrowser compatibility, rollover effects achieved by simple CSS and (X)HTML code beats it hands down. If you’re ready to go, don’t stop reading!
Take Two
Of course you can stick to just simple text for your links, but what if you adore a more sophisicated, user-friendly and attention-grabbing list of links? There are actually two ways to achieve the rollover effect - one is instructing the browser to load a separate background image when you hover your mouse over the link, the other is simply ask the browser to alter the coordinates of the background image. Maybe this illustration will help clear things up a little:

Personally I prefer the latter for a few reasons:
- Reduces the number files the browser needs to load
- Need not load two images separately (inactive and hover states)
- Rollover effect works perfectly when you’re disconnected
- No flickering effect in Internet Explorer
Make your own rollover icons
Since I’m yet to learn how to create icons, I usually use certain icons from other marvellous, free-of-charge icon packages generously shared by their respective designers. I would recommend the following:
- FamFamFam Mini Icons - if you like small, nifty ones, this is it!
- Dropline Neu! - glossy icons that come in many different sizes.
- Vista Icons - icons from the latest Windows OS
- iPhone Handdrawn icons - woah this set is going to be classic! I just love it.
Of course you can google for more icons, there are a lot more out there, for free! But of course, please do remember to give credits to the icons’ respective owners - be appreciative and respectful of their ownsership (copyright, anyone?) and effort.
Let’s say, you want to start off with a RSS icon. Well what you need at hand is a simple image editor, like Gimp or Photoshop (I’m not sure whether Paint does offer so much functionality). Since I’m so bad at Gimp, I guess my only option is Photoshop huh? Right click to save the gigantic version (320*320, it’s HUGE!)
Step 1: As usual, open the image

Just like what you’ll usually do, drag and drop the image file into Photoshop workspace. If there happens to be any transparent area (don’t forget this especially if you’re working with PNG icons!), create a new fill layer by going to Layer > New Fill Layer, and make the fill colour same as the background colour of the page you’re intending to use your icons.
Step 2: Double the height

Now what we’ll want to do is to double the height of the canvas. This can be done by going to Image > Canvas Size or by punching in Alt + Ctrl + C. Multiply the original height by 2 (in this case, the original height is 96px so it’ll be 192px) - simple maths, it’s as easy as that. And before you proceed, don’t forget to make sure that the canvas size expands downwards (just click on the arrows, shown in the screenshot above). When you’re done, you should see this:

Step 3: Duplicate image layer

Right click on the image layer and select Duplicate Layer, and poof, you’ll get an identical copy of the image. Nudge it towards the blank area of the canvas. Since you’ve doubled the height, the two icons should fit perfectly in the canvas now.
Step 4: Determine the rollover effect

So now what you really need to do is just to think of what rollover effect you want to achieve. Do you want the icon to get bigger / smaller, or do you want it to become clearer? It all depends on you. For this example, I’ve decided to decrease the opacity of the top icon to 50%, so that it appears faint at first, but when you hover your cursor above it, you’ll see the icon clearly. This effect is identical to that of the top-right navigation bar of this page (this may not be true if I’ve changed themes). Remember to save!
Pain in the butt coding and styling
Now the problematic part. What should I do to materialise the rollover effect? You can’t possibly plop the image somewhere and expect it to happen by itself. So, we’ll need CSS and XHTML to help us out. This is what we want to do:
- Create a list of links, you can use either the <ul> or <li> tags
- Make sure that CSS loads the images into the links
- Create rollover effect with CSS
- And you’re done!
(X)HTML coding Now we’ll create a list of links using XHTML coding. It’s actually quite simple!
<ul id="navigation" class="clearfix"><li><a href="#" title="RSS Feed" id="rssfeed">RSS Feed</a></li>*You can add more if you wish, but remember to get the id correct!</ul>
Don’t forget to change the link from “#” to something else! For example, if you’re linking your blog feed, copy and paste the link inside the quotation marks after the href tag.
Tricky CSS
This is the harder part, but don’t worry, I’ll try my best to guide you through.
.clearfix:after {content: ".";clear: both;display: block;height: 0;visibility: hidden;}/* Hides from IE-mac \*/* html .clearfix {height: 1%;}/* End hide from IE-mac */#navigation {background: transparent;margin: 0;padding: 0;width: 100%;}#navigation li {float: left;list-style-type: none;}#navigation li a {display: block;margin: 0 3px;padding: 0;height: 96px;width: 96px;text-indent: -9999px;}#navigation li a#rssfeed {background: url(images/rssfeed_bg.jpg) no-repeat top left;}#navigation li a#rssfeed:hover {background-position: bottom left;}
I shall explain the code bit by bit
.clearfix:after
- *This is the holy hack!
- Of course you can always opt to insert
<br style="clear: both" />at the end of the floated element but it’s messy and unsightly. So this is the clean way to fix it without any additional markup. - The code is actually from here, they say it’s a little old, but hey, it works!
#navigation
- background - I usually set it to transparent by default, but you can always specify the colour or background image you want for the element. To have a background color, use:
background: #xxxxxx;wherexxxxxxstands for the hexadecimal colour code. For image, use:background: url(imageurl) top left no-repeat;. If you want to repeat the background, you can repeat it horizontalll:repeat-xor vertically:repeat-y. - padding and margin - feel free to change the values of these elements to suit your page. Usually I’ll set both to zero, unless other reasons call for it to be changed.
- width - set to a value in terms of pixels or percentage to your own liking. Since I usually wrap the entire list in a <div> container, I set the width to 100%.
#navigation li
- float -
float:left:is used so that the each component of the list will stack up against each other horizontally and not the conventional vertical arrangement. - list-style-type - it is set to none because you wouldn’t want to see the ugly little bullets floating around right?
#navigation li a
- margin - this represents the spaces between each tab.
- height and width - if you’ve noticed, they are set to the original size of the icon (not the modified canvas size!).
- text-indent - intentionally set to -9999px, this causes the text between the <a></a> tags to be displaced way beyond any monitor on earth but not removing the text. This is to make sure that text readers can find your RSS feed as well (they don’t read images).
#navigation li a#rssfeed
- background - I’ve specified the CSS script to load the image rssfeed_bg.jpg from the images folder, relative to the directory where the CSS file has been placed in. The background is not repeated (the no-repeat statement) and the image is positioned at the top left corner (alternatively, you can use top center) so that the upper half of the image is displayed.
#navigation li a#rssfeed:hover
- *This is the key to the rollover effect
- background-position - it has been altered to bottom left (you can use bottom center as well), so the bottom half of the image is displayed.
See it live!
You can see this styling (not identical but similar) being in use on a rollover effects demo page!
Rollover Effects with CSS | 177 hits | 4 KB
There are two examples in the page, be sure to check them out! They are actually variations of the original styling and coding I’ve provided. Feel free to experiment with it!
Hope you find this tutorial useful! ![]()


15 responses to Rollover Effects Made Easy - with CSS!
Responses can be separated into comments and trackbacks
Comments »
March 18th, 2008 at 12:32 pm
Good tutorial. I rarely use image rollovers but maybe I should give it a try sometime.
As for clear both usually just this code will do. It works from IE 6 onwards and of course in Firefox and Opera. (not sure about safari, and probably doesn’t work on IE 5.5 but who the heck uses IE 5 anyway)
.clear {
clear:both;}
And on your XHTML inserting something like this will do the job.
Check out Ivy’s last blog post: Craigslist Ad: Beauty Isn’t Worth Money, Honey
March 18th, 2008 at 12:35 pm
Dammit.. looks like the [code] doesn’t actually work in the comments. I meant for the XHTML to say:
[div class='clear'] [/div]
Check out Ivy’s last blog post: Craigslist Ad: Beauty Isn’t Worth Money, Honey
March 18th, 2008 at 10:03 pm
Hey Ivy thanks for your comment. Ah wells this rollover effect is actually quite basic CSS, I believe most of the browsers web users use are able to handle it in one way or another. IE 5.5 is seriously a big screwup, just like IE6, there are just a big dictionary-like list of CSS hacks and fixes. Gee, I agree that crossbrowser styling is a big pain in the butt.
The clear property works in Safari, but as far as I know, the clearfix css does not seem to work pretty well in IE mac. But they say that since Microsoft has already begun phasing out IE mac, it’s not a big problem afterall.
I’ve tried using both
<br class="clear" />and<div class="clear"></div>but Firefox fails to render the margins correctly. Maybe these two screenshots might help clear things up a little:In IE: clearfixcss_ie.png
In Firefox (the buggy one): clearfixcss_ff.png
Clearfix seems to be the only fix. Even the clear property doesn’t make Firefox to render the spacing properly, weird eh?
Any takers?
P.S. anyway Ivy, I just realised that only by using < and > works, don’t know why. Hmm.
P.P.S. I’ve found this plugin that automatically escapes the code enclosed between <code> tags, very handy indeed!
March 19th, 2008 at 2:12 pm
IE has stopped production for macs a long time ago. There’s no need to cater for them any longer.
I don’t really understand the BR clear tag property since I use margins to control all containers. I only use the clear property between divs so I haven’t run into problems so far.
Check out Ivy’s last blog post: Beauty Isn’t Worth Money, Honey
March 19th, 2008 at 2:18 pm
I’ve seen other wordpress themes using the
tags to clear floated containers, and yea of courseworks equally fine.I didn’t have any problems with floats, margins and paddings when designing Wordpress themes - I get them only then they’re out of Wordpress (ie, in HTML files etc). This is weird. When I save my blog as a .HTML file and I open it up in my browsers, the display is totally screwed up! Looks like Wordpress team did some tweaking to the coding
March 18th, 2008 at 7:30 pm
wow another really useful post.
i wonder how many comments i’ve made here begun with wow.. -_-’
Check out momo’s last blog post: Facebook App: Graffiti
March 18th, 2008 at 10:14 pm
LOl momo it’s perfectly fine. Wow to you is just like Haha to me, I use it very, very often!
March 19th, 2008 at 2:23 pm
*praises you*
I was struggling with rollover effects today and I couldn’t understand why my coding wasn’t working. You know I really just wanted to try something new and possibly take my websites to the “next level” in my book…but nothing was working. I just settled on looking for a good script and just messed around from there. I think I should probably learn how to do it with CSS instead. I have to add, your illustrations are so great. I was looking for a big RSS feed icon. I want to try and make my own using the little ones.
March 19th, 2008 at 10:54 pm
Hey Melle thanks for being so supportive! I hope that this tutorial is of good use to you. Frankly speaking I find your webdesign clean and elegant, it’s almost perfect!
Hope you’ll be back from your hiatus soon! Take care!
March 20th, 2008 at 1:47 am
Woah. Kudos to you teddY for putting in so much effort in making this tutorial!
As much as I love knowing about tech stuffs, I’m just too tired to learn it now. Hehe. But I’m sure many out there will find it useful!
Check out SilverIsle’s last blog post: An evening at Nobbys Beach
March 20th, 2008 at 2:33 am
I have used the CSS rollovers before, but it’s always a battle to get it right and there is always a fine line between frustration and wanting to headddesk out of frustration! Haha! But thanks for the tutorial. Seriously, it’s nice that you are doing service to those who will need this technique. I wanted to implement it in my redesign, but I guess I can wait for another bout of inspiration to consider rollovers. =P
Check out Id’s last blog post: Maundy.
March 20th, 2008 at 12:36 pm
I’m a fan of the roll-over effect. Use it all the time. =) Great tutorial!
Check out Brandy’s last blog post: Dream Diary - Storm of the Century.
March 20th, 2008 at 1:40 pm
Thx for putting up this tutorial …!
This is what I looking for so long …!
Thx again … Try it later …
Check out Eric’s last blog post: Theme modified done
March 21st, 2008 at 12:09 pm
SilverIsle: Hey there thanks for the compliments! I’m sorry to hear that exams and late-night studying are wearing you out, but hang in there! All the best and good luck.
Id: Same here - I struggled with CSS rollovers for quite some time until I realised the technique of loading one single image but changing the background position between different states. I actually got this idea after looking at N.Design Studio’s CSS styling - I know it’s very sneaky of me, haha! Initially I still stick to the coordinate kind of background position, until I realised that using bottom left removes all the need to do so. Nice new theme you have over there, it’s actually very nice, even without any CSS rollovers!
Brandy: I can see that for your blog - I like it the way you use CSS rollovers without making them much of a distraction!
Eric: Thank you, I hope you find it useful!
March 21st, 2008 at 4:59 pm
hey teddY! The demo was really addictive.. i love it.. realllllllly