top of page

Just Jolly Functions

Functions: Adding a Tooltip with HTML, CSS, and JavaScript

A tooltip is a small, interactive box that appears when a user hovers over an element to provide additional information. Tooltips usually appear as small pop-up boxes with text, and sometimes include icons or arrows pointing to the element they describe. They are often used in user interfaces to improve accessibility and user experience by offering contextual information in a concise manner.



HTML, CSS, and JavaScript Combined Code for Tooltip

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tooltip Example</title>
  <style>
    /* Tooltip container */
    .tooltip-container {
      position: relative;
      display: inline-block;
      cursor: pointer;
    }

    /* Tooltip text */
    .tooltip-text {
      visibility: hidden;
      width: 120px;
      background-color: black;
      color: #fff;
      text-align: center;
      border-radius: 5px;
      padding: 5px;
      position: absolute;
      z-index: 1;
      bottom: 125%; /* Position above the text */
      left: 50%;
      margin-left: -60px; /* Offset the tooltip to be centered */
      opacity: 0;
      transition: opacity 0.3s;
    }

    /* Tooltip arrow */
    .tooltip-text::after {
      content: '';
      position: absolute;
      top: 100%;
      left: 50%;
      margin-left: -5px;
      border-width: 5px;
      border-style: solid;
      border-color: black transparent transparent transparent;
    }

    /* Show the tooltip when hovering */
    .tooltip-container:hover .tooltip-text {
      visibility: visible;
      opacity: 1;
    }
  </style>
</head>
<body>

  <div class="tooltip-container">
    Hover over me
    <div class="tooltip-text">This is a tooltip!</div>
  </div>

  <script>
    // Optionally, you can add JavaScript functionality for tooltips, but this basic version uses only CSS hover for the tooltip.
    // If you'd like to create a dynamic tooltip with JS, you could do something like this:

    /*
    document.querySelector('.tooltip-container').addEventListener('mouseover', function() {
      const tooltip = this.querySelector('.tooltip-text');
      tooltip.style.visibility = 'visible';
      tooltip.style.opacity = 1;
    });

    document.querySelector('.tooltip-container').addEventListener('mouseout', function() {
      const tooltip = this.querySelector('.tooltip-text');
      tooltip.style.visibility = 'hidden';
      tooltip.style.opacity = 0;
    });
    */
  </script>

</body>
</html>

How it Works:

  1. HTML:

    • The tooltip is wrapped inside a <div> with the class tooltip-container.

    • The text for the tooltip is inside another <div> with the class tooltip-text.

    • When you hover over the "Hover over me" text, the tooltip will appear.

  2. CSS:

    • tooltip-container: This is styled to display as an inline block and has a cursor pointer to show that it's interactable.

    • tooltip-text: This is the hidden tooltip that becomes visible on hover. It is styled to appear above the text with a dark background, white text, and rounded corners.

    • Tooltip Arrow: Using the ::after pseudo-element, a small triangle (arrow) is added pointing to the element the tooltip is describing.

    • Hover effect: The tooltip's visibility and opacity change when the .tooltip-container is hovered.

  3. JavaScript:

    • In this example, the tooltip functionality is implemented using CSS hover for simplicity. However, I’ve added commented JavaScript code at the bottom that would allow you to control the visibility of the tooltip programmatically by listening for mouseover and mouseout events.


Result:

  • When you hover over the "Hover over me" text, a tooltip appears above the text with the message "This is a tooltip!".



Tooltip Customizations

Below is a list of possible customizations you can implement to enhance the tooltip's design, behavior, and functionality. These changes will involve tweaking the HTML, CSS, and JavaScript to provide additional effects and options.


1. Change Tooltip Position (Top, Bottom, Left, Right)

You can modify the tooltip's position relative to the target element by adjusting the CSS.


Customization Example:


Change Tooltip Position to the Right:

.tooltip-text {
  visibility: hidden;
  width: 120px;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 5px;
  padding: 5px;
  position: absolute;
  z-index: 1;
  left: 125%; /* Position to the right */
  top: 50%;
  transform: translateY(-50%); /* Center vertically */
  opacity: 0;
  transition: opacity 0.3s;
}

2. Add Fade-in and Fade-out Effects

You can enhance the tooltip's appearance with more noticeable fade-in and fade-out transitions.


Customization Example:

.tooltip-text {
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.4s ease-in-out;
}

.tooltip-container:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
}

3. Change Tooltip Background Color

You can customize the tooltip background to fit your post's theme, using different colors or even gradients.


Customization Example:

.tooltip-text {
  visibility: hidden;
  background-color: #4CAF50; /* Green background */
  color: #fff;
  text-align: center;
  border-radius: 5px;
  padding: 5px;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  margin-left: -60px;
  opacity: 0;
  transition: opacity 0.3s;
}

4. Add Tooltip Arrow in Different Styles

You can change the arrow's design or add multiple variations, such as rounded arrows or different colors.


Customization Example:


Rounded Arrow:

.tooltip-text::after {
  content: '';
  position: absolute;
  top: 100%;
  left: 50%;
  margin-left: -5px;
  border-width: 10px;
  border-style: solid;
  border-color: #4CAF50 transparent transparent transparent; /* Change the arrow color */
  border-radius: 2px; /* Make the arrow rounded */
}

5. Dynamic Tooltip with JavaScript (Manual Trigger)

Instead of using hover, you can trigger the tooltip through JavaScript when a button or element is clicked or focused.


Customization Example:

<button id="showTooltipButton">Click to Show Tooltip</button>
<div class="tooltip-container">
  Click to Show Tooltip
  <div class="tooltip-text">This is a tooltip!</div>
</div>

<script>
  document.getElementById('showTooltipButton').addEventListener('click', function() {
    const tooltip = document.querySelector('.tooltip-text');
    tooltip.style.visibility = 'visible';
    tooltip.style.opacity = 1;
  });

  document.querySelector('.tooltip-container').addEventListener('mouseleave', function() {
    const tooltip = this.querySelector('.tooltip-text');
    tooltip.style.visibility = 'hidden';
    tooltip.style.opacity = 0;
  });
</script>

6. Custom Tooltip Text or Content

You can dynamically change the content of the tooltip depending on the element it's associated with.


Customization Example:

<div class="tooltip-container" data-tooltip="Custom Tooltip Content">
  Hover over me
  <div class="tooltip-text"></div>
</div>

<script>
  document.querySelector('.tooltip-container').addEventListener('mouseenter', function() {
    const tooltipText = this.getAttribute('data-tooltip');
    const tooltip = this.querySelector('.tooltip-text');
    tooltip.innerText = tooltipText;
  });
</script>

7. Adjust Tooltip Size Dynamically

You can make the tooltip size responsive based on its content, so it adjusts based on the text length.


Customization Example:

.tooltip-text {
  visibility: hidden;
  background-color: black;
  color: #fff;
  text-align: center;
  border-radius: 5px;
  padding: 5px;
  position: absolute;
  z-index: 1;
  bottom: 125%;
  left: 50%;
  transform: translateX(-50%);
  opacity: 0;
  transition: opacity 0.3s ease, width 0.3s ease;
  white-space: nowrap;
  width: auto;
  min-width: 120px;
  max-width: 250px; /* Limit width */
  padding: 10px;
}

8. Tooltip with Multiple Lines of Text

You can allow the tooltip to show multiline content or HTML inside the tooltip.


Customization Example:

<div class="tooltip-container">
  Hover over me
  <div class="tooltip-text">
    <strong>Important Info:</strong><br>
    This is a longer tooltip with more text that spans multiple lines.
  </div>
</div>

9. Tooltip with Delayed Show/Hide

Add a delay before the tooltip shows up or hides to create a more polished effect.


Customization Example:

.tooltip-text {
  visibility: hidden;
  opacity: 0;
  transition: opacity 0.3s ease-in-out;
  transition-delay: 0.5s; /* Delay the visibility */
}

.tooltip-container:hover .tooltip-text {
  visibility: visible;
  opacity: 1;
  transition-delay: 0s; /* Remove delay when showing */
}

10. Tooltip with Animation (Bounce)

You can add an animation effect like a bounce when the tooltip appears or disappears.


Customization Example:

@keyframes bounceIn {
  0% { transform: translateY(-20px); opacity: 0; }
  50% { transform: translateY(10px); opacity: 1; }
  100% { transform: translateY(0); opacity: 1; }
}

.tooltip-container:hover .tooltip-text {
  animation: bounceIn 0.3s ease-out forwards;
}

Full Combined Example with Several Customizations:

<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Tooltip Example with Customizations</title>
  <style>
    /* Tooltip container */
    .tooltip-container {
      position: relative;
      display: inline-block;
      cursor: pointer;
    }

    /* Tooltip text with various customizations */
    .tooltip-text {
      visibility: hidden;
      opacity: 0;
      width: auto;
      min-width: 120px;
      background-color: #4CAF50; /* Green background */
      color: #fff;
      text-align: center;
      border-radius: 5px;
      padding: 10px;
      position: absolute;
      z-index: 1;
      bottom: 125%;
      left: 50%;
      transform: translateX(-50%);
      transition: opacity 0.3s ease-in-out, width 0.3s ease;
      white-space: nowrap;
    }

    /* Tooltip arrow */
    .tooltip-text::after {
      content: '';
      position: absolute;
      top: 100%;
      left: 50%;
      margin-left: -5px;
      border-width: 10px;
      border-style: solid;
      border-color: #4CAF50 transparent transparent transparent;
    }

    /* Show tooltip with hover */
    .tooltip-container:hover .tooltip-text {
      visibility: visible;
      opacity: 1;
    }

    /* Tooltip with bounce animation */
    @keyframes bounceIn {
      0% { transform: translateY(-20px); opacity: 0; }
      50% { transform: translateY(10px); opacity: 1; }
      100% { transform: translateY(0); opacity: 1; }
    }

    /* Apply bounce effect */
    .tooltip-container:hover .tooltip-text {
      animation: bounceIn 0.3s ease-out forwards;
    }
  </style>
</head>
<body>

  <div class="tooltip-container">
    Hover over me
    <div class="tooltip-text">This is a tooltip with customizations!</div>
  </div>

</body>
</html>

Customizations included:

  • Tooltip positioning above the target element.

  • Green background with white text.

  • Animated bounce effect when the tooltip appears.

  • Visibility transitions with delay and fade-in/out effects.

  • Arrow at the bottom of the tooltip.


Feel free to play around with these customizations and adjust them according to your design needs!

bottom of page