How to Display Text on Image Hover Using CSS and jQuery?

Linda Hamilton
Release: 2024-11-07 07:00:03
Original
901 people have browsed it

How to Display Text on Image Hover Using CSS and jQuery?

Displaying Text on Image Hover

Creating a small box that appears when a user hovers over an image can be achieved through various methods. Here's a comprehensive guide to help you achieve the desired effect:

CSS3 Using :hover Pseudoelement

By utilizing the :hover pseudoelement in CSS3, you can display a box on image hover. The HTML and CSS code for this method are as follows:

<div>
Copy after login
Copy after login
#wrapper .text {
  position: relative;
  bottom: 30px;
  left: 0px;
  visibility: hidden;
}

#wrapper:hover .text {
  visibility: visible;
}
Copy after login

jQuery

Alternatively, you can use jQuery to achieve the same result. Here's the HTML, CSS, and jQuery code:

<div>
Copy after login
Copy after login
#wrapper p {
  position: relative;
  bottom: 30px;
  left: 0px;
  visibility: hidden;
}
Copy after login
$('.hover').mouseover(function() {
  $('.text').css("visibility", "visible");
});

$('.hover').mouseout(function() {
  $('.text').css("visibility", "hidden");
});
Copy after login

This jQuery code will show the "text" element when the mouse hovers over the image and hide it when the mouse leaves.

Customizing the Box

To customize the size and position of the box, you can adjust the CSS values provided in the bottom: and left: properties. To remove the border connection to the image, add the following to your CSS:

#wrapper img {
  border: none;
}
Copy after login

Multiple Images and Captions

If you have multiple images and captions, you can create a wrapper div for each image and include the image and text elements within. Simply copy the format shown in the code snippet and replace the image source and text accordingly.

The above is the detailed content of How to Display Text on Image Hover Using CSS and jQuery?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template