CSS Swap Hover Effect

It was requested that the product photo be swapped by its product description when the mouse hovers over it. This kind of effect is very interesting and easy to achieve, so I would like to share it with you.

 

Markup

To get into this effect, every project should have two pieces of information. The first piece contains a project thumbnail that will be displayed by default (in normal state). The second piece contains the text/description and this piece of project description should replace the thumbnail when it’s in hover state.

The markup will look like this


Social Developement Direct

SDDirect Website Development

Design and devevelopment the website and custom CMS for SDDirect

As you see the code above, the thumbnail is wrapped in the , and the project description (the second piece) is put in the

. There will be more than one project and since they have same structure, the ideal way to display them all is using unordered list. That means that every list item will hold a project detail.

We’ve done the markup. Now let’s move to the styling part and add some rock ’n’ roll.

/* ---- styling the list ---- */

#work {
list-style:none;
margin:0;
padding:0;
}
#work li {
float:left;
display:block;
width:235px;
margin:10px;
display:inline;
padding:3px;
background:#fff;
border:1px solid #cad789;
height:154px;
}
#work li a {
border:none;
}
#work p   {
margin-bottom:0;
}

The code is quite simple, isn’t it? I simply float all the items of the unordered list #work to the left, so every item is a 255px x 1574px box, with 10px gutter (margin: 10px). Now let’s add some CSS magic to achieve the effect we want.

Styling and adding the CSS effect

#work li .show{
display:block;
width:235px;
height:154px;
}
#work li .hide {
color:#d4df9d;
text-align: left;
height: 0;
overflow: hidden;
background:#687b00;
}

What is the code above about? I use the properties height:0; and overflow: hidden; to ensure that the elements assigned with the class hide should be invisible and so only the elements with class name .show appear. Then I add some following CSS code to show the .hide element and hide the .show element when hovering over each li.

#work li:hover .hide{
cursor: pointer;
height: 133px;
padding:10px;
width:215px;
}
#work li:hover .show {
height: 0;
overflow: hidden;
}

I use the active pseudo-class #work li:hover to set some properties for the element .hide every time the mouse hovers on the list item of unordered list work. I simply set the height of the .hide to 153px, then hide .show element by adding overflow: hidden; and the property height:0; to change its height to zero.

 

Works fine, but how about IE 6?

The effect acts nicely in Firefox, Safari, IE7 but not IE6. Why? Because IE6 only supports :hover for element, which means #work li:hover would be skiped. We will need some fix to make it perform as well as expected on IE6 and that fix should be this JQuery-based javascript to hook IE6:

$(function() {
$('#work li').mouseover
(function(){
$(this).addClass('over');
});
$('#work li&').mouseout
(function(){
$(this).removeClass('over');
});

});

In the script above, simply add a class over into every list item of #work if the mouse moves over it. I also wrap it in the conditional comment to ensure that the script only executes in IE 6 or older IE. What we need to do is adding the properties for #work li.over .hide to make it work in the same way as #work li:hover .hide . So the CSS part should be changed like this:

#work li:hover .hide, #work li.over .hide{
cursor: pointer;
height: 133px;
padding:10px;
width:215px;
}
#work li:hover .show, #work li.over .show  {
height: 0;
overflow: hidden;
}

Now everything seems to be solved.

Alternative solution for IE 6

There is an alternative solution to solve the :hover issue in IE6 by using this behavior script whatever:hover. If you want to get rid of javascript, this fix is very easy to use and sure to work perfectly, you only need to add the following code into your markup

#work li { behavior: url(hover.htc); }

Download: http://www.htmldrive.net/items/download/502

Leave a comment