How to make SVGs and other images WCAG compliant - hackforla/website GitHub Wiki
Using the alt property in img HTML tags or Accessible Rich Internet Applications (ARIA) labels with Scalable Vector Graphics (SVGs) and other images is important so that all images on the Hack for LA website are Web Content Accessibility Guidelines (WCAG) compliant. This pages explains how the Website Team deals with SVGs and other images to make them WCAG compliant.
- Any new images added to the website repo, which are added to /assets/images directory or in a subdirectory of it
- Images already in /assets/images directory or in a subdirectory of it
- For SVGs, see the pros and cons detailed in The quick way: img element section in MDN Web Docs Adding vector graphics to the web.
<img class="header-hero-image" src="/assets/images/toolkit/toolkit-hero.svg">
In an img HTML tag,
- The image needs to be in the /assets/images directory or be in a subdirectory of it
- Set
srcto the path to the SVG or image - Add an
altattribute with an appropriate descriptive text. (Depending on the image, thealtattribute will either be an emptyaltattribute (alt="") or will have an appropriate descriptive text. You can use this alt Decision Tree to help you decide what thealtattribute should contain.)
<img class="header-hero-image" src="/assets/images/toolkit/toolkit-hero.svg" alt="" >
- Any existing images in the _includes in the website repo
- For SVGs, see the pros and cons detailed in How to include SVG code inside your HTML section in MDN Web Docs Adding vector graphics to the web. This section discusses inline SVG, which is what this method does using Jekyll's include tag to insert an SVG into a HTML file and the result of this is an inline SVG when the site is generated.
<div>
{% include svg/logo-hfla-small.svg %}
</div>
In an div HTML tag,
- Add a
role="img" - Add an
aria-labelwith an appropriate descriptive text.
<div role="img" aria-label="Hack for LA">
{% include svg/logo-hfla-small.svg %}
</div>
Review the w3.org document about accessible names and descriptions here.
Naming:
- Naming with child content.
- Naming with a string attribute via aria-label.
- Naming by referencing content with aria-labelledby.
- Naming form controls with the label element.
- Naming fieldsets with the legend element.
- Naming tables and figures with captions.
- Fallback names derived from titles and placeholders.
Describing:
- Describing by referencing content with aria-describedby.
- Describing tables and figures with captions.
- Descriptions derived from titles.