{"id":381364,"date":"2024-10-02T09:26:48","date_gmt":"2024-10-02T15:26:48","guid":{"rendered":"https:\/\/css-tricks.com\/?p=381364"},"modified":"2024-10-07T07:30:54","modified_gmt":"2024-10-07T13:30:54","slug":"css-anchor-positioning-guide","status":"publish","type":"post","link":"https:\/\/css-tricks.com\/css-anchor-positioning-guide\/","title":{"rendered":"CSS Anchor Positioning Guide"},"content":{"rendered":"\n
\n
\n

Not long ago, if we wanted a tooltip or popover positioned on top of another element, we would have to set our tooltip’s position to something other than static<\/code> and use its inset\/transform properties to place it exactly<\/em> where we want. This works, but the element’s position is susceptible to user scrolls, zooming, or animations since the tooltip could overflow off of the screen or wind up in an awkward position. The only way to solve this was using JavaScript to check whenever the tooltip goes out of bounds so we can correct it… again in JavaScript.<\/p>\n\n\n\n

CSS Anchor Positioning gives us a simple interface to attach elements next to others just by saying which sides to connect \u2014 directly in CSS. It also lets us set a fallback position so that we can avoid the overflow issues we just described. For example, we might set a tooltip element above its anchor but allow it to fold underneath the anchor when it runs out of room to show it above.<\/p>\n\n\n\n

Anchor positioning is different from a lot of other features as far as how quickly it’s gained browser support: its first draft was published on June 2023<\/a> and, just a year later, it was released on Chrome 125<\/a>. To put it into perspective, the first draft specification for CSS variables was published in 2012<\/a>, but it took four years for them to gain wide browser support.<\/p>\n\n\n\n

So, let’s dig in and learn about things like attaching target elements to anchor elements and positioning and sizing them.<\/p>\n<\/div>\n\n\n\n

Quick reference<\/h2>\n\n\n
\/* Define an anchor element *\/\n.anchor {\n  anchor-name: --my-anchor;\n}<\/code><\/pre>\n\n\n\n
\/* Anchor a target element *\/\n.target {\n  position: absolute;\n  position-anchor: --my-anchor;\n}<\/code><\/pre>\n\n\n\n
\/* Position a target element *\/\n.target { \n  position-area: start end;\n}<\/code><\/pre>\n\n\n\n
\n
\n \n Table of contents <\/summary>\n \n\n
    \n
  1. Basics and terminology<\/a><\/li>\n\n\n\n
  2. Attaching targets to anchors<\/a><\/li>\n\n\n\n
  3. Positioning targets<\/a><\/li>\n\n\n\n
  4. Setting fallback positions<\/a><\/li>\n\n\n\n
  5. Custom position and size fallbacks<\/a><\/li>\n\n\n\n
  6. Multiple anchors<\/a><\/li>\n\n\n\n
  7. Accessibility<\/a><\/li>\n\n\n\n
  8. Browser support<\/a><\/li>\n\n\n\n
  9. Spec changes<\/a><\/li>\n\n\n\n
  10. Known bugs<\/a><\/li>\n\n\n\n
  11. Almanac references<\/a><\/li>\n\n\n\n
  12. Further reading<\/a><\/li>\n<\/ol>\n\n\n<\/details><\/div><\/div>\n<\/div>\n<\/div>\n\n\n\n
    <\/div>\n\n\n\n
    \n \n

    Basics and terminology<\/h2>\n <\/summary>\n \n\n

    At its most basic, CSS Anchor Positioning introduces a completely new way of placing elements on the page relative to one another. To make our lives easier, we’re going to use specific names to clarify which element is connecting to which:<\/p>\n\n\n\n