The problem: two APIs, one uniqueness constraint

Wiring a single popover tooltip to its trigger is straightforward. Doing it many times on one page is not, because both the Popover API and CSS Anchor Positioning lean on unique identifiers to connect elements. Rendered from a loop — a WordPress query is the example here, though any loop creates the same conditions — those identifiers must be generated per iteration rather than hardcoded.

  • A popover needs an ID and an accessible role.
  • A popovertarget attribute must point at that same ID, and IDs must be unique for semantics as well as for making the hookup work.
  • The trigger needs an anchor-name.
  • The target element must reference that anchor-name, and each name has to be unique so a target attaches to the correct anchor.

In WordPress the starting point is a query returning an array of page objects:

$property_query = new WP_Query(array(
  'post_type' => 'page',
  'post_status' => 'publish',
  'posts_per_page' => -1, // Query them all!
  'orderby' => 'title',
  'order' => "ASC"
));

The markup we want per item

The intended shape of a single page object inside its container, with the tooltip expanded down to the interior of the <details> element that represents one page:

<div class="almanac-group">
  <div class="group-letter"><a href="#">A</a></div>
  <div class="group-list">
    <details id="" class="group-item">
      <summary>
        <h2><code>accent-color</code></h2>
      </summary>
    </details>

    <!-- Repeat for all properties -->
  </div>
</div>

<!-- Repeat for the entire alphabet -->
<details id="page" class="group-item">
  <summary>
    <h2><code>accent-color</code></h2>

    <span id="tooltip" class="tooltip">
      <!-- Popover Target and Anchor -->
      <button class="info-tip-button" aria-labelledby="experimental-label" popovertarget="experimental-label">  
        <!-- etc. -->
      </button>

      <!-- Popover and Anchor Target -->
      <div popover id="experimental-label" class="info-tip-content" role="tooltip">
        Experimental feature
      </div>
    </span>
  </summary>
</details>

At this stage a <button> is connected to a <div popover>, and clicking the button toggles the div. A small amount of CSS is worth adding just to reset the popover so it does not render with a border in the center of the viewport; Michelle Barker’s article on progressively enhanced popover toggletips is the reference for doing this in a way that degrades well.

.info-tip {
  position: relative; /* Sets containment */

  /* Bail if Anchor Positioning is not supported */
  [popovertarget] {
    display: none;
  }

  /* Style things up if Anchor Positioning is supported */
  @supports (anchor-name: --infotip) {

    [popovertarget] {
      display: inline;
      position: relative;
    }

    [popover] {
      border: 0; /* Removes default border */
      margin: 0; /* Resets placement */
      position: absolute; /* Required */
  }
}

This is also the point to note browser support: the source advises using Chrome, as Safari and Firefox were still working on the feature.

Attaching the tooltip with anchor positioning

Getting the tooltip to sit beside its button is where anchor positioning takes over. The mechanism: register the <button> as the anchor via anchor-name, attach the <div popover> to it with position-anchor, then place it using the anchor() function on its inset properties. Juan Diego’s anchor positioning guide covers the topic in depth.

.tooltip {
  position: relative; /* Sets containment */

  /* Bail if Anchor Positioning is not supported */
  [popovertarget] {
    display: none;
  }

  /* Style things up if Anchor Positioning is supported */
  @supports (anchor-name: --tooltip) {

    [popovertarget] {
      anchor-name: --tooltip;
      display: inline;
      position: relative;
    }

    [popover] {
      border: 0; /* Removes default border */
      margin: 0; /* Resets placement */
      position: absolute; /* Required */
      position-anchor: --tooltip;
      top: anchor(--tooltip -15%);
      left: anchor(--tooltip 110%);
    }
  }
}

One tooltip behaves correctly. The trouble starts when more are added: with static identifiers, every button ends up culling the same tooltip.

Generating unique identifiers from the loop

Reduced to the relevant spot, the HTML looks like this, with the popover owning an ID of #experimental-label that the trigger references in popovertarget — which inadvertently connects the other tooltips on the page too:

<details>
  <!-- ...  -->

    <!-- Popover Target and Anchor -->
    <button class="info-tip-button" aria-labelledby="experimental-label" popovertarget="experimental-label">  
      <!-- ... -->
    </button>

    <!-- Popover and Anchor Target -->
    <div popover id="experimental-label" class="info-tip-content" role="tooltip">
      Experimental feature
    </div>
    
    <!-- ... -->

</details>

A sequence would be better:

<!-- Popover and Anchor Target -->
<div popover id="experimental-label-1" class="info-tip-content" role="tooltip"> ... </div>
<div popover id="experimental-label-2" class="info-tip-content" role="tooltip"> ... </div>
<div popover id="experimental-label-3" class="info-tip-content" role="tooltip"> ... </div>
<!-- and so on... -->

The page query becomes a callable function:

function letterOutput($letter, $propertyID) {
  $property_query = new WP_Query(array(
    'post_type' => 'page',
    'post_status' => 'publish',
    'posts_per_page' => -1, // Query them all!
    'orderby' => 'title',
    'order' => "ASC"
  ));
}

It takes two arguments specific to the site in question. The pages are structured as Almanac → Type → Letter → Feature (e.g., Almanac → Properties → A → accent-color), and the function outputs the child pages of a “Letter” — A → accent-color, anchor-name, and so on. A child page may be an experimental CSS feature, which the UI marks with a tooltip next to each such feature.

The output HTML goes into an object the function returns, shortened here for brevity:

$html .= '<details id="page" class="group-item">';
$html .=   '<summary>';
$html .=     '<h2><code>accent-color</code></h2>';
$html .=     '<span id="tooltip" class="tooltip">';
$html .=       '<button class="info-tip-button" aria-labelledby="experimental-label" popovertarget="experimental-label">  ';
// ...
$html .=       '</button>';
$html .=       '<div popover id="experimental-label" class="info-tip-content" role="tooltip">';
// ...
$html .=       '</div>';
$html .=     '</span>';
$html .=   '</summary>';
$html .= '</details>';

return $html;

WordPress supplies the looping helpers. the_title() replaces the hardcoded post title:

$html .= '<h2><code>' . get_the_title(); . '</code></h2>';

get_the_id() provides the post’s unique identifier, which can give each <details> element its own ID:

$html .= '<details id="page-' . get_the_id(); . '" class="group-item">';

That identifier is the key to unique popover hooks:

// Outputs something like `id="experimental-label-12345"`
$html .= '<div popover id="experimental-label-' . get_the_id(); . '" class="info-tip-content" role="tooltip">';

The same treatment applies to the <button>, so each button is wired to the right popover:

$html .= '<button class="info-tip-button" aria-labelledby="experimental-label-' . get_the_id(); . '" popovertarget="experimental-label">  ';

And to the .tooltip element, to distinguish one from another:

$html .= '<span id="tooltip-' . get_the_id(); . '" class="tooltip">';

A simplified CodePen with similar markup stands in for a real WordPress instance. The popovers toggle, but both targets remain attached to the same anchor element, so clicking either button appears to trigger the same popover.

The CSS side and the compromise

The anchors need the equivalent of unique IDs, as dashed-idents:

/* First tooltip */
#info-tip-1 {
  [popovertarget] {
    anchor-name: --infotip-1;
  }

  [popover] {
    position-anchor: --infotip-1;
    top: anchor(--infotip-1 -15%);
    left: anchor(--infotip-1 100%);
  }
}

/* Second tooltip */
#info-tip-2 {
  [popovertarget] {
    anchor-name: --infotip-1;
  }

  [popover] {
    position-anchor: --infotip-1;
    top: anchor(--infotip-1 -15%);
    left: anchor(--infotip-1 100%);
  }
}

/* Rest of tooltips... */

This is where a trade-off was unavoidable. An @for loop in Sass could generate the identifiers, at the cost of a new dependency. Alternatively, a <style> tag dropped directly into the WordPress template could reuse the same functions to emit the same post identifiers, at the cost of maintaining styles in PHP.

The second option was chosen, so the dashed-idents match the IDs set on the .tooltip and popover. It is not pretty, but it works:

$html .= '
<style>
  #info-tip-' . get_the_id() . ' {
    [popovertarget] {
      anchor-name: --infotip-' . get_the_id() . ';
    }

    [popover] {
      position-anchor: --infotip-' . get_the_id() . ';
      top: anchor(--infotip-' . get_the_id() . ' -15%);
      left: anchor(--infotip-' . get_the_id() . ' 100%);
    }
  }
</style>'

That completes the core implementation. For the specific use case, one addition remained: a conditional that outputs the tooltip only when the item is marked “Experimental Feature” in the CMS.

The cleaner path that isn’t ready

A better route exists, just not yet shipped. Bramus has proposed an ident() function that, once official, would generate a series of dashed idents for naming things like these anchors while preventing the names from colliding.

<div class="group-list">
  <details id="item-1" class="group-item">...</details>
  <details id="item-2" class="group-item">...</details>
  <details id="item-3" class="group-item">...</details>
  <details id="item-4" class="group-item">...</details>
  <details id="item-5" class="group-item">...</details>
  <!-- etc. -->
</div>
/* Hypothetical example — does not work! */
.group-item { 
  anchor-name: ident("--infotip-" attr(id) "-anchor");
  /* --infotip-item-1-anchor, --infotip-item-2-anchor, etc. */
}

Until that reaches the specifications, generated IDs and template-level CSS remain the practical approach.