How to display font on mouseover of image?

WBOY
Release: 2023-09-13 08:09:02
forward
2898 people have browsed it

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
Copy after login

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>
Copy after login

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>
Copy after login

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>
Copy after login

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!

Related labels:
source:tutorialspoint.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template