Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Css hover on image - load a div

Tags:

html

css

I would like to do a CSS effect on hovering an image.

I'd like to show a div containing content like text when I hover on an image.

So I'd like to do something with this code:

<div id="image"><img src="image.png"/></div>

<div id="hover">Test message</div>

I have tried to hide the "hover" div in css display and on hover I tried to set the display to block, but its not working...:(

EDIT: I want the text on the image. When I hover the image it should get some opacity BUT the text should not recieve that opacity!

IS there any method to do this?

like image 438
Taboo Loco Avatar asked Mar 11 '13 12:03

Taboo Loco


People also ask

Can I use hover on image CSS?

Pure CSS to swap image on hoverThe best way to swap images on hover is to place both images in the same container, with the "rollover" image transparent by default. When the user hovers over the container, the "rollover" image becomes opaque. It is fast loading, easy to implement and works on all browsers.

How do you make a div appear on hover?

To display div element using CSS on hover a tag: First, set the div element invisible i.e display:none;. By using the adjacent sibling selector and hover on a tag to display the div element.

How do you hover an image in CSS?

Answer: Use the CSS background-image property You can simply use the CSS background-image property in combination with the :hover pseudo-class to replace or change the image on mouseover.

Can a div have hover?

You can attach styles to a div by using the :hover keyword.


1 Answers

http://jsfiddle.net/ZSZQK/

#hover {
    display: none;
}

#image:hover + #hover {
    display: block;
}

Just keep it simple, no need to change your markup.


Update

If you want to change opacity of image on mouse hover, then http://jsfiddle.net/ZSZQK/4/

#hover {
    display: none;
    position: relative;
    top: -25px;
}

#image:hover {
    opacity: .7;
}

#image:hover + #hover {
    display: block;
}

Update 2

Since you added a couple of more requirements to your initial question, now it requires a change in the original html markup.

I am assuming you are using html5, and if so, you should use the tags appropriated for your content's context:

<figure>
    <img src="http://placekitten.com/200/100" />
    <figcaption>Test message</figcaption>
</figure>

and the css

figure {
    position: relative;
    display: inline-block;
}

figcaption {
    display: none;
    position: absolute;
    left: 0;
    bottom: 5px;
    right: 0;
    background-color: rgba(0,0,0,.15);
}

figure:hover img {
    opacity: .7;
}

figure:hover figcaption {
    display: block;
}

jsFiddle

like image 148
Jose Rui Santos Avatar answered Sep 20 '22 18:09

Jose Rui Santos