Lets compare the following three code examples:
<nav>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
<li><a href="#"><img src="trigger.png"></a></li>
</ul>
</nav>
Example 1
<nav>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
<a href="#"><img src="trigger.png"></a>
</nav>
Example 2
<nav>
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
</nav>
<a href="#"><img src="trigger.png"></a>
Example 3
Regarding semantics, which one is the most precise - should the menu trigger be included in the list inside nav
, inside nav
but outside the list or even outside nav
?
I'd prefer:
<nav role="navigation">
<ul>
<li><a href="#">Link 1</a></li>
<li><a href="#">Link 2</a></li>
<li><a href="#">Link 3</a></li>
</ul>
<button title="Show / Hide menu"><img src="trigger.png" alt="Show / Hide menu"></button>
</nav>
(note the landmark role and use of a button)
because the trigger will be found by Assistive Technologies (screen readers mainly but not only them) inside the element where they expect to find the main navigation of a website.
[role="navigation"]
will help browsers ant ATs to find without any doubt the navigation. Other landmark roles are banner, contentinfo, search, main, complementary (and other less important to current use)button
should be used instead of link element whenever clicking on the 'link' doesn't jump to another page, anchor and/or if the href
attribut has a value of #
)If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With