I have a situation where I need physically separate a caption from the associated figure, e.g. this sort of markup:
<figure>
<img src="…" alt="…"/>
</figure>
…later in the code…
<figcaption>
Caption text
<figcaption>
This isn't semantically correct (there are existing SO questions addressing it), and it produces a validation error. Given that, I'm wondering what the appropriate way to represent this would be (considering markup and accessibility).
Ideally, there would be some sort of attribute (like the label elements for attribute) that would let you associate a figcaption with a figure. Currently I am using an aria-describedby attribute (see below), but I'm not sure if this is accurate either.
<figure aria-describedby="caption">
<img src="…" alt="…"/>
</figure>
…later in the code…
<div id="caption">
Caption text
<div>
There are a couple of ways you could approach this, either by adding a role and using
aria-labelledby, or with JavaScript.Method 1 - HTML Only
The
aria-labelledbyattribute is only recognized on form controls (e.g. buttons, inputs, checkboxes, etc.) or on elements that have aroleattribute assigned. Without either of these conditions, the accessible name computation will not be calculated on the element.In my testing on NVDA/Chrome this works as long as the
altattribute on the image is not empty. I would strongly recommend testing this using different browsers and screen-readers before deploying to a production environment for the reason that it's essentially alabelon a non-interactive element.Method 2 - JavaScript and CSS
This method is more inline with your original question. It produces a non-visual
figcaptionelement as a child of thefigureelement.