Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Published
2 min read
Up to date

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Native Popover Element with HTML

Create overlays and dropdowns easily with the native HTML popover API

Building overlay UI elements like tooltips and dropdowns has always required complex JavaScript libraries. The new HTML Popover API changes this with native browser support for floating elements.

What is Lorem Ipsum?

Lorem Ipsum is simply dummy text of the printing and typesetting industry.

The Popover API introduces a new popover attribute that transforms any HTML element into a floating overlay.

The browser handles all the complex positioning, stacking order, and focus management automatically. This means we can create interactive UI elements without wrestling with z-index, click-outside handling, or managing focus states.

We can control popovers through two modes: auto and manual

Auto popovers dismiss themselves when clicking outside or when another auto popover opens, perfect for simple tooltips or dropdowns. Manual popovers give us more control over their behavior, useful for complex dialogs or multi-step flows.

The JavaScript API provides three methods to control popovers programmatically: showPopover(), hidePopover(), and togglePopover()

These methods return promises, allowing us to react to state changes and coordinate animations. The API also provides the :popover-open CSS pseudo-class for styling popovers based on their visibility state.

The Popover API represents a significant step forward in web platform capabilities. Instead of relying on complex positioning libraries or managing overlay states manually, we now have a native solution that handles these challenges elegantly. This means simpler code, better performance, and improved accessibility out of the box.

Browser support for the Popover API is growing. Chrome already supports it, and Safari has implemented the feature behind a flag. As adoption increases, we can expect this native solution to become the standard way of creating overlay UI elements on the web.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
4 min read

Mental Toughness is the Best Quality a Developer Can Have

Mental toughness gets developers through challenges like debugging, picking up new tools, and hitting tight deadlines. It’s about staying calm and pushing through when things get tough.

Sep 12, 2024
Read article
Webdev
5 min read

WebAssembly (Wasm): When (and When Not) to Use It

Understanding the real use cases for WebAssembly beyond the performance hype

Nov 25, 2024
Read article
Webdev
3 min read

CSS :has() - The Parent Selector We've Always Wanted

Transform your CSS with :has(), the game-changing selector that finally lets us style elements based on their children.

Dec 4, 2024
Read article
Webdev
3 min read

Preloading Responsive Images

How to properly preload responsive images to improve initial page load

Nov 28, 2024
Read article
Webdev
3 min read

CSS ::target-text for Text Highlighting

A look at how browsers can highlight text fragments using CSS ::target-text, making text sharing and navigation more user-friendly

Dec 17, 2024
Read article
Webdev
4 min read

Speed Up Your Website With rel='preconnect' and increase PageSpeed Insights Score

Using link rel='preconnect' can improve your website's performance by reducing connection setup times to key external domains.

Sep 13, 2024
Read article
Webdev
3 min read

HTML Details Element: The Native Accordion You're Not Using

Discover how the HTML details element can replace your JavaScript accordions and why it might be better than your current solution

Dec 10, 2024
Read article
Webdev
2 min read

link rel='modulepreload': Optimize JavaScript Module Loading

The rel='modulepreload' indicates that a module script should be fetched, parsed, and compiled preemptively, and stored for later execution

Dec 4, 2024
Read article
Webdev
3 min read

CSS @supports: Write Future-Proof CSS

Detect CSS feature support and provide smart fallbacks with @supports

Dec 6, 2024
Read article

Become a better engineer

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Many companies have a fixed annual stipend per engineer (e.g. $2,000) for use towards learning resources. If your company offers this stipend, you can forward them your invoices directly for reimbursement. By using my affiliate links, you support my work and get a discount at the same!


This article was originally published on https://www.trevorlasn.com/blog/native-popover-with-html. It was written by a human and polished using grammar tools for clarity.