CSS rollover image
January 3rd, 2006 by Sonja DuijvesteijnImages with a rollover effect are easy to make with the use of javascripts onRollOver function. However, it doesn’t work with javascript disabled. Luckily this is also possible with CSS.
My favorite way to do this works with an image that contains both the rollover and the normal state of the image, thus making it twice as long.
div.imgcontainer {
width: 50em;
height : 25em;
overflow : hidden;
text-align: left;
}
div.imgcontainer:hover {
text-align: right;
}
<div class='imgcontainer'>
<img src='images/image.jpg'/>
</div>
Note that the image container is the width of a single image, while the actual image is double that. When you use ‘overflow: hidden; everything that falls outside of the div will be clipped. Thus showing only the left half of the image. When you hover over the image the alignment of the div changes, showing only the right half of the image.
Another way to make it work
Another way to do this is by putting an image as a background for the div, and a different one for the :hover pseudo class.
div.imgcontainer {
width: 50px;
height : 25px;
overflow : hidden;
background-image: url('/images/image.jpg');
}
div.imgcontainer:hover {
background-image: url('/images/imageon.jpg');
}
<div class='imgcontainer'>
 
</div>
This works the same, but it has a downside, the image still has to be loaded once the the mouse is put over the image, making it flicker for a moment. This can of course be solved with preloading in javascript, but were just trying to get rid of javascript.
And a third way
div.imgcontainer {
width: 50em;
height : 25em;
background-image : url('/images/image.jpg');
}
div.imgcontainer img:hover {
visibility : hidden;
}
<div class='imgcontainer'>
<img src='images/image.jpg'/>
</div>
This example has a regular image, and it has a background-image which will be hidden by the picture. When you hover over the image tag, inside the div the image is made invisible, showing the background image.
Again, there’s a downside to this method, a background-image is always the size the original image is. While an image from an img tag can be resized just like text, when you set width and height in em. So, this method gives a resizeable normal image, and a fixed size mouseover image.
Take you pick, no more excuses for javascript rollovers.
Related posts
Font-sizes in css: em ex px, mm cm in pt pc, %
CSS image preloading
Cool use of css
Favicons
HTML and XHTML, which to use?
August 25th, 2006 at 4:09
Hi.
Tried to get your 1st example of CSS rollover images to work, but for some reason I can’t get a mouseover (doesn’t change to a hand) to change the image. The 3rd example worked but had some buggy flipping back and forth that wigged out. Am I trippin out on something here?:
div.imgcontainer {
width: 95px;
height : 97px;
overflow: hidden;
text-align: left;
}
div.imgcontainer img:hover {
text-align: right;
}
September 30th, 2009 at 4:44
Greetings all…
So after nearly 5 years of focusing on the finance sector and really enjoying every minute of it…the time has come to expand my horizons and take the next step in delivering great products for Microsoft. So as of very soon (date TBD) I will be moving to a Program Management role in a new product being developed in the Office division.
April 12th, 2010 at 2:45
A crude image or effigy of a person set up in a field to scare birds away from growing crops.
May 1st, 2010 at 12:54
Great, http://technorati.com/people/pokemonporn Real pokemon porn, uqdnw,
June 28th, 2010 at 6:12
ou
http://002evolves.blogspot.com