(as a reply to: https://css-tricks.com/svg-use-with-external-reference-take-2/)
While I love SVG (sprites) there are a lot of issues to take into account.
UPDATE: you should take a look at https://dl.dropboxusercontent.com/u/145744/accessible-svg-icon/test.html which seems a simpler and more robust approach
Another thing: since people copy-paste our code examples it would be great if we could advocate the most robust and accessible markup IMO. I am no a11y expert, but in my understanding we could/should take some extra steps to make out SVG sprite icons more accessible.
Below my current thinking, I would love an a11y expert's advice on this!
First: make a distinction between 'presentational' and 'content' icons. Just as we would with img and their alt attributes.
For all SVG Icon sprites (in sprite.svg):
- Add a
titleelement child to the sprite'ssymbol - Add a
descelement child to the sprite'ssymbol
<symbol id="left-arrow">
<title>Back</title>
<desc>An arrow pointing left</desc>
<path etc.../>
</symbol>
Now, for useing those symbols as purely presentational icons:
- Use
role="presentation"on the SVG
<svg role="presentation">
<use xlink:href="sprite.svg#left-arrow"/>
</svg>
That's it. But for SVG icons as 'content' we should do more:
- Add
role="img" - Add a
titleattribute - Add a
titleelement with uniqueid - Add
aria-labelledby="title-id"
<svg role="img" title="Back" aria-labelledby="title-back1">
<title id="title-back1">Back</title>
<use xlink:href="sprite.svg#left-arrow"/>
</svg>
This ensures the best accessibility AFAIK. Again: I am no a11y expert, so please correct me when I'm mistaken.
Thoughts?
- Github: @davidhund
- Twitter: @valuedstandards
I've done some reading (of the ARIA spec re. role="img" and accessible text computation), and I looked more closely of the test results for VoiceOver, and barring further results I'd recommend this:
<svg aria-hidden="true"><use xlink:href="#symbol-id"></use></svg><svg role="img" aria-label="Text alternative"><use xlink:href="#symbol-id"></use></svg>(aria-labelledbyreferencing an element in the document is also an option, but it's harder to manage so I would only use it as an exception for very specific content, long alternatives, etc. Not sure it would be useful for icons.)I would not bother with adding
<title>elements, let alonearia-labelledbyattributes on the symbols themselves, but if people want a belt-and-braces approach, that's still an option. The accessible texts in the specific instances an icon is used should have priority over the default text alternative in the sprite (says the ARIA spec).