Lesson 14 of 30
Tooltips, menus, and lightweight dialogs used to need a JS library just for layering above everything and closing on an outside click. The popover attribute gives you both natively, for free.
12 min read
Every option explains itself — why the right one is right, and why each wrong one is wrong. Miss one and it comes back later on a spaced-repetition schedule.
Question 1 of 3
A team needs a confirmation prompt that blocks all interaction with the rest of the page until the user responds. Should they use `popover` or `<dialog>`?
Hints without spoilers, and a plain explanation of why a wrong answer was wrong — answered only from this lesson. Free account, no card.
Sign up freeWhat interviewers actually ask about this, and what a strong answer has to cover.
A strong answer covers
Likely follow-ups
Non-modal floating UI — a dropdown menu, a tooltip, a lightweight "more options" panel — has always needed the same handful of things: render above everything else on the page (no fighting z-index against unrelated components), close automatically when the user clicks outside it, and toggle open/closed from a trigger. None of that is complicated individually, but getting all of it right together, across a whole app, is exactly why teams reach for a UI library instead of hand-rolling it per component.
The popover HTML attribute plus the popovertarget attribute (put on the triggering button) give you this natively, no JavaScript library required:
popover to any element and the browser promotes it to the top layer — a rendering layer above the normal document, so it's guaranteed to render above everything else without a single z-index declaration.popovertarget="id-of-the-popover" to a <button> and clicking it toggles that popover open and closed automatically.<button popovertarget="user-menu">Account</button>
<div id="user-menu" popover>
<a href="/profile">Profile</a>
<a href="/settings">Settings</a>
<button popovertarget="user-menu" popovertargetaction="hide">Close</button>
</div>
[popover] {
border: 1px solid var(--color-border);
border-radius: 0.5rem;
padding: 0.75rem;
}
Clicking the "Account" button opens #user-menu in the top layer — it renders above the page's normal stacking context automatically. Clicking anywhere outside the menu closes it without any JavaScript. This is the class of UI that used to require a library specifically for the positioning/layering/dismiss logic; here that logic is the browser's job, not the app's.
popover is for non-modal UI; <dialog> (with .showModal()) is for modal, blocking UI. A popover doesn't block interaction with the rest of the page or trap focus the way a modal dialog does — reach for <dialog> when the UI genuinely needs to block the page behind it (a confirmation prompt), and popover for the lighter-weight case (a menu, tooltip, non-blocking panel).z-index alone often can't — a popover inside a container with overflow: hidden or its own stacking context would normally get clipped or trapped behind it; the top layer sidesteps that entirely.popovertargetaction controls the specific action ("show", "hide", or the default "toggle") — without it, every popovertarget button toggles, which is usually fine for an open/close trigger but not always what you want for a dedicated "close" button inside the popover."How would you build a dropdown menu that closes when you click outside it, without a UI library?" is a real question that checks whether a candidate knows the native popover attribute exists and what specific plumbing (layering, light-dismiss) it removes the need to hand-roll.