CSS Image Rollovers without Javascript

What we want to create.

Web Designers use a few different ways to create rollover images, usually for buttons. The most popular techniques use javascript to preload the hover image or ‘over state’ however pure CSS is a better more efficient technique. Alot of people however only get it half right. 

a{
background:(my-image.jpg);
}
 

a:hover{
background:(my-image2.jpg);
}

The problem here is that when the image is rolled over the second image still needs to be downloaded from the server. The user will usually experience an unsightly delay between the rollover action and the appearance of the replacement image. Visit hosting.digiweb.ie for an example.

Why people use javascript is to preload the second image in the browser so when its called it instantly appears. CSS alone cant preload a second image from the server but there is a clever work around.

 

 

 

 

 

Above we have our 2 images. The left one is the link with the right one being the the rollover. We need these two images loaded with the page but using CSS alone this is not possible. Each image is 240px by 160px.

To get around this we create an image 240px wide by 320px high. Twice the size of the original and place toe 2 images, one on top of the other inside it creating the image below.

null

So now for the HTML. Its fairly simple, just a basic link.

<a href="portrait.html" title="portrait">protrait</a>

CSS Bit.
First we need to define the links style. We’ll set the width & height to 240 x 160 and the background as image1.jpg

a{
background:url(my-image.jpg);
width:240px;
height:160px;
}

This leaves us with a text link with a small piece of the imahe as the background. The width & height atributes are not being used. To activate these we need to add display:block

a{
background:url(my-image.jpg);
width:240px;
height:160px;
display:block
}

Next we need to remove or hide the ‘Portraits’ text. To do this we add line-height:999px

This moves the text 999px down the screen. The problem now is that the text is displayed way down the screen. to hide this we need to add another piece of code: overflow:hidden. This hides everything thats outside the 240px by 160px box.

a#portraits{
background:url(my-image.jpg);
width:240px;
height:160px;
display:block;
line-height:999px;
overflow:hidden;
}

Next we need to specify the rollover. What we do is position the background image 160px higher when the mouse is over the link. We can do this with one line of code.

a:hover{
background:url(my-image.jpg) bottom
}

And there you go. View the finished link here.

Here’s the code:
CSS
a{
background:url(my-image.jpg);
width:240px;
height:160px;
display:block;
line-height:999px;
overflow:hidden;
}

 

a:hover{
background:url(my-image.jpg) bottom;
}

HTML
<a id="portraits" title="portraits" href="#">Portraits</a>;

View the finished link here.

3 thoughts on “CSS Image Rollovers without Javascript

  1. [...] is the original: CSS Image Rollovers without Javascript | Conor Darcy Dundalk Web … .myForm td, input, select, textarea, checkbox { font-family: tahoma; font-size: 12px; } [...]

  2. Well, but I still prefer the jQuery way (http://plugins.jquery.com/project/SwapImage)

    The markup is more simple and clean to use.

  3. What an excellent blog, I’ve added your feed to my RSS reader. :-)

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>