Search

Showing and Hiding HTML elements using Layers

2 min read
0 views

Hover Tooltip with Images

When you visit a site that feels polished, one of the first things that catches your eye is how the navigation responds to your mouse movements. The designer may have slipped a subtle animation into the header, or they might have added a tooltip that pops up when you hover over a link. In the early days of the web, developers relied on layers - transparent containers that could be positioned freely on the page - to achieve this kind of interaction. Although modern CSS has largely superseded layers, the underlying principle remains the same: you create a hidden element and reveal it when the user triggers an event.

Consider a situation where a site has a link called “Product Features.” Instead of just directing the user to another page, the site could display a comic‑style speech bubble containing a quick description or a short illustration. This not only keeps the user on the same page but also delivers information in an engaging visual format. The trick is to make the bubble appear instantly on hover and to hide it when the mouse leaves the link.

To implement this, you’ll need three building blocks: an HTML element that holds the link, a CSS rule that defines the look and position of the tooltip, and a small JavaScript snippet that toggles visibility. The CSS layer must sit above the page’s content, so setting a high z-index is essential. If you want the bubble to appear over the text rather than below it, you’ll need to position it absolutely relative to a positioned ancestor. The JavaScript part will listen for mouseover and mouseout events and switch the element’s display property from none to block

Before diving into the code, take a moment to explore a live demonstration. A simple link on the page triggers an image that slides into view above the text. You can adjust the positioning by tweaking a few values, giving you full control over where the tooltip appears. The demo can be seen on the online version of this article, which also hosts the source code. By watching the interaction live, you’ll see how the combination of CSS positioning and JavaScript event handling brings the tooltip to life.

In short, the goal is to combine a hidden container with precise styling and a lightweight script that manages user interactions. This approach is versatile: it can work with plain images, SVG graphics, or even HTML content. Because the tooltip stays on the same page, the user remains engaged without the need for a new window or a redirect. The technique is lightweight, easy to maintain, and can be adapted to modern frameworks or vanilla JavaScript projects.

Building the Markup and Styles

The first step is to create the HTML structure. The markup consists of an anchor tag that the user will hover over and a <div> that will hold the image. The <div> is placed just after the anchor so that it appears in the document flow. Here is the basic skeleton:

Prompt
<a href="#" id="tooltip-link">Hover over me</a></p> <p><div id="tooltip-container"></p> <p> <img src="example-image.jpg" alt="Tooltip Image" /></p> <p></div>

Notice the id attributes; they give us unique hooks for CSS and JavaScript. The <div> is the layer that will be positioned absolutely and hidden by default. In the CSS, we’ll set the position to absolute, give it a z-index high enough to sit above all other content, and apply the display: none; rule so it stays hidden until the user hovers over the link.

Here is a comprehensive CSS block that handles all of that. It also includes a subtle fade‑in transition to make the appearance smoother.

Prompt
#tooltip-container {</p> <p> position: absolute;</p> <p> top: 30px; /<em> Distance from the top of the nearest positioned ancestor </em>/</p> <p> left: 0; /<em> Align left edge with the link </em>/</p> <p> z-index: 1000; /<em> Ensure the tooltip appears above other elements </em>/</p> <p> display: none; /<em> Hidden by default </em>/</p> <p> width: 200px; /<em> Fixed width for consistency </em>/</p> <p> padding: 10px; /<em> Space inside the container </em>/</p> <p> background: #fff; /<em> Background color </em>/</p> <p> border: 1px solid #ccc; /<em> Light border for definition </em>/</p> <p> box-shadow: 0 4px 8px rgba(0,0,0,0.1); /<em> Slight shadow for depth </em>/</p> <p> transition: opacity 0.3s ease; /<em> Fade effect </em>/</p> <p> opacity: 0; /<em> Start fully transparent </em>/</p> <p>}</p> <p>#tooltip-container.show {</p> <p> display: block; /<em> Switch to block when shown </em>/</p> <p> opacity: 1; /<em> Fade to fully opaque </em>/</p> <p>}</p> <p>#tooltip-link {</p> <p> position: relative; /<em> Needed for accurate positioning of the tooltip </em>/</p> <p> cursor: pointer; /<em> Indicates interactive element </em>/</p> <p> color: #007bff; /<em> Default link color </em>/</p> <p> text-decoration: underline; /<em> Underline for clarity </em>/</p> <p>}</p> <p>#tooltip-link:hover {</p> <p> color: #0056b3; /<em> Darken on hover for visual feedback </em>/</p> <p>}

There are a few key points to note. The #tooltip-container is absolutely positioned relative to the nearest ancestor with position: relative;, which in this case is the link itself. By setting top: 30px;, the tooltip will appear 30 pixels below the link. If you want it above the link, use a negative value. The z-index: 1000; is a high number that guarantees the tooltip sits on top of almost everything else. The opacity transition adds a subtle fade‑in that makes the interaction feel more refined.

In many cases, developers want the tooltip to be centered horizontally over the link. You can achieve that by setting left: 50%; and then applying transform: translateX(-50%); to shift the container back by half its width. This technique ensures the tooltip stays centered regardless of the link’s width or the container’s content size.

Now that the markup and styling are in place, the last piece is the script that toggles the visibility class on #tooltip-container. By listening to the mouseover and mouseout events on the link, the script will add or remove the show class. This class is what triggers the CSS transition and changes the display property. The script is intentionally lightweight and vanilla, making it easy to drop into any project without bringing in a heavy library.

Adding JavaScript to Control Visibility

The JavaScript portion is straightforward but essential. We’ll add an event listener to the link that listens for the mouse entering and leaving the element. When the mouse enters, we add the show class to the tooltip container; when the mouse leaves, we remove it. Below is the complete script that accomplishes this:

Prompt
// Select the elements</p> <p>const tooltipLink = document.getElementById('tooltip-link');</p> <p>const tooltipContainer = document.getElementById('tooltip-container');</p> <p>// Function to show the tooltip</p> <p>function showTooltip() {</p> <p> tooltipContainer.classList.add('show');</p> <p>}</p> <p>// Function to hide the tooltip</p> <p>function hideTooltip() {</p> <p> tooltipContainer.classList.remove('show');</p> <p>}</p> <p>// Attach event listeners</p> <p>tooltipLink.addEventListener('mouseover', showTooltip);</p> <p>tooltipLink.addEventListener('mouseout', hideTooltip);</p> <p>// Optional: Handle keyboard focus for accessibility</p> <p>tooltipLink.addEventListener('focus', showTooltip);</p> <p>tooltipLink.addEventListener('blur', hideTooltip);</p>

The script first grabs references to the link and the tooltip container. The showTooltip function adds the show class, which, as defined in the CSS, sets display: block; and opacity: 1;. The hideTooltip function reverses that by removing the class. By attaching both mouseover and mouseout events, we cover mouse interaction. Adding focus and blur events ensures the tooltip also appears when a keyboard user navigates to the link, improving accessibility.

Because the script only manipulates class names, it leaves the heavy lifting to CSS. This separation of concerns keeps the JavaScript lean and the presentation fully controllable through stylesheets. If you need the tooltip to follow the cursor instead of staying fixed, you can modify showTooltip to set the left and top properties dynamically based on event.clientX and event.clientY. However, for most use cases, a fixed position relative to the link is simpler and more predictable.

When you integrate this solution into your project, consider the user’s device. On touch devices, the mouseover event is not triggered, so you may want to replace it with touchstart or use a media query to hide the tooltip on smaller screens. Additionally, if you’re working with a framework like React or Vue, the same logic applies - just convert the vanilla event handling into the framework’s component lifecycle or event system.

Suggest a Correction

Found an error or have a suggestion? Let us know and we'll review it.

Share this article

Comments (0)

Please sign in to leave a comment.

No comments yet. Be the first to comment!

Related Articles