Another way to do this is:
<p><span aria-label="something else" role="img">Lorem</span>ipsum blah blah</p>
... NVDA announces this as "graphic something else ipsum blah blah".
The img
role tells the AT not to look inside the element for further accessible names, which is somewhat similar to aria-hidden
so it is especially useful for accessibly labelling 'dingbat' type glyphs and emojis (i.e. 'graphic' icons represented as 'text' characters), or when a sequence of characters may be interpreted (especially by a speech synth) in multiple ways, or not announced at all. This will allow you to lock into a single interpretation.
Example: The single quote character ' might mean minutes or feet, depending on context. NVDA doesn't announce it at all, unless punctuation is enabled in the prefs.
Now, unlike the default semantic-free span, the img
role allows you to treat arbitrary character sequences as a graphic, and use an aria-label
.
A disadvantage is that the role is announced (e.g. as "graphic" or "image"), and the element will appear in any image list UI offered by your AT.
You may or may not be happy with the role announcement. (In this case, it's not a graphic at all). You can tweak that with aria-roledescription
, thus:
<p>You turned over the 5<span aria-label="hearts" role="img" aria-roledescription="suit">♥</span></p>
... NVDA announces this as "You turned over the 5 suit hearts".
Not perfect, but this pins down the glyph as the card suit "hearts", rather than (say) the word "love" (as in "I ♥ NY"). Other special role names may be more appropriate in your situation. Note that empty aria-roledescription
attributes are not valid in ARIA, so you can't do away with them altogether with this technique, and you can't predict exactly how different ATs will insert the role announcement into the speech stream. Therefore, use the aria-roledescription
attribute with extreme care.
And Graham's caveats remain important. The valid use cases for these kinds of approach are where the AT-only content would be superfluous or disruptive for sighted users. These cases are rarer than you might think.
Consider also that some screen reader users are not partially sighted - they may be dyslexic, for example.
Another less-than-obvious point is that not all ATs are screen readers!
Some 'creative' solutions for text variants may work well with screen readers but poorly with (say) Braille devices. This is especially true if you try to 'second guess' how a screen reader will pronounce something, and are tempted to provide some kind of phonetic hack, which Braille users will then struggle to understand.
As a general rule, hiding stuff from ATs or offering stuff only for ATs should be avoided.
If you absolutely must do it, consider dyslexic screen reader users, braille device users, and (preferably) get a second opinion about your implementation from the accessibility community or some representative users, providing enough context (i.e. not just lorem ipsum) to allow your implementation to be judged appropriately.