Table of contents
1.
Introduction
2.
What is a tooltip in HTML?
2.1.
Syntax of tooltip in HTML
3.
How to Add Tooltips in HTML?
3.1.
1. Top Position
3.2.
2. Right Position
3.3.
3. Left Position
3.4.
4. Bottom Position
4.
Steps for Creating an HTML Tooltip?
5.
How to Add Effects to Tooltips?
5.1.
CSS
6.
Examples of HTML Tooltip Attribute
6.1.
Example 1: Position of a tooltip with Top and Bottom value
6.2.
CSS
6.3.
Example 2: Using tooltip on links
6.4.
CSS
7.
Frequently Asked Questions
7.1.
What is the attribute for tooltip in HTML?
7.2.
How do I show tooltip in HTML table?
7.3.
How to set tooltip position in HTML?
7.4.
How to use tooltip in HTML button?
8.
Conclusion
Last Updated: Aug 7, 2024
Easy

How to create HTML Tooltip?

Author Gaurav Singh
0 upvote

Introduction

Hi Ninjas, in this blog we will discuss the HTML Tooltip and why it's essential to have one on your website. We will also see how to add the HTML tooltip through examples using real-life code. Any information generated when one touches the content on an HTML page by hovering the mouse over it is simply done using the HTML Tooltip.

HTML Tooltip

So this lets us understand the purpose of the HTML tooltip, different ways of adding HTML tooltips, creating basic tooltips with CSS, and then finally moving on to the advanced courses of adding HTML tooltips using JavaScript.

What is a tooltip in HTML?

A tooltip is a user interface component having text that is displayed when a user hovers their cursor over an element. Usually, a tooltip contains text that provides additional context, description, or instructions that users may want to know. Tooltips are great for bits of text that can be hidden to save space on the page but are accessible easily when users require them. 

Syntax of tooltip in HTML

While HTML itself doesn't provide a built-in tooltip functionality, you can achieve tooltips using a combination of HTML and CSS. Here's a breakdown of the general syntax:

1. HTML Structure:

  • Use an element (like <div>, <span>) to represent the element that triggers the tooltip on hover.
  • Assign a class name to this element for easy CSS targeting.

2. CSS Styles:

  • Define styles for the element with the class name (e.g., .tooltip). This might include hover behavior to show/hide the tooltip.
  • Create another style for a separate element (e.g., <span>) with a different class name (e.g., .tooltiptext) to style the actual tooltip content. This element is typically hidden by default and positioned strategically near the triggering element.

How to Add Tooltips in HTML?

While HTML itself doesn't have built-in tooltips, you can create them using HTML and CSS together. Here's a breakdown of the steps:

1. HTML Structure:

  • Trigger Element: Use an element like <div>, <span>, or even an <img> to represent the element that will trigger the tooltip on hover.
  • Class Name: Assign a class name to this element for easy targeting with CSS. This class name will be used to define the hover behavior and basic appearance.

Example:

<div class="tooltip">Hover me for a tooltip!</div>

2. CSS Styles:

  • Trigger Element Styles: Define styles for the element with the class name you assigned (e.g., .tooltip). This might include a slight visual change on hover to indicate interactivity (optional).
  • Tooltip Content: Create styles for a separate element (e.g., <span>) with a different class name (e.g., .tooltiptext) to style the actual tooltip content. This element will typically be hidden by default and positioned strategically near the triggering element.

Example CSS:

.tooltip {
  /* Optional: Add a subtle hover effect */
  cursor: pointer;
}

.tooltiptext {
  visibility: hidden;
  position: absolute;
  background-color: black;
  color: white;
  padding: 5px;
  border-radius: 6px; /* Optional: Add rounded corners */
}

.tooltip:hover .tooltiptext {
  visibility: visible;
  /* Position the tooltip based on your needs (see below) */
}

Now, let's explore how to position the tooltip in different directions:

1. Top Position

To display the tooltip above the trigger element:

.tooltiptext {
  /* ... existing styles */
  top: -5px; /* Position above the element */
  left: 50%; /* Center horizontally */
  transform: translateX(-50%); /* Center horizontally */
}

2. Right Position

To display the tooltip to the right of the trigger element:

.tooltiptext {
  /* ... existing styles */
  top: 50%; /* Center vertically */
  left: 110%; /* Position to the right */
  transform: translateY(-50%); /* Center vertically */
}

3. Left Position

To display the tooltip to the left of the trigger element:

.tooltiptext {
  /* ... existing styles */
  top: 50%; /* Center vertically */
  left: -100%; /* Position to the left */
  transform: translateY(-50%); /* Center vertically */
}

4. Bottom Position

To display the tooltip below the trigger element:

.tooltiptext {
  /* ... existing styles */
  top: 100%; /* Position below the element */
  left: 50%; /* Center horizontally */
  transform: translateX(-50%); /* Center horizontally */
}

By combining HTML structure and CSS styles, you can create tooltips in various positions to enhance the user experience on your web pages.

Steps for Creating an HTML Tooltip?

For creating an HTML Tooltip follow the mentioned steps below:

  • Step 1: To make a simple tooltip, create an HTML ‘div’ with class ‘hover-text’ for triggering the tooltip when hovered over it.
  • Step 2: Next, we have to create a span element with the class ‘tooltip-text’ for the tooltip. Then place this element inside the parent 'div' with the ‘hover-text’ class.
  • Step 3: At last, we have to include CSS to add behavior to the tooltip.
    1. For hiding the “tooltip-text” class, the visibility 
          is set to hidden.
    2. The z-index is set as 1 for placing it above any other page content.
    3. The hover pseudo-class of class hover-text shows the tooltip text when the mouse hovers over it. 

How to Add Effects to Tooltips?

For adding effects to improve our tooltips, we can add options such as bounce, fade, or grow effect, color change, delay in appearance, etc.

This example will show how we can change the border-radius and fade a tooltip with the help of transition. For fading, a tooltip initially set the CSS opacity property to 0 to make the element invisible. For seeing a transition in the border radius and value. Add duration to both the properties to see a smooth effect.

Code

HTML

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="styles.css">
   <title>Centered Tooltip with Arrow</title>
</head>
<body>
   <div class="centered-container">
       <div class="tooltip-container">
           <div class="hover-text">Top
               <span class="tooltip-text top">Tooltip Text</span>
           </div>
       </div>
   </div>
</body>
</html>

CSS

  • CSS

CSS

body {
display: flex;
justify-content: center;
align-items: center;
height: 100vh;
margin: 0;
}

.centered-container {
text-align: center;
}

.tooltip-container {
position: relative;
display: inline-block;
cursor: pointer;
}

.tooltip-text {
display: block;
background-color: #333;
color: #fff;
padding: 8px;
z-index: 1;
white-space: nowrap;
opacity: 0;
border-radius: 0;
transition: opacity 3s, border-radius 5s;
}

.tooltip-container:hover .tooltip-text {
opacity: 1;
border-radius: 50%;
}

.tooltip-text::before {
content: '';
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}

 

Output:

output

Examples of HTML Tooltip Attribute

Example 1: Position of a tooltip with Top and Bottom value

Code

HTML

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="styles.css">
   <title>Centered Tooltip with Arrow</title>
</head>
<body>
   <div class="hover-text">Top
       <span class="tooltip-text top">Tooltip Text</span>
   </div>
   <br>
   <div class="hover-text">Bottom
       <span class="tooltip-text bottom">Tooltip Text</span>
   </div>
</body>
</html>

CSS

  • CSS

CSS

.hover-text {
position: relative;
display: inline-block;
margin: 40px;
text-align: center;
}

.hover-text:hover .tooltip-text {
visibility: visible;
}

.tooltip-text {
visibility: hidden;
position: absolute;
z-index: 1;
width: 100px;
color: white;
background-color: #192733;
padding: 5px;
}

.top {
z-index: 0;
bottom: 110%;
left: 0;
right: 0;
margin: 0 auto;
}

.bottom {
z-index: 1;
top: 110%;
left: 0;
right: 0;
margin: 0 auto;
}

Output

output1
output2

Example 2: Using tooltip on links

Code 

HTML

<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="UTF-8">
   <meta name="viewport" content="width=device-width, initial-scale=1.0">
   <link rel="stylesheet" href="styles.css">
   <title>Centered Tooltip with Arrow</title>
</head>
<body>
   <a href="https://www.scaler.com/topics/" title="To empower You.">
       Coding Ninjas Studio</a>
   

</body>
</html>

CSS

  • CSS

CSS

.hover-text {
position: relative;
display: inline-block;
margin: 40px;
text-align: center;
}

.hover-text:hover .tooltip-text {
visibility: visible;
}

.tooltip-text {
visibility: hidden;
position: absolute;
z-index: 1;
width: 100px;
color: white;
background-color: #192733;
padding: 5px;
}

.top {
z-index: 0;
bottom: 110%;
left: 0;
right: 0;
margin: 0 auto;
}

.bottom {
z-index: 1;
top: 110%;
left: 0;
right: 0;
margin: 0 auto;
}

 

Output

output

Frequently Asked Questions

What is the attribute for tooltip in HTML?

HTML itself doesn't have a built-in tooltip attribute. You can achieve tooltips using a combination of HTML and CSS.

How do I show tooltip in HTML table?

For tooltips in HTML tables, use the same approach as other elements. Add a class to your table cells (e.g., <td>) and style the tooltip content with CSS.

How to set tooltip position in HTML?

Tooltip position is controlled with CSS. You can style the tooltip element with classes to appear above, below, left, or right of the trigger element.

How to use tooltip in HTML button?

Similar to other elements, assign a class to your button element and style the tooltip content with CSS. The button can also trigger the hover behavior.

Conclusion

In this blog, we have seen how HTML tooltips help one have a more interactive website and also keep the website clean and attractive. It also helps make space for other content, since the tooltip content is only visible when the mouse is hovered over the HTML element.

We can add images as well as videos and also make it more interactive by using JavaScript or JQuery or also even simple CSS would suffice. For more information related to attributes in html, bootstrap tooltip, etc. check out the following articles - 

Live masterclass