Let’s take one interactive feature apart so the markup and behavior make sense separately.
What You Need to Know
Anchor positioning is a CSS technique for placing a popup near the element that opened it. HTML supplies the popover and its trigger; layout rules determine the position.
Try the Example
Take a look at the example. We’ll explain the important lines right below it.
<button type="button" id="help-trigger" popovertarget="help-popover">Help</button>
<div id="help-popover" popover="auto">
<p>Anchor positioning can keep help close to its trigger.</p>
</div>
<style>
#help-trigger { anchor-name: --help-anchor; }
#help-popover { position-anchor: --help-anchor; position-area: bottom; }
</style>
Let’s Read the Code
- popover: Popover lets supported browsers manage showing and hiding a small overlay.
- <button>: The button offers an action; its type or command determines what happens.
- <style>: Style contains CSS rules for the page.
- type: Type changes what sort of control or resource the browser uses.
What the example shows: CSS anchor positioning can place a popover relative to the button that opens it in supporting browsers.
What Happens in the Browser?
When this example includes JavaScript, it supplies behavior that ordinary HTML does not automatically provide. Always think about a fallback.
Start with native behavior. Details, dialog, and popover each solve a different interface problem. Choose the simplest control that actually matches what a visitor wants to do, then test focus and dismissal as well as appearance.
Your Turn
Remove the CSS rule and compare where the popover appears.
Common Beginner Mistakes
- Assuming new HTML syntax works in every browser without testing.
- Anchor positioning depends on CSS support and fallback placement; it is not an automatic effect of popovertarget.
Key Takeaways
- Anchor positioning is a CSS technique for placing a popup near the element that opened it.
- popover: Popover lets supported browsers manage showing and hiding a small overlay.
- Test the behavior you expect rather than trusting how the snippet looks in your editor.