Home  >  Article  >  Web Front-end  >  How Can I Display Text on Image Hover Without Using Tooltips?

How Can I Display Text on Image Hover Without Using Tooltips?

Linda Hamilton
Linda HamiltonOriginal
2024-11-08 08:46:02385browse

How Can I Display Text on Image Hover Without Using Tooltips?

Displaying Text on Image Hover Without Tooltips

In web design, it's often desirable to display additional information when the user hovers over an image. While tooltips are a popular solution, they may not always provide the desired aesthetic or functionality. This article explores how to achieve this effect using pure CSS or jQuery.

Using CSS:

CSS3 introduces the :hover pseudoelement, which allows for styling when the mouse hovers over an element. In this case, the hover effect will be applied to the image.

HTML:

<div>

CSS:

#wrapper .text {
  position: relative;
  bottom: 30px;
  left: 0px;
  visibility: hidden;
}

#wrapper:hover .text {
  visibility: visible;
}

This CSS places the text caption at the bottom-left corner of the image and hides it until the mouse hovers over the image.

Using jQuery:

jQuery can also be used to achieve the same effect. This method may provide more flexibility for more complex scenarios.

HTML:

Same as before.

CSS:

#wrapper p {
  position: relative;
  bottom: 30px;
  left: 0px;
  visibility: hidden;
}

jQuery:

$('.hover').mouseover(function() {
  $('.text').css("visibility", "visible");
});

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

This jQuery script binds mouseover and mouseout events to the image element and toggles the visibility of the caption.

Regardless of the method used, by utilizing CSS or jQuery, it's possible to create a custom hover effect that displays additional information on an image without the use of tooltips.

The above is the detailed content of How Can I Display Text on Image Hover Without Using Tooltips?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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