Home
  • Page one
  • P" /> Home
  • Page one
  • P" /> Home
  • Page one
  • P"/>

    Is it correct to use the <menu> element as navigation?

    255 views Asked by At

    Is it correct to use the <menu> tag as navigation? Like that:

    <menu>
        <li><a href="#">Home</a></li>
        <li><a href="#">Page one</a></li>
        <li><a href="#">Page two</a></li>
        <li><a href="#">Page three</a></li>
    </menu>
    

    Or is it indicated only for a Context menu, like the example on Mozilla?

    2

    There are 2 answers

    2
    tacoshy On BEST ANSWER

    In your exmaple you are using the <menu> tag correctly and as matter of fact that is the way you should actually code a navigation list.
    The <menu> tag is a sementic alternative to the unsemantic <ul> as is treated by the browser as the same.

    To quote MDN WebDocs:

    The <menu> and <ul> elements both represent an unordered list of items. The key difference is that <ul> primarily contains items for display, while <menu> was intended for interactive items.

    <a>nchors are interactive elements and as such the <menu> should be correctly used as a list container in that case.

    However, your wording is incorrect. <menu> is only a container for the unordered list of anchors but not an entire navigation for which the <nav> element should be used.

    As example a navigation bar could contain more then just links:

    <nav>
      <img src="logo.jpg" alt="Website Logo">
      <h1>table of contents</h1>
      <menu>
        <li><a href="#chapter-1">Chapter 1</a></li>
        <li><a href="#chapter-2">Chapter 2</a></li>
        <li><a href="#chapter-3">Chapter 3</a></li>
      </menu>
      <input type="text" id="search-bar">
    </nav>
    
    0
    Sean On

    No. According to the spec:

    The <menu> element represents a toolbar consisting of its contents, in the form of an unordered list of items (represented by li elements), each of which represents a command that the user can perform or activate.

    It's semantically appropriate for toolbars. One example of a toolbar is a rich text formatting menu, where users can press a "bold" button to activate bold styling on their text.

    Page navigation should be marked up using a <nav> element. If your navigation is a list of links, use a <ul> or <ol> inside that <nav> as appropriate, but not a <menu>.