简体   繁体   中英

Image displays text on hover

I have an image, and I style it in CSS. I need the image to display a caption when the cursor hovers over it. However, something like:

#image:hover {
  content:'foo';
}

won't work because the image is not <p> , right? Is there any jQuery solution to this?

If you give them a common parent, you can wrap the image with the parent, hide the text, then show the text relative to the parent's position when you hover the parent.

 figure { display: inline-block; position: relative; } figcaption { opacity: 0; transition: opacity .25s, transform .25s; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%) rotate(0); color: #fff; font-size: 3em; background: rgba(0,0,0,0.8); padding: 1rem; } figure:hover figcaption { opacity: 1; transform: translate(-50%,-50%) rotate(720deg); } 
 <figure> <img src="http://kenwheeler.github.io/slick/img/fonz1.png"> <figcaption>Text</figcaption> </figure> 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM