Create a Custom Tooltip with JavaScript and Use It in Webflow Components

Ever hovered over a button and seen a fancy tooltip pop up with helpful info? 🤔 You know, those little floating labels that save users from confusion?

Webflow doesn’t have a built-in tooltip component, but don’t worry—we’ll build a fully custom tooltip that works on any Webflow element with just HTML, CSS, and JavaScript. 💡

🔥 What You’ll Learn:
✅ How to use custom attributes to create tooltips dynamically
✅ A clean JavaScript function to handle tooltips
✅ Smooth CSS animations for a sleek effect
✅ How to integrate it into Webflow like a pro

Let’s get started! 🚀

Step 1: Set Up Your Webflow Elements

First, choose which elements will have tooltips. You can apply tooltips to buttons, icons, links—anything!

📌 Add Custom Attributes in Webflow

1️⃣ Select the element (e.g., a button, icon, or link).
2️⃣ Go to the Element Settings Panel.
3️⃣ Add a custom attribute:

  • Name: data-tooltip
  • Value: (your tooltip text, e.g., "Click here to learn more!")

📸 (Insert image of Webflow settings panel with the custom attribute added)

Example HTML in Webflow

If you’re using Webflow’s Embed Code Block, you can add something like this:

<button class="tooltip-trigger" data-tooltip="I am a tooltip!">
   Hover me
</button>

Step 2: Add JavaScript to Handle Tooltips

Now, we need a JavaScript function that:
✅ Detects elements with the data-tooltip attribute
✅ Creates a tooltip box dynamically
✅ Positions it correctly
✅ Hides it when not needed

⚡ Add This JavaScript to Webflow

Go to Page Settings → Custom Code (Before </body>) and paste this:

<script>
  document.addEventListener("DOMContentLoaded", function () {
    const tooltipElements = document.querySelectorAll("[data-tooltip]");

    tooltipElements.forEach((element) => {
      element.addEventListener("mouseenter", function () {
        // Create tooltip element
        let tooltip = document.createElement("div");
        tooltip.className = "custom-tooltip";
        tooltip.innerText = element.getAttribute("data-tooltip");
        document.body.appendChild(tooltip);

        // Position tooltip
        const rect = element.getBoundingClientRect();
        tooltip.style.left = rect.left + window.scrollX + rect.width / 2 + "px";
        tooltip.style.top = rect.top + window.scrollY - 30 + "px";

        // Show tooltip
        setTimeout(() => {
          tooltip.classList.add("visible");
        }, 100);
        
        element.addEventListener("mouseleave", function () {
          tooltip.classList.remove("visible");
          setTimeout(() => tooltip.remove(), 300);
        });
      });
    });
  });
</script>


💡 How It Works:

1️⃣ Finds all elements with data-tooltip.
2️⃣ Creates a floating tooltip div dynamically.
3️⃣ Positions the tooltip above the element.
4️⃣ Fades in/out with smooth transitions.

Step 3: Style the Tooltip with CSS 🎨

To make the tooltip look clean and professional, we’ll add CSS animations.

⚡ Add This CSS to Webflow

Go to Page Settings → Custom Code (Inside <style> in Head Section) and paste this:

.custom-tooltip {
  position: absolute;
  background: black;
  color: white;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 14px;
  white-space: nowrap;
  opacity: 0;
  transform: translateY(-5px);
  transition: opacity 0.2s ease, transform 0.2s ease;
  pointer-events: none; /* Prevents interfering with user actions */
}

.custom-tooltip.visible {
  opacity: 1;
  transform: translateY(0);
}


📸 Tooltip in Action!

📸 (Insert an image showing the tooltip appearing above a button in Webflow)

✅ Done! Now Test Your Custom Tooltip

👀 Hover over any element with a data-tooltip and watch the magic happen! 🎩✨

🔥 Why This Is Awesome?
✔️ No need for Webflow interactions
✔️ Fully customizable – Just tweak CSS!
✔️ Works on any Webflow component

💡 Bonus: Position the Tooltip Left or Right!

Want to control where the tooltip appears? Easy! Just add a second custom attribute:

  • For left-aligned tooltips: data-tooltip-position="left"
  • For right-aligned tooltips: data-tooltip-position="right"

🔹 Modify the JavaScript to Handle Positioning

Replace the previous JavaScript with this:

<script>
  document.addEventListener("DOMContentLoaded", function () {
    const tooltipElements = document.querySelectorAll("[data-tooltip]");

    tooltipElements.forEach((element) => {
      element.addEventListener("mouseenter", function () {
        let tooltip = document.createElement("div");
        tooltip.className = "custom-tooltip";
        tooltip.innerText = element.getAttribute("data-tooltip");
        document.body.appendChild(tooltip);

        const rect = element.getBoundingClientRect();
        const position = element.getAttribute("data-tooltip-position") || "top";

        if (position === "left") {
          tooltip.style.left = rect.left + window.scrollX - tooltip.offsetWidth - 10 + "px";
          tooltip.style.top = rect.top + window.scrollY + rect.height / 2 - tooltip.offsetHeight / 2 + "px";
        } else if (position === "right") {
          tooltip.style.left = rect.right + window.scrollX + 10 + "px";
          tooltip.style.top = rect.top + window.scrollY + rect.height / 2 - tooltip.offsetHeight / 2 + "px";
        } else {
          tooltip.style.left = rect.left + window.scrollX + rect.width / 2 - tooltip.offsetWidth / 2 + "px";
          tooltip.style.top = rect.top + window.scrollY - tooltip.offsetHeight - 10 + "px";
        }

        setTimeout(() => {
          tooltip.classList.add("visible");
        }, 100);

        element.addEventListener("mouseleave", function () {
          tooltip.classList.remove("visible");
          setTimeout(() => tooltip.remove(), 300);
        });
      });
    });
  });
</script>


Now, you can control tooltip position in Webflow without touching code! 😍

🎯 Final Thoughts

And that’s it! You just built a fully functional tooltip system in Webflow using only HTML, CSS, and JavaScript. 🎉

🚀 What Did You Learn?

✅ How to use custom attributes in Webflow
✅ How to generate tooltips dynamically with JavaScript
✅ How to style smooth tooltip animations with CSS
✅ How to position tooltips based on attributes