Simple Tooltip Using HTML and CSS

Introduction

 
A tooltip is a pop-up message that is shown when the user hovers the mouse over an element such as a TextBox or Button etcetera. 
 
In this article, I show you how to create a simple tooltip using HTML and CSS.
 
Observe the following table. Here we have placed the Input (Text) controls (txtUsername, txtPassword) inside the anchor tag. We have added a class name (tooltip) to the anchor tag and given some text to the alt attribute. Using an alt attribute we will display a tooltip for the Input (Text) controls.
  1. <html>  
  2. <head>  
  3. </head>  
  4. <body>  
  5.     <table style="width: 270px;">  
  6.         <tr>  
  7.             <td>  
  8.                 Username:  
  9.             </td>  
  10.             <td>  
  11.                 <a href="#" alt="Please enter username" class="tooltip">  
  12.                     <input id="txtUsername" type="text" /></a>  
  13.             </td>  
  14.         </tr>  
  15.         <tr>  
  16.             <td>  
  17.                 Password:  
  18.             </td>  
  19.             <td>  
  20.                 <a href="#" alt="Please enter password" class="tooltip">  
  21.                     <input id="txtPassword" type="text" /></a>  
  22.             </td>  
  23.         </tr>  
  24.         <tr>  
  25.             <td>  
  26.             </td>  
  27.             <td>  
  28.                 <input id="Button1" type="button" value="Login" />  
  29.             </td>  
  30.         </tr>  
  31.     </table>  
  32. </body>  
  33. </html> 
Now our the page looks in the browser as follows
 
1.png
 
Now we will add some CSS style to the anchor tags; see:
  1. .tooltip  
  2. {  
  3.             display: inline;  
  4.             position: relative;  
  5.             text-decoration: none;  
  6.             top: 0px;  
  7.             left: 4px;  
Now by using the anchor tags, we will display the tooltip for the Input (Text) controls.
 
Observe the following style of the anchor tags:
  1. .tooltip:hover:after  
  2. {  
  3.        background: #333;  
  4.         background: rgba(0,0,0,.8);  
  5.         border-radius: 5px;  
  6.         top: -5px;  
  7.         color: #fff;  
  8.         content: attr(alt);  
  9.         left: 160px;  
  10.         padding: 5px 15px;  
  11.         position: absolute;  
  12.         z-index: 98;  
  13.         width: 150px;  
  14. }  
Here we are providing a style whenever the user hovers the mouse over the anchor tags.
 
We are specifying some style using the: after selector.
 
In the preceding style, we have given a border and border-radius etcetera.
 
Here we have set the content attribute to content: attr(alt); this property will display the tooltip by using the alt attribute of the anchor tag.
 
Whatever you give to the alt tag of the anchor element it will be displayed as a tooltip.
 
Now we will add one arrow to the tooltip as follows using the: before selector:
  1. .tooltip:hover:before  
  2. {  
  3.       border: solid;  
  4.       border-color: transparent black;  
  5.       border-width: 6px 6px 6px 0;  
  6.       bottom: 20px;  
  7.       content: "";  
  8.        left: 155px;  
  9.        position: absolute;  
  10.        z-index: 99;  
  11.        top: 3px;  
  12. }  
Finally, the script of our page is:
  1. <html>  
  2. <head>  
  3.     <style type="text/css">  
  4.         .tooltip  
  5.         {  
  6.             display: inline;  
  7.             position: relative;  
  8.             text-decoration: none;  
  9.             top: 0px;  
  10.             left: 4px;  
  11.         }  
  12.         .tooltip:hover:after  
  13.         {  
  14.             background: #333;  
  15.             background: rgba(0,0,0,.8);  
  16.             border-radius: 5px;  
  17.             top: -5px;  
  18.             color: #fff;  
  19.             content: attr(alt);  
  20.             left: 160px;  
  21.             padding: 5px 15px;  
  22.             position: absolute;  
  23.             z-index: 98;  
  24.             width: 150px;  
  25.         }  
  26.         .tooltip:hover:before  
  27.         {  
  28.             border: solid;  
  29.             border-color: transparent black;  
  30.             border-width: 6px 6px 6px 0;  
  31.             bottom: 20px;  
  32.             content: "";  
  33.             left: 155px;  
  34.             position: absolute;  
  35.             z-index: 99;  
  36.             top: 3px;  
  37.         }  
  38.     </style>  
  39. </head>  
  40. <body>  
  41.     <table style="width: 270px;">  
  42.         <tr>  
  43.             <td>  
  44.                 Username:  
  45.             </td>  
  46.             <td>  
  47.                 <a href="#" alt="Please enter username" class="tooltip">  
  48.                     <input id="txtUsername" type="text" /></a>  
  49.             </td>  
  50.         </tr>  
  51.         <tr>  
  52.             <td>  
  53.                 Password:  
  54.             </td>  
  55.             <td>  
  56.                 <a href="#" alt="Please enter password" class="tooltip">  
  57.                     <input id="txtPassword" type="text" /></a>  
  58.             </td>  
  59.         </tr>  
  60.         <tr>  
  61.             <td>  
  62.             </td>  
  63.             <td>  
  64.                 <input id="Button1" type="button" value="Login" />  
  65.             </td>  
  66.         </tr>  
  67.     </table>  
  68. </body>  
  69. </html> 
Save this script as .html file or download the attachment and open in the browser then mouse over on to the username and password Textboxes we will get the tooltip as follows. 
 
Mouseover on to the Username Textbox.
 
2.png
 
Mouseover on to the Password Textbox
 
3.png
 
So this is the simple way to display the tooltip using HTML and CSS.
 
We can use this tooltips procedure for images also.
 
If you have any confusion please download the attachment and check.


Similar Articles