日本語

Place a Menu Next to a Button with CSS Anchor Positioning

​​CSS​HTML,Playwright,Popover API

CSS anchor positioning lets you place a menu to the right of a button, with its top edge aligned to the button's top edge. You can also write CSS-only rules that flip the menu to the left of the button when there is not enough room on the right side of the screen. This approach works for an action menu ("Copy", "Delete", and so on) opened from a "…" button in a list row, and for a submenu (flyout menu) that opens sideways from an item in a menu.

Note

I tested on Ubuntu 24.04, Node.js 22.23.4, Playwright 1.63.0, and headless Chromium 153. I opened the menu in a 480px × 400px viewport and measured the positions of the button and the menu with getBoundingClientRect(). I did not test Firefox, Safari, real smartphones, right-to-left languages, or vertical writing pages. The submenu does not specify position-anchor, but I did not test older browser versions where the initial value of position-anchor is different. I did not build keyboard operation for the menu, such as moving between items with the arrow keys.

Link the button and the menu by name, and place the menu to the right of the button

To open and close the menu, use the Popover API. Set the id of the menu as the popovertarget of the button.

html
<button id="menu-button" type="button" popovertarget="menu">メニュー</button>
<div id="menu" popover>
  <button type="button">コピー</button>
  <button type="button">名前を変更</button>
  <button type="button">削除</button>
</div>

Decide the position in CSS. Give the button a name with anchor-name, and write the same name in the menu's position-anchor. The menu is then placed relative to the button. Names start with --.

css
#menu-button {
  anchor-name: --menu-button;
}

#menu {
  position-anchor: --menu-button;
  position-area: right span-bottom;
  margin: 0 0 0 4px;
  width: 140px;
  padding: 4px;
  border: 1px solid #ccc;
}

#menu button {
  display: block;
  width: 100%;
  height: 32px;
}

In my test, I placed the button with position: absolute and gave it a width of 100px and a height of 36px. The outer size of the menu is 150px wide (width of 140px plus padding and border) and 106px high.

When I placed the button 40px from the left and 40px from the top and opened the menu, the menu appeared 144px from the left and 40px from the top. It sat to the right of the button's right edge (140px) with a 4px gap, and its top edge was aligned with the top edge of the button.

With a button and menu linked by popovertarget, the menu appeared at the same position even when I removed position-anchor. According to MDN's explanation of anchoring a popover to a button, the button that opens a popover becomes the popover's implicit anchor, and it is used with position-area even without anchor-name. In this article, I write the name explicitly so that the reader can see what the position is based on just by reading the CSS.

Use the span keyword in position-area to set the vertical position

position-area is a property that decides where the menu goes, using a 3×3 grid centered on the button. right span-bottom means using the middle and bottom cells of the right column.

Changing the direction of span- changed the vertical position of the menu as follows. The button spans from 40px to 76px from the top.

position-area Menu top Menu bottom Appearance
right span-bottom 40px 146px Top aligned with the top of the button, extends downward
right 5px 111px Vertical center aligned with the button
right span-top 0px 106px Extends upward, but stops at the top of the screen

With right span-top, the menu extended upward, but it did not fit above the top of the screen, so its top settled at 0px. Its bottom (106px) did not line up with the bottom of the button (76px). right span-bottom, which aligns the top edges and extends downward, is the easiest placement to work with for an action menu.

Create the gap with margin

Create the gap between the button and the menu with the menu's margin. Without a margin, the left edge of the menu was at 140px, touching the right edge of the button. In the example above, I set the left margin to 4px.

This margin also takes effect when the menu is flipped to the left in the next section.

Flip the menu to the left with flip-inline when it does not fit on the right

When I placed the button near the right edge of the screen (300px to 400px from the left), there was no room to place the menu (150px outer width) on the right with a 4px gap. The menu shifted left to stay inside the screen and overlapped the button.

css
#menu {
  /* Add to the rules above */
  position-try-fallbacks: flip-inline;
}

position-try-fallbacks is a property that lists alternative positions to try when the first position would extend past the screen. flip-inline is a value that tries the position with the left and right sides swapped.

Button position (left/right) position-try-fallbacks Menu position (left/right) Appearance
300px to 400px none 330px to 480px Pushed to the right edge of the screen and overlaps the button
300px to 400px flip-inline 146px to 296px Placed to the left of the button with a 4px gap

When flipped to the left, the right edge of the menu was at 296px, leaving a 4px gap from the left edge of the button (300px). The margin written on the left side was used as the right-side margin after the flip. MDN's explanation of position fallbacks also gives an example: an element placed 10px away from its anchor is still 10px away after the flip.

Clicking the Menu button opens the menu to the right of the button. The button then slowly moves to the right, and the menu moves with it. When the button gets close to the right edge of the screen, the menu moves to the left of the button. The label in the box below changes from "Menu: right of button" to "Menu: left of button" and turns pink. The labels in the image are in Japanese.
After opening the menu, moving the button to the right makes the menu move to the left of the button once it no longer fits on the right. The bar at the top, the cursor circle, and the box below were added for the recording. The button is moved by a script for the recording. The labels in the image are in Japanese.

Near the bottom of the screen, align with the bottom edge of the button

When I placed the button near the bottom of the screen (300px to 336px from the top), the menu extending downward did not fit above the bottom edge (400px). With flip-inline alone, the menu shifted up by 6px to fit on the screen, and its top (294px) no longer lined up with the top of the button (300px).

I also added a vertical flip (flip-block) and a combined flip of both axes (flip-block flip-inline).

css
#menu {
  position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;
}

I tested this CSS with the button placed at the upper right, lower left, and lower right. In every case, the menu appeared beside the button, with either its top or its bottom aligned with the button.

Button position (left/right, top/bottom) position-try-fallbacks Menu position (left/right, top/bottom) Appearance
Upper right (300px to 400px, 40px to 76px) the three above 146px to 296px, 40px to 146px Appears on the left, top edges aligned
Lower left (40px to 140px, 300px to 336px) flip-inline 144px to 294px, 294px to 400px Appears on the right, but shifted up by 6px
Lower left (40px to 140px, 300px to 336px) the three above 144px to 294px, 230px to 336px Appears on the right, bottom edges aligned
Lower right (300px to 400px, 300px to 336px) flip-inline only, flip-block only, or flip-inline, flip-block 330px to 480px, 294px to 400px Overlaps the button
Lower right (300px to 400px, 300px to 336px) the three above 146px to 296px, 230px to 336px Appears on the left, bottom edges aligned

At the lower right corner, flip-inline alone, flip-block alone, and both listed together all placed the menu at the same position, 330px to 480px horizontally and 294px to 400px vertically, where it overlapped the button. MDN's explanation says that if every listed position would extend past the screen, the first specified position is used. The measured position was not right at the button's lower right corner, but pushed inside the screen. When I added flip-block flip-inline, which flips both axes, as the third option, the menu appeared to the left of the button with its bottom edge aligned with the bottom of the button.

With all of the above, the menu CSS looks like this.

css
#menu-button {
  anchor-name: --menu-button;
}

#menu {
  position-anchor: --menu-button;
  position-area: right span-bottom;
  position-try-fallbacks: flip-inline, flip-block, flip-block flip-inline;
  margin: 0 0 0 4px;
  width: 140px;
  padding: 4px;
  border: 1px solid #ccc;
}

#menu button {
  display: block;
  width: 100%;
  height: 32px;
}

I also checked what happens when the page is scrolled vertically with the menu open. When the top of the button moved from 200px to 100px to 20px, the top of the menu moved from 200px to 100px to 20px as well, following the button.

Submenus open sideways with the same approach

You can use the same approach for a submenu that opens sideways from an item in a menu. Place the submenu inside the parent menu, and set the id of the submenu as the popovertarget of the item's button.

html
<div id="menu" popover>
  <button type="button">コピー</button>
  <button type="button">名前を変更</button>
  <button id="share-button" type="button" popovertarget="share">共有</button>
  <button type="button">削除</button>
  <div id="share" popover>
    <button type="button">リンクをコピー</button>
    <button type="button">メールで送る</button>
  </div>
</div>
css
#share {
  position-area: right span-bottom;
  margin: 0 0 0 4px;
  width: 140px;
  padding: 4px;
  border: 1px solid #ccc;
  position-try-fallbacks: flip-inline;
}

#share button {
  display: block;
  width: 100%;
  height: 32px;
}

I did not write anchor-name or position-anchor for the submenu. The Share button becomes the implicit anchor of the submenu linked by popovertarget, so the submenu opened to the right of the Share button with its top edge aligned.

A popover without a value (the auto state) normally allows only one open popover at a time, and opening another one closes the previous one. According to MDN's explanation of nested popovers, there are exceptions: a popover placed inside a parent, or one opened from a button inside a parent, can stay open together with the parent. In my test, when I pressed the Share button to open the submenu, the parent menu stayed open.

I also checked how the menus close. Pressing the Esc key once closed only the submenu, and pressing it again closed the parent menu too. When both were open and I clicked outside the menu, both closed.

In the top screenshot, the menu opens to the right of the Menu button, which is on the left side of the screen, and the submenu (with the Copy link and Send by email items) opens to the right of the Share button. In the bottom screenshot, the button is on the right side of the screen, and the submenu opens to the left of the Share button, beside the left side of the parent menu. The labels in the image are in Japanese.
Screenshots of the submenu. The top shows the button on the left side, and the bottom shows it on the right side. The bar at the top and the box at the bottom were added for the screenshot. The labels in the image are in Japanese.

When I placed the button 160px from the left, the right edge of the Share button was at 409px, leaving no room on the right for the submenu (150px outer width). The submenu flipped to the left with flip-inline and opened at 115px to 265px. The left edge of the parent menu is at 264px, so the submenu overlaps the parent's border by 1px where they sit side by side.

Supported browsers

Here are the desktop versions that support each feature, according to @mdn/browser-compat-data (8.1.8).

Feature Chrome/Edge Firefox Safari
popover attribute 114 125 17
anchor-name 125 147 26
position-anchor 125 (partial before 151) 147 (partial before 151) 26 (partial before 27)
position-try-fallbacks 128 147 26
position-area 129 147 26

The partial-support note for position-anchor describes differences in the initial value. The initial value should be normal, but in Chrome and Edge it was implicit in 125 to 126, auto in 127 to 143, and none in 144 to 150. In Firefox 147 to 150 and Safari 26 to 26.6, it is auto.

​Posted by 86