|
| 1 | +# SVGs must have an accessible name (`github/a11y-svg-has-accessible-name`) |
| 2 | + |
| 3 | +💼 This rule is enabled in the ⚛️ `react` config. |
| 4 | + |
| 5 | +<!-- end auto-generated rule header --> |
| 6 | + |
| 7 | +## Rule Details |
| 8 | + |
| 9 | +An `<svg>` must have an accessible name. Set `aria-label` or `aria-labelledby`, or nest a `<title>` element as the first child of the `<svg>` element. |
| 10 | + |
| 11 | +However, if the `<svg>` is purely decorative, hide it with `aria-hidden="true"` or `role="presentation"`. |
| 12 | + |
| 13 | +## Resources |
| 14 | + |
| 15 | +- [Accessible SVGs](https://css-tricks.com/accessible-svgs/) |
| 16 | + |
| 17 | +## Examples |
| 18 | + |
| 19 | +### **Incorrect** code for this rule 👎 |
| 20 | + |
| 21 | +```html |
| 22 | +<svg height='100' width='100'> |
| 23 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 24 | +</svg> |
| 25 | +``` |
| 26 | + |
| 27 | +```html |
| 28 | +<svg height='100' width='100' title='Circle with a black outline and red fill'> |
| 29 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 30 | +</svg> |
| 31 | +``` |
| 32 | + |
| 33 | +```html |
| 34 | +<svg height='100' width='100'> |
| 35 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 36 | + <title>Circle with a black outline and red fill</title> |
| 37 | +</svg> |
| 38 | +``` |
| 39 | + |
| 40 | +### **Correct** code for this rule 👍 |
| 41 | + |
| 42 | +```html |
| 43 | +<svg height='100' width='100'> |
| 44 | + <title>Circle with a black outline and red fill</title> |
| 45 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 46 | +</svg> |
| 47 | +``` |
| 48 | + |
| 49 | +```html |
| 50 | +<svg aria-label='Circle with a black outline and red fill' height='100' width='100'> |
| 51 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 52 | +</svg> |
| 53 | +``` |
| 54 | + |
| 55 | +```html |
| 56 | +<svg aria-labelledby='circle_text' height='100' width='100'> |
| 57 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 58 | +</svg> |
| 59 | +``` |
| 60 | + |
| 61 | +```html |
| 62 | +<svg aria-hidden='true' height='100' width='100'> |
| 63 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 64 | +</svg> |
| 65 | +``` |
| 66 | + |
| 67 | +```html |
| 68 | +<svg role='presentation' height='100' width='100'> |
| 69 | + <circle cx='50' cy='50' r='40' stroke='black' stroke-width='3' fill='red'/> |
| 70 | +</svg> |
| 71 | +``` |
| 72 | + |
| 73 | +## Version |
0 commit comments