Back

Enhance User Interaction with Interactive Smart Tooltips

A detailed guide on building intelligent tooltips that offer users an intuitive information experience with smooth animations.

HTML

To create this tooltip, we need a container that holds the button and the tooltip content. Let's start by creating a div with the class "tooltip-container".
        <div class="tooltip-container">
          <button class="info-button">
            Info
          </button>
          <div class="tooltip-content">
            <img src="https://images.unsplash.com/photo-1527529482837-4698179dc6ce" alt="Informative Image" width="100%"/>
            <p>This is an interactive tooltip with additional information to enhance user experience and provide helpful context.</p>
          </div>
        </div>
      

CSS

Now let's style our tooltip. First, let's style the container to position elements properly.
        .tooltip-container {
          position: relative;
          display: inline-block;
        }
      
Next, style the button to make it visually appealing and interactive.
        .info-button {
          padding: 10px 20px;
          font-size: 16px;
          cursor: pointer;
          background-color: #02a8f4;
          color: #fff;
          border: none;
          border-radius: 5px;
          transition: background-color 0.3s;
        }
        .info-button:hover {
          background-color: #029ddb;
        }
      
Now, let's focus on styling the tooltip content itself. Initially, it should be invisible. We use transitions for smooth animations when the tooltip is shown or hidden.
        .tooltip-content {
          visibility: hidden;
          width: 240px;
          background-color: #fff;
          color: #333;
          text-align: center;
          border-radius: 8px;
          padding: 10px;
          position: absolute;
          z-index: 1;
          top: 125%;
          left: 50%;
          transform: translateX(-50%);
          box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
          transition: visibility 0.3s, opacity 0.3s ease-out, transform 0.3s;
          opacity: 0;
        }
        .tooltip-content.visible {
          visibility: visible;
          opacity: 1;
          transform: translateX(-50%) translateY(-10px);
        }
      

JavaScript

Finally, we'll use JavaScript to toggle the visibility of the tooltip when the button is hovered.
        document.querySelector('.info-button').addEventListener('mouseover', function() {
          document.querySelector('.tooltip-content').classList.add('visible');
        });
        document.querySelector('.info-button').addEventListener('mouseout', function() {
          document.querySelector('.tooltip-content').classList.remove('visible');
        });
      
The event listeners wait for mouseover and mouseout events on the button. When triggered, they toggle the "visible" class on the tooltip element, making it appear and disappear with smooth transitions.

Whole code

<div class="tooltip-container">
  <button class="info-button">
    Info
  </button>
  <div class="tooltip-content">
    <img src="https://images.unsplash.com/photo-1527529482837-4698179dc6ce" alt="Informative Image" width="100%"/>
    <p>This is an interactive tooltip with additional information to enhance user experience and provide helpful context.</p>
  </div>
</div>


<script>
document.querySelector('.info-button').addEventListener('mouseover', function() {
  document.querySelector('.tooltip-content').classList.add('visible');
});

document.querySelector('.info-button').addEventListener('mouseout', function() {
  document.querySelector('.tooltip-content').classList.remove('visible');
});

</script>

<style>
.tooltip-container {
  position: relative;
  display: inline-block;
}

.info-button {
  padding: 10px 20px;
  font-size: 16px;
  cursor: pointer;
  background-color: #02a8f4;
  color: #fff;
  border: none;
  border-radius: 5px;
  transition: background-color 0.3s;
}

.info-button:hover {
  background-color: #029ddb;
}

.tooltip-content {
  visibility: hidden;
  width: 240px;
  background-color: #fff;
  color: #333;
  text-align: center;
  border-radius: 8px;
  padding: 10px;
  position: absolute;
  z-index: 1;
  top: 125%;
  left: 50%;
  transform: translateX(-50%);
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.1);
  transition: visibility 0.3s, opacity 0.3s ease-out, transform 0.3s;
  opacity: 0;
}

.tooltip-content.visible {
  visibility: visible;
  opacity: 1;
  transform: translateX(-50%) translateY(-10px);
}

</style>
      
Thank you for reading this article.

Comments

More tooltips

Similar

See also