Home >Web Front-end >HTML Tutorial >How to display font on mouseover of image?

How to display font on mouseover of image?

WBOY
WBOYforward
2023-09-13 08:09:022941browse

How to display font on mouseover of image?

The task we are going to perform in this article is how to display fonts when hovering the mouse over an image. Let’s dive into this article and take a quick look at hover and mouseover in HTML.

The onmouseover event in HTML is triggered when the mouse pointer touches an element. When the mouse pointer leaves an element, an event called onmouseout occurs.

The :hover CSS pseudo-class matches when the user interacts with an element using a pointing device, but it is not always activated. Normally it is activated when the user hovers the cursor over the element (mouse pointer).

grammar

The following is the syntax of hover -

:hover

To better understand displaying fonts when hovering over an image, let's look at the following example.

Example

In the example below, we make the font visible when the mouse is hovered over the image.

<!DOCTYPE html>
<html>
   <body>
      <style>
      .img {
         position: relative;
         width: 200px;
         height: 200px;
         float: left;
         margin-right: 10px;
      }
      .img div{
         position: absolute;
         bottom: 0;
         right: 0;
         background: black;
         color: white;
         margin-bottom: 5px;
         visibility: hidden;
      }
      .img:hover{
         cursor: pointer;
      }
      .img:hover div{
         width: 150px;
         padding: 8px 15px;
         visibility: visible;
         opacity: 0.7;
      }
      </style>
      <div class="img">
         <img  src='https://www.tutorialspoint.com/html/images/html-mini-logo.jpg'    style="max-width:90%"  style="max-width:90%" alt="How to display font on mouseover of image?" >
         <div>TP HTML LOGO</div>
      </div>
      <div class="img">
         <img  src='https://www.tutorialspoint.com/java/images/java-mini-logo.jpg'    style="max-width:90%"  style="max-width:90%" alt="How to display font on mouseover of image?" >
         <div>TP JAVA LOGO</div>
      </div>
   </body>
</html>

When the script executes, it will generate an output that displays the image on the web page. If the user hovers over the image, it will display a text description of that specific image on the page.

Example

In the example below, we make the font appear when the mouse is hovered over the image, and cover the entire image.

<!DOCTYPE html>
<html>
   <body>
      <style>
      .tutorial {
         position: relative;
         max-width: 500px;
      }
      .tutorial img { width: 100%; }
      .fulltext {
         box-sizing: border-box;
         width: 100%;
         height: 100%;
         position: absolute;
         top: 0; left: 0;
         text-align: center;
         padding-top: 30%;
         background-color: #EAFAF1 ;
         color: black;
      }
      .fulltext {
         visibility: none; opacity: 0;
         transition: opacity 0.3s;
      }
      .tutorial:hover .fulltext {
      visibility: visible; opacity: 1;
      }
      </style>
      <div class="tutorial">
         <img  src=https://www.tutorialspoint.com/java/images/java-mini-logo.jpg alt="How to display font on mouseover of image?" >
         <div class="fulltext">
            LEARN JAVA...!<br>
         </div>
      </div>
   </body>
</html>

When you run the above script, an output window will pop up to display the image on the web page. If the user moves the mouse over the image, it will display text covering the entire image.

Example

Execute the following code and observe how the font appears when you hover the mouse over the image.

<!DOCTYPE html>
<html>
   <body>
      <style>
      div {
         position: relative;
         top: 0px;
         left: 0px;
         width: 400px;
         height: 400px;
         border-radius: 50%;
         overflow: hidden;
         text-align: center
      }
      img {
         width: 400px;
         height: 400px;
         position: absolute;
         border-radius: 50%
      }
      img:hover {
         opacity: 0;
         transition:opacity 2s;
      }
      heading {
         line-height: 40px;
         font-weight: bold;
         text-align: center;
         position: absolute;
         display: block
      }
      div p {
         width: 420px;
         line-height: 20px;
         display: inline-block;
         padding: 200px 0px;
         vertical-align: middle;
         height: 450px
      }
      </style>
      <div>
         <img  src="https://www.tutorialspoint.com/static/images/logo-color.png" alt="How to display font on mouseover of image?" >
         <p>Welcome to Tutorialspoint</p>
      </div>
   </body>
</html>

When the script is executed, the output window will pop up, making the image appear as a circle on the web page. When the user hovers over the image, it will display the text.

The above is the detailed content of How to display font on mouseover of image?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete