Creating a custom tooltip

Loading

Creating a Custom Tooltip: A Complete Guide

Introduction to Tooltips

A tooltip is a small pop-up box that appears when the user hovers over an element, providing additional information or context. Tooltips are widely used in modern web applications to enhance usability, accessibility, and user experience (UX).

By default, browsers offer a simple tooltip via the title attribute, but custom tooltips provide better styling, animations, positioning, and flexibility.


Why Create a Custom Tooltip?

The default browser tooltip is limited in terms of: ✔️ Styling: Cannot be customized (background, text, font, etc.).
✔️ Positioning: Appears in a default position near the element.
✔️ Animation: Lacks fade-in, fade-out, or transition effects.
✔️ Content Customization: Cannot include images, links, or HTML formatting.

By creating a custom tooltip, we can: ✅ Fully control the design (colors, size, shadows, etc.).
Customize placement (above, below, left, right, or dynamic positioning).
Add animations (fade, slide, bounce, etc.).
Include rich content (HTML, images, links, buttons).


1. Setting Up the HTML Structure

To build a tooltip, we need:

  • A target element (button, link, image, or text).
  • A tooltip container that appears dynamically.

Basic HTML Structure

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Custom Tooltip</title>
    <link rel="stylesheet" href="styles.css">  <!-- External CSS -->
</head>
<body>

    <!-- Tooltip Target -->
    <button class="tooltip-trigger" data-tooltip="This is a custom tooltip!">Hover Me</button>

    <script src="script.js"></script> <!-- External JS -->
</body>
</html>

📌 Explanation:

  • The button has a data-tooltip attribute storing the tooltip text.
  • We will use JavaScript to read this attribute and display a tooltip dynamically.

2. Styling the Tooltip (CSS)

Now, let’s design the tooltip appearance.

CSS Code: Tooltip Design

.tooltip {
    position: absolute;
    background: #333;
    color: white;
    padding: 8px 12px;
    font-size: 14px;
    border-radius: 5px;
    white-space: nowrap;
    box-shadow: 2px 2px 8px rgba(0, 0, 0, 0.2);
    opacity: 0;
    transition: opacity 0.3s ease-in-out;
    pointer-events: none; /* Prevents interaction */
}

/* Tooltip Arrow */
.tooltip::after {
    content: "";
    position: absolute;
    border: 5px solid transparent;
    border-top-color: #333; /* Arrow pointing downward */
    bottom: -10px;
    left: 50%;
    transform: translateX(-50%);
}

📌 Explanation:
position: absolute; → Allows precise placement.
background: #333; color: white; → Sets tooltip styling.
opacity: 0; → Initially hidden, appears smoothly with transition.
pointer-events: none; → Prevents users from clicking on the tooltip.
::after → Creates a triangle arrow pointing to the element.


3. JavaScript to Display the Tooltip

Now, let’s add JavaScript to handle hover events and display the tooltip.

JavaScript Code

document.addEventListener("DOMContentLoaded", function () {
    const tooltipTrigger = document.querySelector(".tooltip-trigger");

    tooltipTrigger.addEventListener("mouseenter", function (event) {
        let tooltipText = this.getAttribute("data-tooltip");

        // Create tooltip element
        let tooltip = document.createElement("div");
        tooltip.classList.add("tooltip");
        tooltip.innerText = tooltipText;
        document.body.appendChild(tooltip);

        // Position the tooltip
        let rect = this.getBoundingClientRect();
        tooltip.style.left = rect.left + window.scrollX + "px";
        tooltip.style.top = rect.top + window.scrollY - tooltip.offsetHeight - 10 + "px"; // Above the button

        // Show tooltip
        tooltip.style.opacity = "1";

        // Remove tooltip when mouse leaves
        this.addEventListener("mouseleave", function () {
            tooltip.remove();
        });
    });
});

📌 Explanation:

  1. When the user hovers over tooltip-trigger, we:
    • Read the data-tooltip text.
    • Create a tooltip <div> and add text inside.
    • Append the tooltip to the body.
    • Position the tooltip relative to the button.
    • Make it fade in by setting opacity: 1.
  2. When the mouse leaves, the tooltip disappears.

4. Adding Dynamic Positioning

Instead of always placing the tooltip above, we can dynamically adjust the position.

Improved JavaScript with Position Adjustment

document.addEventListener("DOMContentLoaded", function () {
    const tooltipTrigger = document.querySelector(".tooltip-trigger");

    tooltipTrigger.addEventListener("mouseenter", function (event) {
        let tooltipText = this.getAttribute("data-tooltip");

        let tooltip = document.createElement("div");
        tooltip.classList.add("tooltip");
        tooltip.innerText = tooltipText;
        document.body.appendChild(tooltip);

        // Get element position
        let rect = this.getBoundingClientRect();
        let tooltipHeight = tooltip.offsetHeight;
        let tooltipWidth = tooltip.offsetWidth;

        let topPosition = rect.top + window.scrollY - tooltipHeight - 10; // Default above
        let leftPosition = rect.left + window.scrollX + (rect.width / 2) - (tooltipWidth / 2);

        // Adjust position if tooltip goes out of viewport
        if (topPosition < 0) {
            topPosition = rect.bottom + window.scrollY + 10; // Show below
            tooltip.style.background = "#ff9800"; // Change color for below position
            tooltip.style.borderRadius = "8px";
        }

        tooltip.style.left = `${leftPosition}px`;
        tooltip.style.top = `${topPosition}px`;

        tooltip.style.opacity = "1";

        this.addEventListener("mouseleave", function () {
            tooltip.remove();
        });
    });
});

🎯 Features Added:
Tooltip repositioning if out of viewport (appears below instead).
Color change for visibility (for below-positioned tooltips).
Smooth transitions & better centering.


5. Enhancing Tooltips with Animations

We can improve user experience by adding fade-in, bounce, or slide animations.

Updated CSS with Animation

.tooltip {
    animation: fadeIn 0.3s ease-out;
}

/* Fade-in effect */
@keyframes fadeIn {
    from { opacity: 0; transform: translateY(-10px); }
    to { opacity: 1; transform: translateY(0); }
}

6. Adding Rich Content to Tooltips

We can add HTML content (images, links, buttons) inside tooltips.

Example: Tooltip with an Image

<button class="tooltip-trigger" data-tooltip="<img src='icon.png' width='30'> Custom Tooltip!">Hover Me</button>

Modify JavaScript to Support HTML

tooltip.innerHTML = tooltipText; // Supports HTML inside tooltip

🎯 Now tooltips can include:
✅ Images
✅ Links
✅ Buttons


7. Performance Considerations

Optimize Event Listeners: Use event delegation for many tooltips.
Throttle Tooltip Creation: If using tooltips on many elements, optimize rendering.
Memory Management: Remove tooltips when they are no longer needed.


We have successfully created a custom tooltip with:
Custom styling and animations
Dynamic positioning
Support for HTML content
Smooth fade-in effects

Would you like an advanced version with arrow indicators and different shapes? Let me know!

Leave a Reply

Your email address will not be published. Required fields are marked *