Position a tooltip below a button with CSS anchor positioning
Hover over Trail grade on a walking guide and a hint appears. It says “Expect two steep climbs.” You’ll use CSS anchor positioning to line up that hint with the button and keep them together when the button moves.
You can follow the same steps in Phoenix Pro and Community (free). The practice page is a single HTML file. Use the desktop app or open Phoenix Code in Chrome or Edge.
Open the trail page in Live Preview
- Download the practice file and put it in a new folder called
trail-tooltip. Keep the filenameindex.html. - Click the folder with a plus at the top left of Phoenix Code. Choose Open Folder… and select
trail-tooltip. If the browser asks to view the files in this folder, choose Allow. - Double-click
index.htmlin the file tree. Look for Pine Ridge loop in Live Preview. If the preview is closed, click the lightning bolt on the right. If prompted for this practice folder, choose Trust & Execute Preview so its tooltip can respond to your mouse and keyboard. - In the preview toolbar, click the small down arrow beside the pen and choose Preview Mode. Hover over Trail grade. The button sits a little to the right, but the hint starts at the left edge of the text above it. You’ll bring those two edges into line.
Attach the tooltip to the button
In index.html, find the comment /* Add the anchor rules here. */ near the end of <style>. Paste this CSS on the next line, before </style>.
#trail-grade {
anchor-name: --trail-grade;
}
@supports (top: anchor(bottom)) {
#grade-tip {
position: absolute;
position-anchor: --trail-grade;
top: anchor(bottom);
left: anchor(left);
}
}
Choose File → Save and hover over the button again. The hint now starts directly below the button’s left edge.
anchor-name gives the button a name, and position-anchor tells the tooltip which name to follow. In the tooltip rule, top: anchor(bottom) puts its top edge at the button’s bottom edge. The two left edges line up because of left: anchor(left). The small space between the button and the dark background comes from padding on the tooltip.
Move the button and check the result
- Find
margin-left: 2remin the button’s original rule. Change2remto4remand save. - Hover over Trail grade again. The button and the hint should both have moved to the right. You can move the pointer onto the hint to keep reading it.
- To check with a keyboard, open the saved
index.htmlin a browser. Press Tab to focus Trail grade and show the hint. Press Escape to dismiss it. The focus outline stays on the button.
Why does the tooltip still appear at the left?
Check that both CSS rules use the same name, including the two leading hyphens in --trail-grade. If a property is missing from the editor’s suggestions, type it in full and try moving the button again.
Open your saved index.html in a current browser and repeat the hover test. If the tip follows the button there but stays at the left in Live Preview, check which version you’re using. The example worked in Phoenix Code 5.5.11 on Ubuntu and in the web editor in Chrome. Browsers that don’t recognize anchor() skip the @supports block and show the hint in its original position. Check browser support for the CSS anchor() function.
