How To Create Context Menus That Always Display Inside The Viewport Using Only CSS
Make sure your menu is always visible without complex JavaScript
Jun 28, 2023
If you have ever tried to build an adaptive context menu you know how challenging this can be.
You need to display the menu next to an element, let’s say on the right of it. But if the element itself is close to the right edge of the screen the menu may be cut off so you need to display it on the left side. The same goes when you display it on any other side: there’s always the possibility that (part of) the menu is outside of the viewport when it’s displayed.
So you need to check the position and size of the menu each time it’s displayed so you can make sure it’s never outside the viewport. In addition to that, you also need to implement event handlers for when the user resizes the screen or changes the orientation of the device. When that happens, you need to get the size of the viewport again to make sure the menu is not outside of it when it’s shown.
This requires some complex JavaScript and repeated querying of the size and position of the menu, so this is definitely not something you can implement with CSS only.
Until now…
Using the Popover API and CSS Anchor Positioning you can now make sure the menu is always correctly displayed inside the viewport using only CSS, no JavaScript needed.
Popover API
In addition to the <dialog> element we can now use the Popover API to display an element on top of everything else in the so-called top-layer of the browser window.
The nice part about this API is that a popover can be defined entirely declaratively in HTML and CSS so no JavaScript is needed. To define an element as a popover you simply add the popover attribute:
A popover is hidden by default and can be toggled by a <button> that has a popovertarget attribute that corresponds to the id of the popover element:
Now when you click the button repeatedly, the popover will be shown and hidden.
The popover attribute is short for popover="auto". In this state, the popover can be closed by clicking outside it or pressing the Esc key and only one popover can be shown at a time (unless popovers are nested).
The button now toggles the popover but you can also define a button that either only shows or hides the button using the popovertargetaction attribute:
Here’s a codepen to demonstrate this:
You will notice that by default, the popover is displayed in the center of the viewport. Of course, you can position the popover using CSS but that doesn’t solve the problem we discussed earlier: we still need JavaScript to make sure we always position the tooltip fully inside the viewport.
This is where CSS anchor positioning comes in.
Tethering elements together using only CSS
Anchor positioning enables developers to tether elements together using only CSS in an adaptive way, meaning that the anchored element (tooltip, context menu etc.) will remain visible even when the user scrolls or resizes the viewport.
💡 Currently, anchor positioning is only supported in Chrome Canary behind the “Experimental Web Platform Features” flag. To enable that flag, open Chrome Canary and visit
chrome://flags. Then enable the "Experimental web platform features" flag and restart.
To anchor an element to another you need to give the anchored element the anchor attribute with a value of the id of the anchor element. To anchor the tooltip to the button from the previous examples:
<button popovertarget="tooltip" id="tooltip-button">?</button>
<div id="tooltip" popover anchor="tooltip-button">
I am a tooltip
</div>
You can also define the anchor in CSS using the anchor-name property which accepts a "dashed-ident" value. We can replace the anchor="tooltip-button" from the previous example with:
#tooltip {
anchor-name: --tooltip-button;
}
💡 Note that the value is now --tooltip-button instead of tooltip-button
Since the anchored element is a popover it will be displayed in the center of the viewport. To position it we will use the anchor function. It takes three arguments:
- Anchor element: the value of
anchor-nameof the anchor. When the anchor was defined with theanchorattribute this may be omitted. - Anchor side: the position keyword, either
top,right,bottomorleftor a percentage (e.g. 50% would be equal tocenter). - Fallback: an optional fallback that accepts a length or percentage.
You then use the anchor function as the value for the top, right, bottom or left properties of the anchored element.
For example, if you want to display the anchored element on the right of the anchor you would position the left side of the anchored element on the right side of the anchor:
#tooltip {
left: anchor(right);
}
// or, if the anchor doesn't have an "anchor" attribute:
#tooltip {
left: anchor(--tooltip-button, right);
}
Similarly, you can align the top of the anchored element with the top of the anchor:
#tooltip {
left: anchor(right); // left of anchor aligned with right of button
top: anchor(top); // top of anchor aligned with top of button
}
This will enable you to position an element without any JavaScript but it still won’t prevent that the tooltip may end up outside the viewport.
This is where position-fallback comes in.
Adaptive positioning
position-fallback requires developers to define a set of fallback positions that the browser will go through to find a set that positions the anchored element fully inside the viewport. If it cannot find one it will default to the first set.
These sets are defined with the @position-fallback keyword, a name for the sets in "dashed-ident" format, and each set wrapped inside a @try block. Let's go back to the previous example of the tooltip positioned on the right of the anchor and say we now want to position a context menu on the right.
We will name the sets --clockwise and add the first set that positions the menu on the right:
@position-fallback --clockwise {
// right side, top aligned
@try {
left: anchor(right);
top: anchor(top);
}
}
If the anchor is positioned all the way on the right side of the viewport, the menu will need to be positioned on the left of the anchor. This will be the next set:
@position-fallback --clockwise {
// right side, top aligned
@try {
left: anchor(right);
top: anchor(top);
}
// left side, top aligned
@try {
right: anchor(left);
top: anchor(top);
}
}
Now what if the anchor is not only positioned all the way on the right side of the viewport but also at the bottom of it? In that case, the menu would need to be positioned on the left again but now the bottom of it will need to be aligned with the bottom of the anchor so it won’t be cut off by the viewport at the bottom.
Let’s add a third set for that:
@position-fallback --clockwise {
// right side, top aligned
@try {
left: anchor(right);
top: anchor(top);
}
// left side, top aligned
@try {
right: anchor(left);
top: anchor(top);
}
// left side, bottom aligned
@try {
right: anchor(left);
bottom: anchor(bottom);
}
}
Finally, the anchor could be positioned on the left side and at the bottom of the viewport. In that case, the menu will need to be positioned on the right and the bottom of the menu will need to be aligned with the bottom of the anchor again. This is the final set we need to add:
@position-fallback --clockwise {
// right side, top aligned
@try {
left: anchor(right);
top: anchor(top);
}
// left side, top aligned
@try {
right: anchor(left);
top: anchor(top);
}
// left side, bottom aligned
@try {
right: anchor(left);
bottom: anchor(bottom);
}
// right side, bottom aligned
@try {
left: anchor(right);
bottom: anchor(bottom);
}
}
With all position sets in place, we now apply them to the menu like this:
#menu {
position-fallback: --clockwise;
}
Now, whenever the menu is shown, the browser will go through the positions we defined and use the first one that makes sure the menu stays entirely inside the viewport.
No JavaScript needed, brilliant! 🎉
Here’s a codepen that demonstrates fallback positioning. Use the radiobuttons to place the button in any corner of the viewport:
Join Modern Web Weekly, my weekly newsletter on the modern web platform, web components and Progressive Web Apps.