Archived topic
Improving Accessibility Scores of Simple Mega Menu
13 replies · Started by Michael on March 11, 2021
I have incorporated the simple mega menu as described here and began to check the accessibility using Site Improve. It's telling me I'm missing alternative text for what looks like the down/up arrows used for the mega menu. As the arrows are functional and not purely decorative, it something I'd like to fix. Is there a way to address this issue on the mega menu? I've included a snapshot of the report in the private info area. Thanks much.
Hi there,
You can try swapping icons from fonts to SVG through Appearance > Customize > General so you don't have to bother about FOUT/FOIT from fonts since SVGs are going to be served instead of fonts of chevron icons.
Hi Elvin,
Interesting.
My setting in Appearance > Customize > General was set to SVG and using WAVE I received the following results:
- Errors: 0
- Contrast Errors: 0
- Alerts: 18
- Features: 10
- Structural Elements: 84
- ARIA: 190
By changing from SVG to fonts I received the following results:
- Errors: 0
- Contrast Errors: 1
- Alerts: 18
- Features: 10
- Structural Elements: 84
- ARIA: 74
The only significant change was the ARIA result. Admittedly I don't know as much as I should regarding accessibility so I'm not sure which is best. The site looks visually better using SVG.
Thanks again.
Ah right sorry my answer was incomplete.
You can filter these icons to add in your attributes to adhere to the accessibility recommendations.
You can use generate_svg_icon for this.
Example: modifying the svg code to add role="presentation" for both close and hamburger icon.
add_filter( 'generate_svg_icon', function( $output, $icon ) {
if ( 'menu-bars' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M0 96c0-13.255 10.745-24 24-24h464c13.255 0 24 10.745 24 24s-10.745 24-24 24H24c-13.255 0-24-10.745-24-24zm0 160c0-13.255 10.745-24 24-24h464c13.255 0 24 10.745 24 24s-10.745 24-24 24H24c-13.255 0-24-10.745-24-24zm0 160c0-13.255 10.745-24 24-24h464c13.255 0 24 10.745 24 24s-10.745 24-24 24H24c-13.255 0-24-10.745-24-24z" />
</svg>';
}
if ( 'close' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M71.029 71.029c9.373-9.372 24.569-9.372 33.942 0L256 222.059l151.029-151.03c9.373-9.372 24.569-9.372 33.942 0 9.372 9.373 9.372 24.569 0 33.942L289.941 256l151.03 151.029c9.372 9.373 9.372 24.569 0 33.942-9.373 9.372-24.569 9.372-33.942 0L256 289.941l-151.029 151.03c-9.373 9.372-24.569 9.372-33.942 0-9.372-9.373-9.372-24.569 0-33.942L222.059 256 71.029 104.971c-9.372-9.373-9.372-24.569 0-33.942z" />
</svg>';
}
return $output;
}, 15, 2 );
I'm not sure how WAVE reads this but it's worth giving a try.
Thanks Elvin. I'll give it a go and let you know in a bit.
Mike
No problem. Let us know how it goes.
Hi Elvin - So I'm confused as to how I target the up/down arrow in the mega-menu like you targeted the hamburger in the if statement. When I inspect the arrow itself I can see that the span has a role of presentation but the svg has the img role. If I want to change the role of the svg what would the if statement look like?
<span role="presentation" class="dropdown-menu-toggle" aria-expanded="false">
<span class="gp-icon icon-arrow">
<svg viewBox="0 0 330 512" aria-hidden="true" role="img" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em"><path d="M305.913 197.085c0 2.266-1.133 4.815-2.833 6.514L171.087 335.593c-1.7 1.7-4.249 2.832-6.515 2.832s-4.815-1.133-6.515-2.832L26.064 203.599c-1.7-1.7-2.832-4.248-2.832-6.514s1.132-4.816 2.832-6.515l14.162-14.163c1.7-1.699 3.966-2.832 6.515-2.832 2.266 0 4.815 1.133 6.515 2.832l111.316 111.317 111.316-111.317c1.7-1.699 4.249-2.832 6.515-2.832s4.815 1.133 6.515 2.832l14.162 14.163c1.7 1.7 2.833 4.249 2.833 6.515z" fill-rule="nonzero"></path></svg>
</span>
</span>
If I can nail this down, most if not all of my accessibility issues have been solved. Maybe I could write a blog post about it as well.
Thanks again.
Hi there,
this PHP Snippet is a filter that includes the two icons Elvin provided, and for the following icons: Search, Categories, Tags, Comments and Arrow:
add_filter( 'generate_svg_icon', function( $output, $icon ) {
if ( 'menu-bars' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M0 96c0-13.255 10.745-24 24-24h464c13.255 0 24 10.745 24 24s-10.745 24-24 24H24c-13.255 0-24-10.745-24-24zm0 160c0-13.255 10.745-24 24-24h464c13.255 0 24 10.745 24 24s-10.745 24-24 24H24c-13.255 0-24-10.745-24-24zm0 160c0-13.255 10.745-24 24-24h464c13.255 0 24 10.745 24 24s-10.745 24-24 24H24c-13.255 0-24-10.745-24-24z" />
</svg>';
}
if ( 'close' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M71.029 71.029c9.373-9.372 24.569-9.372 33.942 0L256 222.059l151.029-151.03c9.373-9.372 24.569-9.372 33.942 0 9.372 9.373 9.372 24.569 0 33.942L289.941 256l151.03 151.029c9.372 9.373 9.372 24.569 0 33.942-9.373 9.372-24.569 9.372-33.942 0L256 289.941l-151.029 151.03c-9.373 9.372-24.569 9.372-33.942 0-9.372-9.373-9.372-24.569 0-33.942L222.059 256 71.029 104.971c-9.372-9.373-9.372-24.569 0-33.942z" />
</svg>';
}
if ( 'search' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path fill-rule="evenodd" clip-rule="evenodd" d="M208 48c-88.366 0-160 71.634-160 160s71.634 160 160 160 160-71.634 160-160S296.366 48 208 48zM0 208C0 93.125 93.125 0 208 0s208 93.125 208 208c0 48.741-16.765 93.566-44.843 129.024l133.826 134.018c9.366 9.379 9.355 24.575-.025 33.941-9.379 9.366-24.575 9.355-33.941-.025L337.238 370.987C301.747 399.167 256.839 416 208 416 93.125 416 0 322.875 0 208z"/>
</svg>';
}
if ( 'categories' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M0 112c0-26.51 21.49-48 48-48h110.014a48 48 0 0 1 43.592 27.907l12.349 26.791A16 16 0 0 0 228.486 128H464c26.51 0 48 21.49 48 48v224c0 26.51-21.49 48-48 48H48c-26.51 0-48-21.49-48-48V112z" fill-rule="nonzero"/>
</svg>';
}
if ( 'tags' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M20 39.5c-8.836 0-16 7.163-16 16v176c0 4.243 1.686 8.313 4.687 11.314l224 224c6.248 6.248 16.378 6.248 22.626 0l176-176c6.244-6.244 6.25-16.364.013-22.615l-223.5-224A15.999 15.999 0 0 0 196.5 39.5H20zm56 96c0-13.255 10.745-24 24-24s24 10.745 24 24-10.745 24-24 24-24-10.745-24-24z"/>
<path d="M259.515 43.015c4.686-4.687 12.284-4.687 16.97 0l228 228c4.686 4.686 4.686 12.284 0 16.97l-180 180c-4.686 4.687-12.284 4.687-16.97 0-4.686-4.686-4.686-12.284 0-16.97L479.029 279.5 259.515 59.985c-4.686-4.686-4.686-12.284 0-16.97z" fill-rule="nonzero"/>
</svg>';
}
if ( 'comments' === $icon ) {
$output = '<svg viewBox="0 0 512 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M132.838 329.973a435.298 435.298 0 0 0 16.769-9.004c13.363-7.574 26.587-16.142 37.419-25.507 7.544.597 15.27.925 23.098.925 54.905 0 105.634-15.311 143.285-41.28 23.728-16.365 43.115-37.692 54.155-62.645 54.739 22.205 91.498 63.272 91.498 110.286 0 42.186-29.558 79.498-75.09 102.828 23.46 49.216 75.09 101.709 75.09 101.709s-115.837-38.35-154.424-78.46c-9.956 1.12-20.297 1.758-30.793 1.758-88.727 0-162.927-43.071-181.007-100.61z" fill-rule="nonzero"/>
<path d="M383.371 132.502c0 70.603-82.961 127.787-185.216 127.787-10.496 0-20.837-.639-30.793-1.757-38.587 40.093-154.424 78.429-154.424 78.429s51.63-52.472 75.09-101.67c-45.532-23.321-75.09-60.619-75.09-102.79C12.938 61.9 95.9 4.716 198.155 4.716 300.41 4.715 383.37 61.9 383.37 132.502z" fill-rule="nonzero" />
</svg>';
}
if ( 'arrow' === $icon ) {
$output = '<svg viewBox="0 0 330 512" aria-hidden="true" role="presentation" version="1.1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" width="1em" height="1em">
<path d="M305.913 197.085c0 2.266-1.133 4.815-2.833 6.514L171.087 335.593c-1.7 1.7-4.249 2.832-6.515 2.832s-4.815-1.133-6.515-2.832L26.064 203.599c-1.7-1.7-2.832-4.248-2.832-6.514s1.132-4.816 2.832-6.515l14.162-14.163c1.7-1.699 3.966-2.832 6.515-2.832 2.266 0 4.815 1.133 6.515 2.832l111.316 111.317 111.316-111.317c1.7-1.699 4.249-2.832 6.515-2.832s4.815 1.133 6.515 2.832l14.162 14.163c1.7 1.7 2.833 4.249 2.833 6.515z" fill-rule="nonzero"/>
</svg>';
}
return $output;
}, 15, 2 );
So you can replace what Elvin provided with this code.
Thanks David. I put the code in my functions file and pushed it to my test site. Scores went up slightly but one side effect was that the SVG icons are not visible, like they are transparent. Is there a simple fix to make them display? I put a link to the test site in the private window.
Thanks again for all the help.
The SVGs are actually there but the fill is set to #FFFFFF which is white like its background so it just blends in.
Your child theme actually has one.
change the values to this.
span svg {
fill: black;
}
@media (min-width: 769px){
span svg {
fill: black;
}
}
Change black to your preference.
Gotcha. Makes sense. Thanks Elvin & David. The only thing I see remaining that I would need to address is that of the up arrow when I apply the filter. Without the filter I get the down arrow and when the menu is not expanded and the up arrow when the menu is expanded. With the filter I only see the down arrows. Thank you for working with me on this. It is much appreciated.
Hi there,
Try adding this CSS:
.menu > .mega-menu.sfHover .dropdown-menu-toggle svg {
transform: rotate(180deg);
}
Works great Tom. I can't tell you how much I appreciate GP support. From Elvin, David, Leo and Ying to you Tom on the technical side to Stacey on the billing side. You all have helped me so damn much I can't thank you enough. I look forward to all the great things coming down the line from the GP family.
Thanks, Michael! That's really great to hear. We appreciate it :)