About Us

Bulletpoint StarImulus® is a technology focused design + interactive agency.

In addition to the services we provide our clients we also have several products in the works. Our office is always filled with chatter and this blog is an outlet for some of our creative energy, rants and ideas.

Previous Posts

Imulus Twitters

Featured Project

Jun12

Browser compliant reusable image rollovers with CSS

Months ago when working on development for RMI | BET’s video website I needed a way to do image rollovers but I didn’t want to have a reliance on javascript. So, I set out to a create a reusable image rollover solution that was completely CSS based. Basically the concept was to have a universal class that could be called at any time in the HTML to create a rollover image.

Since first creating this code I’ve integrated it as a standard into all of our starting CSS templates.

Here’s how this works:

Start out with a basic anchor tag and give it a class of “hover-box”. Then inside the anchor tag add both images, your original and your hover version. Place the hover image first in the tag, and then add the original one second. Add a class of “hover” to the hover image. It should look like this:

<a href="#" title="Link title..." class="hover-box">
   <img src="rollover-image.jpg" class="hover" alt="Rollover"/>
   <img src="original-image.jpg" class="original" alt="Original"/>
</a>

Now, make sure this CSS is included in your document.

a.hover-box img.original { /* required for IE6 */
    float: left;
    position: relative;
}
a.hover-box img.hover {
    display: none;
}
a.hover-box {
    position: relative;
    float: left;
    display: block;
}
a.hover-box:hover {
    display: block;
    font-size: 100%;
    z-index: 1;
}
a.hover-box:hover img.hover {
    display: block;
    position: absolute;
    left: 0;
    top: 0;
    z-index: 1000;
}

Okay, basically this is hiding the hover image to begin. Then, once the anchor tag is hovered over the image with class “hover” displays itself as block and is positioned absolutely directly on top of the original image. This is accomplished because the hover-box class is set with relative positioning, meaning that the hover image is tied to its boundaries.

RolloverOriginalBang! There you have it, a simple CSS hack-free solution that works in: Firefox, Safari, IE 6 & 7, and presumably all other standards compliant browsers. Check it out in action…

P.S. This is especially sweet because the image is preloaded and then hidden, meaning the hover effect is instant for the viewer.

Edit: After some testing I found out my original version wasn’t always working in IE6. It seems that for some reason IE6 needs a class applied to the original image that floats it and positions it relative. I tried every possible way and this is the only thing making it work without a * hack. Not a huge deal though. Also, IE6 requires the original image to come second in the HTML, unlike I originally thought.

posted in: CSS, concepts, web development, web standards

This post was published on Thursday, June 12, 2008 at 1:00 am

Leave a comment


Comments

1

trevor filter

June 12, 2008 at 9:29 am

Nice! Really clever solution.
So this is what they hired you for, huh?

2

Ryan

June 14, 2008 at 11:14 am

As I was saying on reddit before it went down;

The image is being used to style “text” so it should actually be real text in the HTML, then use CSS to hide it and apply background rollovers… MUCH cleaner.

However assuming it’s simply a demo it’s still not clean. Wrapping two images in an anchor to get a rollover effect - which makes little sense when CSS is disabled - is not what HTML is for. Use CSS or Javascript.

The before/after idea mentioned is almost acceptable, but the anchor around both images is negates the idea of semantic markup.

3

Bruce

June 20, 2008 at 11:30 am

Ryan,

All good points. The real problem with this is that IE requires the anchor tag to actually do the hover state effectively. As noted on the comments of the reddit posting, the new browsers (IE7,safari,ff) all support hover on non A elements and therefore it would work semantically. Even so, the trade off between using an anchor tag and using javascript is an easy decision, and one that makes sense semantically if the images need to be linked. In fact, if the images need to link to something (like they often do with our projects) this is the best markup solution I’ve seen.

Ideally you are right, if adding text on hover the best option would be to just include text and control if via styling. Still, I simply added those images to demonstrate the idea of what is being done. I suppose I could change them out for something more semantically appropriate, i.e. before and after images.

In the end though I have to say the technique is quite clean in comparison to other methods. It doesn’t require javascript for the end user to see it — which requires more work to implement, and having a global style that can be called to utilize this function speeds up projects. It’s as simple as that.

I take my coding pretty seriously. There are many instances that I think things in CSS/XHTML aren’t used properly, this is not one of them. One example of bad form would be Douglas Bowman’s use of inline background styling for his photo gallery: http://www.dbowman.com/photos/type/gallery/ . He uses a style to handle each image thumbnail, and then puts that within a list. Perhaps acceptable, but no ideal by any means. My point is this, even the CSS all-star coders bend things sometimes. Until we have HTML 5 and CSS3 in all modern browsers we have to make small sacrifices to achieve the most well rounded solution.