JavaScript: How to Create Tooltip

By Xah Lee. Date: . Last updated: .

This page shows you how to do floating tooltip, without using any JavaScript library.

Hover over me to see tooltip.

Here is the HTML code:

<span id="hotbox18623">Hover over me</span>
to see tooltip.

Here is the JavaScript code:

// © 2013, 2020 Xah Lee,
// create a tooltip

  const hotbox18623 = document.getElementById("hotbox18623");

  // create tooltip element
  const ttBox = document.createElement("div");

  // set style = "tooltip88493"; = "hidden"; // make it hidden till mouse over = "fixed"; = "8px"; = "8px"; = "8px"; = "300px"; = "16px"; = "solid thin grey"; = "grey";

  // insert into DOM

  const f_TurnOn = ((evt) => {
    // get the position of the hover element
    const boundBox =;
    const coordX = boundBox.left;
    const coordY =;

    // adjust bubble position = (coordX + 40).toString() + "px"; = (coordY + 40).toString() + "px";

    // add bubble content. Can include image or link
    ttBox.innerHTML =
      "some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing some thing.";

    // make bubble VISIBLE = "visible";

  const f_TurnOff = (() => { = "hidden";

  hotbox18623.addEventListener("mouseover", f_TurnOn, false);
  hotbox18623.addEventListener("mouseout", f_TurnOff, false);

Here is how it works:

  1. Use JavaScript to create a HTML element. This will be your tooltip box.
  2. Make the tooltip box invisible, by setting CSS visibility:hidden.
  3. Set the tooltip box on its own layer (CSS position:fixed), so that it doesn't interfere with other elements on the page.
  4. Assign function/handler to the “mouse over” and “mouse out” events, for elements that you want tooltip box to appear.
  5. The function to activate the tooltip, will change tooltip's CSS so it's visible (visibility:visible), and also position it near the mouse. Also add whatever content you want to the tooltip.
  6. The function to deactivate the bubble, will change the tooltip's CSS so it's invisible again.

If you have trouble understanding the code, here's some detail on how to do each step:

That's it!

CSS Layout

CSS Layers

JavaScript Using Css Layers

JavaScript in Depth

DOM Scripting


DOM Methods

Basic Examples

HTML Input

Web Scripting Examples

Web Scripting Misc