<nav> or <menu> (HTML5)
Asked Answered
S

5

109

W3Schools.com and I'm pretty sure I remember seeing W3C.org state that <menu> should be used for Toolbar menus and listing form control commands.

So, which one should I use for my main menu? Nav, or Menu? Does it matter?

Sigmon answered 11/2, 2011 at 13:55 Comment(2)
You may want to read w3fools.Guenther
What is a 'form control command'?Trapp
G
173

nav is used for groups of internal links (a elements). Generally this means the links should travel to separate pages, or change content in the case of an AJAX page. Expect some sort of content change when clicking on a nav item.

menu is used for groups of controls (a, input, button). Generally this means the inputs should perform a function within the page. Expect some sort of javascript interaction when clicking on a menu item.

nav: the navigation for the site.

menu: the menu for a web application.

Guenther answered 11/2, 2011 at 16:22 Comment(2)
Interesting fact: The spec allows to nest <li> elements directly under a <menu> tag, without the need for a <ul> list. The requirement is that the <menu>'s type attribute is omitted or set to "toolbar". See the specification for the <li> element and the <menu> element.Acquaint
Can someone point me to <menu> in one of the W3C HTML5 specs? I'm finding it in the WHAT WG version, but not in the latest W3C HTML 5.3 draft.Wentz
J
11

Here's an HTML5Doctor post on nav with a section on how it's different from menu (basically, use it in actual apps). Looks like you want nav.

Jer answered 11/2, 2011 at 16:5 Comment(0)
K
2

With the <menu> it's usually used for context menus.

MDN has good documentation on it: https://developer.mozilla.org/en-US/docs/Web/HTML/Element/menu

Keener answered 1/10, 2013 at 1:48 Comment(1)
This is not valid anymore. Quote from the link in your answer: Note: In early versions of the HTML specification, the <menu> element had an additional use case as a context menu. This functionality is considered obsolete and is not in the specification.Mogerly
C
2

The best place to get this answer is the HTML 5 standards them self.

Menu is defined in HTML 5.1 2nd Edition.

It, "... represents a group of commands."


Nav is defined in HTML 5.

It is, "... a section of a page that links to other pages or to parts within the page: a section with navigation links."

There are notes for nav that I didn't include but think are important, but I think its best for you to get the definition yourself from the standards.

The definitions marked in this post as the answer are close to correct but have extraneous statements in them that make the answer itself wrong.

Crusade answered 26/12, 2018 at 23:47 Comment(0)
A
0

<nav> is a section (like <section> or <article>) so it will appear in your HTML document outline. Due to this, I would use <menu> 99% of the time. To me, <nav> is a mix between <section> and <menu>, with the added constriction that <nav> should be used specifically for navigation, whereas <menu> can be for anything that might be called a menu (including a navigation bar). So most navigation bars, despite the fact that they are navigation bars, do not fit the strict requirements of <nav>, and <menu> is more appropriate.

I have never in my web development career found a situation where I wanted my navigation bar to be part of the document outline.

Adiell answered 16/4, 2014 at 22:0 Comment(3)
This is incorrect. You should rarely (if ever) use <menu> on a website that is not a web app - 99% of the time, your navigation 'menu' will be static links to other pages or other sections on the page, which is what <nav> is for, according to the spec.Burgenland
@IvanDurst, my point is that if you are building simply a list of static links, nav is not a good tag choice because nav is a section element so it will become part of the document outline as if it's a section with a header and content. This is in fact a rare scenario where you want your nav to act like a section. menu was just a suggestion as a semantic replacement. I don't see anything in the spec saying it can't be used in this way. Seems to fit. But if you for some reason believe that menu is only for what you define as a web app, then I recommend ol or ul as a replacement.Adiell
According to the compatibility table here, <menu> still has poor support at this time. (Is it really true that Chrome has no support for it??) Also, you can achieve the exact same semantics by providing the role="menu" attribute to another element (e.g. a div or a nav). The role attribute overrides the default semantics for any element.Molehill

© 2022 - 2024 — McMap. All rights reserved.