Why I can't use role="navigation" attribute in html nav tag?
Asked Answered
C

2

7

My code-snippet is below:-

<nav role="navigation" data-offset-top="100" data-spy="affix" class="navbar navbar-inverse navbar-fixed-top">

When I validate this code, it will give me a warning, but the same code is available on bootstrap documentation.
Here is a screenshot - bootstrap navbar code

Cockalorum answered 13/5, 2016 at 17:20 Comment(1)
Where does it say that you can't? Pretty sure the warning doesn't say that - that's why it's just a warning.Trengganu
A
8

<nav role="navigation"> is redundant, so it's discouraged. During transitional periods for some elements it's encouraged to use both the [role] attribute in addition to the new tag, but only until browsers support the implicit semantics for the new tag.

This is more commonly seen with <main role="main"> which was a later addition to HTML.


In the end, the w3c validator doesn't say you can't use the redundant markup, it just warns you that it's superfluous and unnecessary.

For example, validating:

<!doctype html>
<title>example</title>
<nav role="navigation"></nav>

Produces the following warning:

Warning: Element nav does not need a role attribute.

If it were invalid markup, you'd receive an error.

Alevin answered 13/5, 2016 at 17:24 Comment(1)
Thanks for help. I really appreciate it.Cockalorum
T
1

Because role="navigation" is optional and it is only for user agents that supports ARIA.

In this example, the page includes a top navigation section that does not use the ARIA role of navigation, and a navigation at the bottom of the page that does. The role of navigation is recommended as of September 2014, but at some future point in time may not be needed.

Example: https://awkawk.github.io/using_nav.html

Treva answered 13/5, 2016 at 17:26 Comment(1)
please summarize the example in your answerDesmond

© 2022 - 2024 — McMap. All rights reserved.