Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pure CSS Image Hover Without Using Background Images

Tags:

html

css

I'm trying to make a pure CSS image change on hover / rollover without using background images. So far I have one image and when you rollover that image, another image appears. Here is the CSS:

#hidden {
display: none;
}

#visible:hover + #hidden {
display: block;
}

So, when you rollover the #visible div, the #hidden div appears. Here is the jsFiddle: http://jsfiddle.net/MNyzd/1/

This works great, but it is not exactly what I want to accomplish. I would like the images to swap. So when you rollover #visible, it should disappear instead of remaining visible. My first initial idea was to make the #visible div to display:none on hover (#visible:hover display:none;), but this did not work.

So does anyone have any idea how I would successfully turn this into a traditional image hover / swap using this method? Any help would be appreciated and again, here is the jsFiddle... http://jsfiddle.net/MNyzd/1/

like image 748
Dyck Avatar asked Aug 09 '13 18:08

Dyck


2 Answers

Use a container where you do the hover on:

http://jsfiddle.net/MNyzd/8/

.hidden {
    display: none;
}

.container:hover .visible
{
    display: none;
}

.container:hover .hidden {
    display: block;
}

See also this answer: Hide / show content via CSS:hover (or JS if need be)

like image 164
Ikke Avatar answered Nov 19 '22 11:11

Ikke


Like this?

jsFiddle

div {
    cursor:pointer;
    overflow:hidden;
    width:300px;
}
div > img:nth-child(2), div:hover > img:nth-child(1) {
    display:none;
}
div:hover > img:nth-child(2) {
    display:block;
}
like image 44
Shivam Avatar answered Nov 19 '22 10:11

Shivam