日本語

How to Open a Popover on Hover with HTML and CSS

​​CSS​HTML,Playwright,Popover API

Add an interestfor attribute to a button or link and set it to the id of a popover, and the popover opens when the mouse hovers over the element and closes when the mouse leaves. It also opens when the element gets keyboard focus, and closes with the Esc key. You can change the delay before it opens with the CSS interest-delay property, and no JavaScript is needed. This is useful for making a tooltip that shows a short explanation near a button.

Note

The test environment is Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and Chromium 153. I opened the page in headless Chromium, used Playwright to operate the mouse and keyboard, and recorded the times when the interest, loseinterest, and toggle events fired. I did not test Firefox or Safari, real smartphones (the open-on-long-press behavior), screen reader announcements, or <area> and SVG <a> elements. The browser versions listed for each browser are values from the compatibility data (@mdn/browser-compat-data 8.1.8).

Add interestfor to the Button and popover="hint" to the Element to Show

Here is the basic markup. Write the id of the element you want to show in the interestfor attribute of the button. Add popover="hint" to the element you want to show.

html
<p>
  <button interestfor="tip">ヘルプ</button>
  下書きの保存について
</p>
<div id="tip" popover="hint">保存せずに閉じると、入力した内容は消えます</div>

An element with the popover attribute stays hidden until it is opened. According to MDN's explanation of interest invokers, when the user shows interest in an element with interestfor by hovering over it, focusing it, or long-pressing it, the browser opens the popover.

In the test environment, I measured three times how long it took to open after the mouse was placed over the button. The results were 502 to 505 milliseconds. The time from moving the mouse off the button to the popover closing was 252 to 254 milliseconds. When I placed the mouse on the button and removed it after 300 milliseconds, the popover did not open and no events fired. Simply passing the mouse over the button does not open it.

Animation of a button and a popover. At the top, a label such as "Operation 1 of 3: hover over the button" turns pink each time the operation changes. An arrow cursor moves onto the Help button, and about 0.5 seconds later a popover with a note that unsaved input is lost on close opens below the button. The status label changes to "opened" and turns pink. In the second operation, the cursor moves onto the popover and it stays open. In the third operation, the cursor leaves the popover, and about 0.25 seconds later it closes. The status label changes to "closed" and turns pink. The labels in this image are in Japanese.
The popover opens and closes through three operations: hovering over the button, moving onto the popover, and moving off it

Keyboard operation worked the same way. When I focused the button with the Tab key, the popover opened after 503 milliseconds. When I moved focus to the next button with Tab, it closed after 252 milliseconds. Pressing the Esc key while the popover was open closed only the popover and left focus on the button. When it was opened by the mouse, Esc also closed it, and even when the mouse stayed over the button for another second, it did not reopen.

Use position-area to Show It Just Below the Button

With the HTML above alone, the popover appears in the middle of the screen, away from the button. On the 800×600 test screen, the top-left corner of the popover appeared at 373, 283.

To show it directly below the button, specify position-area on the popover.

css
#tip {
  position-area: bottom;
  margin: 4px;
}

According to MDN's explanation of the Popover API, when a popover is linked to the element that opens it, that element becomes the implicit anchor. For this reason, the position is determined by position-area alone, without writing anchor-name. The same applies when linking with interestfor. In the test environment, the popover appeared with its top edge at 68px, against a button whose bottom edge was at 64px.

The Popover Stays Open When the Mouse Moves onto It

When I moved the mouse from the button onto the popover, the popover stayed open. It did not close even when I kept the mouse on it for 1.5 seconds, and no loseinterest event fired. When I moved the mouse off the popover, it closed after 252 milliseconds.

If there is a gap between the button and the popover, the result depends on how quickly the mouse crosses the gap. When I crossed a 40px gap in 150 milliseconds, the popover stayed open. When I paused for 400 milliseconds in the gap before moving on, it closed before the mouse reached it. Place the button and the popover close together.

Change the Delay with interest-delay

To change the delay before opening and the delay before closing, set interest-delay on the side that has interestfor, such as the button. With one value, it applies to both opening and closing. With two values, the first is the time before opening and the second is the time before closing. To change only one of them, use interest-delay-start and interest-delay-end.

css
button[interestfor] {
  interest-delay: 1s 2s;
}

The results I measured in the test environment are as follows.

Setting Time to open Time to close
Not set (normal) 502 to 505ms 252 to 254ms
interest-delay: 0s 17ms 2ms
interest-delay: 0.1s 103ms 102ms
interest-delay-start: 200ms 218ms 251ms
interest-delay: 1s 2s 1002ms 2002ms

The initial value is normal, and getComputedStyle() also returned normal. MDN states that the delay for normal can differ between browsers. The values of about 0.5 seconds and about 0.25 seconds above are for Chromium 153. Negative values cannot be written: CSS.supports('interest-delay-start', '-1s') returned false.

If a long delay is set on buttons that are lined up together, you have to wait each time you move to the next button. Adding the selector that combines :has() and :interest-source, shown in MDN's example, removes the delay on the other buttons while one of them is open. The value 1s 0.25s below is the one I used in testing.

css
button {
  interest-delay: 1s 0.25s;
}

p:has(button:interest-source) button {
  interest-delay-start: 0s;
}

In the test environment, I linked a separate popover to Button 1 and Button 2, both placed inside a <p>. When I hovered over Button 1 to open its popover and then moved to Button 2, without the :has() rule it took 1004 milliseconds for Button 2's popover to open. With the :has() rule, it opened in 3 milliseconds, and Button 1's popover closed at the same time.

Change the Appearance Only While Open

:interest-source matches the element with interestfor while interest is being shown, and :interest-target matches the popover specified by interestfor during the same period.

css
button:interest-source {
  background: #d85878;
}

#tip:interest-target {
  border-style: dashed;
}

Both selectors matched not at the moment the mouse hovered, but when the popover opened after the delay had passed. At 200 milliseconds after hovering, neither matched, and the button background kept its initial color. 100 milliseconds after leaving, before the popover had closed, they still matched, and they stopped matching once it closed.

With popover="hint", an Open Menu Does Not Close

The value of the popover attribute matters when used alongside other popovers. A popover with no value is treated the same as auto. MDN's explanation of the Popover API says that opening a hint popover does not close an auto popover.

In the test environment, I kept a menu opened by a click (popover) open and then moved the mouse onto the neighboring Help button. When the hint was popover="hint", both the menu and the hint were shown. When the hint was popover (auto), the menu closed at the same time as the hint opened.

Two screens stacked vertically. The top one, "hint is popover=\"hint\"", shows both the menu contents and the help explanation. The bottom one, "hint is popover (auto)", shows only the help explanation and the menu is closed. In both, the arrow cursor is on the Help button. The labels in this image are in Japanese.
Results of hovering over the Help button while the menu was open, comparing hint and auto

You can also put both commandfor and interestfor on the same button, to open a popover on click and another on hover.

html
<button interestfor="tip" commandfor="box" command="toggle-popover">設定</button>
<p id="tip" popover="hint">ヒント</p>
<div id="box" popover>設定の箱</div>

In the test environment, after the hint opened on hover, clicking the button fired loseinterest, closed the hint, and opened the box. When I then moved the mouse off and back on, with the hint set to hint, the hint opened while the box stayed open. With auto, the box closed when the hint opened. On a button or link with only interestfor, clicking did not close the hint, and the link navigated to its destination as usual.

Change the Content with JavaScript

When a popover opens and closes, the interest and loseinterest events fire on the popover (MDN's explanation of the interest event (English)). The source property of the event holds the button or link the mouse is on.

js
const tip = document.getElementById("tip");

for (const link of document.querySelectorAll("a")) {
  link.interestForElement = tip;
}

tip.addEventListener("interest", (e) => {
  tip.textContent = `${e.source.textContent} の説明`;
});

interestForElement is a property that sets the interestfor target from JavaScript. In the test environment, when I set the same popover on two links, it opened whichever link I hovered over. The content changed to match the link being hovered, showing the text "Link 1" or "Link 2" followed by the suffix from the code. After the property was set, the interestfor attribute became an empty string.

When preventDefault() was called in the interest event handler, the popover did not open. The event's cancelable property was true.

Supported Elements and Browsers

According to MDN, interestfor can be used on <a>, <button>, <area>, and SVG <a> elements. In the test environment, it worked with <button> and with <a> that has an href. It did not work with a <span> that has tabindex, or with an <a> that has no href.

While the hint was open, I used the Chrome DevTools Protocol (Accessibility.getFullAXTree) to get Chromium's accessibility tree. The description of the Help button contained the hint's text.

According to the compatibility data, each browser supports the feature from the following versions.

Browser interestfor, interest-delay, etc. popover="hint"
Chrome, Edge 142 151 (133 to 150 use the older spec)
Chrome on Android 142 151 (133 to 150 use the older spec)
Firefox Not supported 153 (149 to 152 use the older spec)
Safari Not supported Technology Preview
Safari on iOS Not supported Not supported

MDN's example says that in browsers without support, a link with interestfor still works as an ordinary link. Do not put information that is only available in the hint. Make sure the same information is also available in the page body or at the link destination.

To check support, you can test for interestForElement, as in MDN's example. In the test environment, this returned true.

js
const supported = Object.hasOwn(
  HTMLButtonElement.prototype,
  "interestForElement",
);

​Posted by 86