CSS Anchor Positioning and Popover API: Building Tooltips and Dropdowns Without External Libraries

Hello, this is Lovefield.

If you have ever built floating UI components—such as tooltips, dropdowns, or context menus—from scratch, you have likely run into a familiar set of layout headaches.

Tooltips get clipped by a parent container's overflow: hidden, get trapped in z-index wars against other modals, or overflow off the edge of the viewport when the user scrolls or resizes the window. Historically, solving these issues meant pulling in third-party JavaScript positioning libraries like Floating UI (Popper.js) or moving DOM nodes via Vue's <Teleport to="body"> and manually calculating coordinates on every scroll frame.

Today, native web standards solve these problems cleanly out of the box through the combination of the HTML Popover API and CSS Anchor Positioning. In this guide, we will explore how to build resilient floating UIs with zero JavaScript positioning logic.

HTML Popover API: Ending the z-index Wars

The Popover API is a native HTML attribute that promotes an element directly into the browser's Top Layer. Because it renders in the same top-level layer as native <dialog> elements, it is guaranteed to appear above every element on the page—regardless of any z-index: 99999 rules elsewhere in your stylesheet.

Working Out of the Box with Zero JavaScript

Simply link a trigger button's popovertarget attribute to the popover element's id to get automatic toggle behavior:

HTML, XML

1

2

3

4

5

6

7

8

9

10

11

<button popovertarget="user-menu" class="trigger-btn">
  Open Profile Menu
</button>

<div id="user-menu" popover="auto" class="dropdown-panel">
  <ul>
    <li>Edit Profile</li>
    <li>Settings</li>
    <li>Log Out</li>
  </ul>
</div>

Understanding popover Modes

  • popover="auto" (Default): Automatically closes when the user clicks outside the popover (Light Dismiss) or presses the ESC key. Opening a new auto popover also automatically closes any previously open auto popover—making it ideal for dropdowns and menus.

  • popover="manual": Does not close on outside clicks or ESC. It must be dismissed explicitly via a button with popovertargetaction="hide" or by calling .hidePopover() in JavaScript. Best suited for toast notifications or persistent banners.

> Tip: The Popover API handles keyboard focus management and screen reader accessibility semantics natively at the browser level.

CSS Anchor Positioning: Tethering Elements Precisely

While the Popover API promotes an element to the Top Layer, CSS Anchor Positioning tethers that floating element directly to a reference trigger (the anchor) and keeps it aligned automatically.

Connecting anchor-name and position-anchor

Assign a unique dashed ident (--) via anchor-name to the trigger element, then bind the floating element using position-anchor:

CSS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

.trigger-btn {
  anchor-name: --menu-anchor;
}

.dropdown-panel {
  /* Reset default popover agent styles */
  margin: 0;
  border: 1px solid #e2e8f0;
  border-radius: 8px;
  padding: 8px;
  background: #ffffff;
  box-shadow: 0 10px 15px -3px rgba(0, 0, 0, 0.1);

  /* Bind to the anchor and set coordinates */
  position: fixed;
  position-anchor: --menu-anchor;
  
  /* Attach the panel's top edge 8px below the anchor's bottom edge */
  top: calc(anchor(bottom) + 8px);
  /* Align the panel's left edge with the anchor's left edge */
  left: anchor(left);
}

With just these CSS rules, the browser tracks the anchor on the compositor thread during scrolling and resizing with zero layout thrashing or jitter.

Automatic Viewport Edge Flipping: position-try-fallbacks

One of the trickiest edge cases in floating UI design is handling viewport boundaries—for example, when a button sits near the bottom of the screen and opening a dropdown downward causes it to clip outside the viewport. Previously, this required measuring getBoundingClientRect() dynamically.

CSS Anchor Positioning handles automatic flipping natively using position-try-fallbacks.

Leveraging flip-block and flip-inline

CSS

1

2

3

4

5

6

7

8

9

10

11

.dropdown-panel {
  position: fixed;
  position-anchor: --menu-anchor;
  
  /* Default placement: below the anchor, spanning to the right */
  position-area: bottom span-right;
  margin-top: 8px;

  /* Automatically flip vertically or horizontally if space runs out */
  position-try-fallbacks: flip-block, flip-inline, flip-block flip-inline;
}

  • position-area: A concise shorthand property based on a 3×3 grid around the anchor (top, bottom, left, right, span-right, etc.), replacing verbose top/left coordinate math.

  • flip-block: Automatically flips the popover above (top) the anchor if there is insufficient vertical space below.

  • flip-inline: Flips horizontal alignment if the popover overflows the right or left edge of the viewport.

Handling Dynamic Lists in Vue 3

In real-world applications, you often render action menus inside dynamic v-for lists or table rows. Because every trigger needs its own distinct anchor reference to prevent collisions, you can bind unique dashed idents via inline styles:

Vue

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

<script setup lang="ts">
interface Item {
  id: number;
  name: string;
}

const items: Item[] = [
  { id: 1, name: 'Project Alpha' },
  { id: 2, name: 'Project Beta' },
  { id: 3, name: 'Project Gamma' },
];
</script>

<template>
  <ul>
    <li v-for="item in items" :key="item.id" class="list-row">
      <span>{{ item.name }}</span>

      <!-- Assign a unique anchor-name and popovertarget per item ID -->
      <button
        :popovertarget="`action-menu-${item.id}`"
        :style="{ anchorName: `--anchor-${item.id}` }"
      >
        Actions
      </button>

      <div
        :id="`action-menu-${item.id}`"
        popover="auto"
        class="action-popover"
        :style="{ positionAnchor: `--anchor-${item.id}` }"
      >
        <button>Edit</button>
        <button>Delete</button>
      </div>
    </li>
  </ul>
</template>

<style scoped>
.action-popover {
  margin: 0;
  position: fixed;
  position-area: bottom span-left;
  position-try-fallbacks: flip-block, flip-inline;
}
</style>

Notice that you do not need <Teleport> to break out of the component DOM hierarchy, nor do you need reactive state variables like activeMenuId to track which row's menu is currently open.

Browser Support and Progressive Fallbacks

The Popover API enjoys universal support across all modern browsers (Chrome, Edge, Safari, and Firefox). CSS Anchor Positioning is also supported in Chromium-based browsers (Chrome, Edge) and modern Safari.

If you need to support older browser versions gracefully, you can use @supports to provide a clean centered modal fallback:

CSS

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

.dropdown-panel {
  /* Fallback: center safely on screen in unsupported browsers */
  position: fixed;
  inset: 0;
  margin: auto;
  width: fit-content;
  height: fit-content;
}

@supports (anchor-name: --test) {
  .dropdown-panel {
    inset: auto;
    margin: 0;
    position-anchor: --menu-anchor;
    position-area: bottom span-right;
    position-try-fallbacks: flip-block;
  }
}

Summary

For years, frontend developers relied on heavy JavaScript runtime calculations to position overlays and manage stacking contexts. The HTML Popover API and CSS Anchor Positioning shift that responsibility back into the browser's native layout engine where it belongs.

Next time you design a tooltip, dropdown, or context menu, consider reaching for these native web standards before adding another external dependency to your bundle.

This article was generated by AI.

Lovefield

Web Front-End developer

하고싶은게 많고, 나만의 서비스를 만들고 싶은 변태스러운 개발자입니다.

Beginner's Guide to Vitest: Setting Up Frontend Testing and Core Syntax

A step-by-step introduction to Vitest for developers new to frontend testing—covering installation with Bun, core assertions (describe, it, expect), function mocking, and basic Vue 3 component testing.

Java Script

v5.0