JS: How to Create Tooltip
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:
<p> <span id="hotbox18623">Hover over me</span> to see tooltip. </p>
Here is the JavaScript code:
// © 2013, 2020 Xah Lee, http://xahlee.info/ // create a tooltip { const hotbox18623 = document.getElementById("hotbox18623"); // create tooltip element const ttBox = document.createElement("div"); // set style ttBox.id = "tooltip88493"; ttBox.style.visibility = "hidden"; // make it hidden till mouse over ttBox.style.position = "fixed"; ttBox.style.top = "8px"; ttBox.style.left = "8px"; ttBox.style.padding = "8px"; ttBox.style.width = "300px"; ttBox.style.borderRadius = "16px"; ttBox.style.border = "solid thin grey"; ttBox.style.backgroundColor = "grey"; // insert into DOM document.body.appendChild(ttBox); const f_TurnOn = ((evt) => { // get the position of the hover element const boundBox = evt.target.getBoundingClientRect(); const coordX = boundBox.left; const coordY = boundBox.top; // adjust bubble position ttBox.style.left = (coordX + 40).toString() + "px"; ttBox.style.top = (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 ttBox.style.visibility = "visible"; }); const f_TurnOff = (() => { ttBox.style.visibility = "hidden"; }); hotbox18623.addEventListener("mouseover", f_TurnOn, false); hotbox18623.addEventListener("mouseout", f_TurnOff, false); document.getElementById("tooltip88493").addEventListener( "click", f_TurnOff, false, ); }
Here is how it works:
- Use JavaScript to create a HTML element. This will be your tooltip box.
- Make the tooltip box invisible, by setting CSS
visibility:hidden
. - Set the tooltip box on its own layer (CSS
position:fixed
), so that it doesn't interfere with other elements on the page. - Assign function/handler to the “mouse over” and “mouse out” events, for elements that you want tooltip box to appear.
- 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. - 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:
- Creating a element. JS: Create and Insert HTML Element
- Using CSS to put a element into a layer, and control position and visibility. CSS Layout Tutorial
- Using JavaScript to change a element's CSS. JS: Change CSS
- Using JavaScript to set a element's content. JS: Change Element's Content
That's it!
CSS, Layout
- CSS: Display Property
- Pure CSS Table
- CSS: 3-Column Side-Panel Layout
- CSS: Newspaper Text-Flow Over Multi-Column Layout
- CSS: Text Flow Around Image
- CSS: Hide Element (visibility)
CSS, Layers
- CSS: Position Property
- CSS: Fix Element to Window
- CSS: Position: Relative
- CSS: Absolute Position
- CSS: Text Over Image
- CSS: z-index