I have the following code:
<h2>
Deadpool
<svg width="53" height="64" class="inline" aria-label="and">
<use href="#oleo-ampersand"></use>
</svg>
Wolverine:<br>
A StackOverflow Example
</h2>
The ampersand is stylized as follows:
Now, I want screen readers to narrate it as a regular phrase (i.e.“Deadpool and Wolverine”). Adding aria-label="and"
to the <svg>
seems to be ignored. Is there a way to achieve this? Maybe, I should make the whole block aria-hidden
and add a visually hidden element instead?
Also, it would be cool to make the block being copied the same way on Ctrl+C
, but I guess it’s not possible without a JS intercepting.
3
Answers
It is indeed not trivial to copy the text without JS.
Does this markup work for you on the screen reader?
You may also apply the
.sr-only
class only to a hidden<span>
with the text content "and".If your SVG doesn’t contain any text elements you may even omit the
aria-hidden
attribute.To avoid browser inconsistencies when copying the text we can:
aria-hidden="true"
to the SVG element (containing the ampersand)<span>
You as well use a span with a subset version of the desired font applied (1. example). In this case you would need to apply
aria-hidden="true"
and make it unselectable viauser-select:none
The commonly used
sr-only
rules ensure the text is still readable by screen-readers and selectable – although invisible for sighted users.The main trick of this technique is to make the element invisible by multiple properties without applying
disply:none
orvisibility:hidden
as both properties would remove the content from the accessibility-tree.See also "The A11Y Project: Hide content"
SVG to font conversion
If your inline SVG graphic is not available as a font – you may also use a SVG-to-font-generator like icomoon or fontello to convert your assets to an icon font.
Subsetting
If your font service doesn’t support subsetting you may also convert it with a service like transfonter. It also has an option to generate base64 encoded dataURLs of the font resource.
Yes, remove the svg from the accessibility tree and add the word "and" as visually hidden text.
Also, notice that I removed the
br
element after "Wolverine" and instead wrapped they first line of content in a span and gave it a display of block to get the line break you want. In browse mode, some screen readers will announce the heading as if it were two separate headings if you use thebr
element to force a line break, so this is a courtesy to prevent that.