Skip to main content

Creating User-Friendly Hints in HTML with Popover=Hint Magic

 

Creating User-Friendly Hints in HTML with Popover=Hint Magic


How to Creating User-Friendly Hints in HTML with Popover Magic

Recently I was going through an article on how to build a web interface that feels effortless is your goal, but layered UI elements like tooltips can be a coding nightmare.

I love that clean, intuitive experience, but layering popovers without breaking the flow is a nightmare. The popover=hint attribute in HTML is my new best friend for this.

It allows you to open a UI like: tooltip or preview without closing other popovers. It’s a lightweight way to show context without hijacking the user’s focus.

Let’s See it in Action


Let’s have a look at a simple example to understand how it works under the hood.

HTML popover=hint example in action

<!DOCTYPE html>
<html lang="en">
<head>
<style>
[popover="auto"] {
inset: unset;
position: absolute;
top: 100px;
justify-self: anchor-center;
margin: 0;
        text-align: center;
padding: 8px;
}
      [popover="hint"] {
inset: unset;
position: absolute;
top: calc(anchor(bottom) + 5px);
justify-self: anchor-center;
margin: 0;
        padding: 8px;
border-radius: 6px;
background: #271717;
color: whitesmoke;
box-shadow: 1px 1px 3px #999;
border: none;
}
      /* Styling help para */
      .help-para {
position: absolute;
top: 16px;
left: 16px;
background: #eee;
font-size: 0.8rem;
line-height: 1.3;
width: 50%;
max-width: 600px;
margin: 0;
padding: 16px;
box-shadow: 1px 1px 3px #999;
}
      @media (max-width: 640px) {
.help-para {
width: auto;
right: 16px;
}
}
      body {
margin: 50px;
padding: 10px;
border: 2px solid lightblue;
border-radius: 8px;
}
</style>
</head>
<body>
<div id="wrapper">
<section id="button-bar">
<button
popovertarget="submenu-1"
popovertargetaction="toggle"
id="menu-1"
>
Menu A
</button>
        <button
popovertarget="submenu-2"
popovertargetaction="toggle"
id="menu-2"
>
Menu B
</button>
        <button
popovertarget="submenu-3"
popovertargetaction="toggle"
id="menu-3"
>
Menu C
</button>
</section>
</div>
<div id="submenu-1" popover="auto">
<button>Option A</button><br /><button>Option B</button>
</div>
<div id="submenu-2" popover="auto">
<button>Option A</button><br /><button>Option B</button>
</div>
<div id="submenu-3" popover="auto">
<button>Option A</button><br /><button>Option B</button>
</div>
    <div id="tooltip-1" class="tooltip" popover="hint">Tooltip A</div>
<div id="tooltip-2" class="tooltip" popover="hint">Tooltip B</div>
<div id="tooltip-3" class="tooltip" popover="hint">Tooltip C</div>
    <script text="text/javascript">
const tooltips = document.querySelectorAll(".tooltip");
const btns = document.querySelectorAll("#button-bar button");
      function addEventListeners(i) {
btns[i].addEventListener("mouseover", () => {
tooltips[i].showPopover({ source: btns[i] });
});
        btns[i].addEventListener("mouseout", () => {
tooltips[i].hidePopover();
});
        btns[i].addEventListener("focus", () => {
tooltips[i].showPopover({ source: btns[i] });
});
        btns[i].addEventListener("blur", () => {
tooltips[i].hidePopover();
});
}
      for (let i = 0; i < btns.length; i++) {
addEventListeners(i);
}
</script>
</body>
</html>

That’s it. A context aware user interface with simple JavaScript, CSS and HTML. The hint pops up on hover, then vanishes when you move on.

But wait, there’s more!


Hint popovers, set with popover=hint, are different from popover=auto or popover=manual.

They’re light-dismissible, meaning a tap outside or ESC key sends them packing. They don’t close auto popovers, but they’ll shut down other hints to avoid clutter.

Here’s a quick comparison:

| Feature           | Popover=Auto | Popover=Hint | Popover=Manual |
|-------------------|--------------|--------------|----------------|
| Light Dismiss | Yes | Yes | No |
| Closes Others | Hints, Autos | Other Hints | Nothing |
| Nesting | Yes | Special | N/A |

Nesting: The Tricky Part


Nesting hints can get wild. Most tooltips stand alone, but sometimes you’ve got a “rich” tooltip like GitHub’s profile previews with elements that have their own tooltips. Those shouldn’t close the parent tooltip.

Here’s the deal:

  • Two stacks exist: “auto stack” for popover=auto and “hint stack” for standalone hints.
  • A hint inside an auto popover joins the auto stack.
  • A hint inside another hint stays in the hint stack.
  • An auto popover can’t nest inside a hint.

I know it can be a little confusing. Here is the trick: think of it like layers. A hint inside an auto popover sticks with its parent, so hovering an unrelated hint won’t break the chain.

Final Takeaway


Today we had a look at very simple HTML magic which allows to buildcontext-sensitive UI elements.

Try this in your next project and share your experience in the comments below.

Thank you for joining. Let’s meet again with another cool HTML magic.


Comments

Popular posts from this blog

Sharpen Your Front-End Skills: Quick HTML, CSS & React Interview Challenges

  The source of this image is Chat GPT based on writing! Are you preparing for front-end developer interviews and looking for practical, hands-on ways to improve your HTML, CSS, and React skills? Whether you’re a beginner aiming to build confidence or an experienced developer brushing up on UI skills, small, targeted challenges can make a huge difference. In this article, I’ll walk you through some of the best free and low-cost resources that offer real-world front-end tasks — perfect for interview prep, portfolio building, and daily practice. 1. Frontend Mentor frontendmentor.io Frontend Mentor is one of the most popular platforms for hands-on HTML, CSS, and JavaScript challenges. You get beautifully designed templates (in Figma or image formats) and are asked to bring them to life using clean code. The platform offers difficulty levels ranging from newbie to expert, and it’s perfect for practicing responsiveness and semantic HTML. Bonus : You can even filter for React-based ...

Difference Between Three.js and Babylon.js: What Actually Should You Choose?

You don’t have to be just a graphic designer to create interactive designs. You can be a coder and still create visually appealing and eye-catching games. All thanks to JavaScript. The first cross-browser JavaScript library–three.js–that can create 3D computer graphics was first released on 24 April 2010 by Ricardo Cabello. He first wrote the code in ActionScript language, which was then used by Adobe Flash. But then in 2009, he ported the code to JavaScript. Previously, people used WebGL. But the problem was its limitation: it can create only simple pointers and lines. Ricardo, instead of abandoning WebGL as something that is futile, used it to his own advantage. He built three.js on top of WebGL. This renders three.js to create 3D graphics in the browser. Even a 3D scene can be created easily using Canvas and WebGL now. But then in 2013, Babylon.js was created. But why? Why did its creators, Microsoft and David Catuhe, make something that another JavaScript library–three.js –was alre...

React Native vs React JS — Key Difference, Advantages-Disadvantages, Limitations

  React Native vs React JS — Key Difference, Advantages-Disadvantages, Limitations React JS It is a JavaScript library that supports each face and server-side. It’s a popularly used library that focuses on developing user interfaces for mobile and internet-primarily based applications. React Native It is a cross-platform mobile framework that uses the ReactJS framework. It’s primarily used for developing native mobile applications like Windows, iOS and mechanical man. The major advantage provided by React Native is that it permits the developers to form mobile applications on varied platforms while not compromising the tip user’s expertise. Components of React JS Components of React Native Basic parts View — it is the essential building block of internet applications. Text — It helps to point out the text. The text element contains nesting, styling, and bit handling. Image — this is often a React element for showing multiple footages like network pictures and static resources. Text...