Dofactory.com
Dofactory.com

HTML <nav> hidden Attribute

A hidden attribute on a <nav> tag hides the nav element.

Although the nav is not visible, it maintains its position on the page.

Example

#

A hidden attribute on a <nav> tag.
The nav element is not visible.

<p>A hidden nav element:</p>

<nav hidden>
  <a href="javascript:alert('Going to Introduction')">Introduction</a> <br />
  <a href="javascript:alert('Going to What Is HTML')">What is HTML</a>  <br />
  <a href="javascript:alert('Going to HTML Syntax')">HTML Syntax</a>  <br />
  <a href="javascript:alert('Going to HTML Elements')">HTML Elements</a>
</nav>

Using hidden

The hidden attribute hides the <nav> element.

You can specify either 'hidden' (without value) or 'hidden="hidden"'. Both are valid.

A hidden <nav> element is not visible, but it maintains its position on the page.

Removing the hidden attribute makes it re-appear.


Syntax

<nav hidden>
or
<nav hidden="hidden">

Values

#

Value Description
hidden Use 'hidden' or hidden='hidden'. Both are valid.


More Examples

Clicking the button toggles A hidden attribute on the <nav> element.


<nav id="mynav">
  <a href="javascript:alert('Going to Introduction')">Introduction</a> <br />
  <a href="javascript:alert('Going to What Is HTML')">What is HTML</a>  <br />
  <a href="javascript:alert('Going to HTML Syntax')">HTML Syntax</a>  <br />
  <a href="javascript:alert('Going to HTML Elements')">HTML Elements</a>
</nav>

<br />
<button onclick="toggle(this);">Hide nav</button>

<script>
  let toggle = button => {
    let element = document.getElementById("mynav");
    let hidden = element.getAttribute("hidden");
    
    if (hidden) {
       element.removeAttribute("hidden");
       button.innerText = "Hide nav";
    } else {
       element.setAttribute("hidden", "hidden");
       button.innerText = "Show nav";
    }
  }
</script>

Code explanation

Initially, the <nav> elements has no hidden attribute and can be seen.

Clicking the button calls JavaScript that toggles the hidden attribute.

With the hidden attribute the <nav> is not visible.


Browser support

Here is when hidden support started for each browser:

Chrome
6.0 Sep 2010
Firefox
4.0 Mar 2011
IE/Edge
9.0 Mar 2011
Opera
11.0 Dec 2010
Safari
5.0 Jun 2010

You may also like

 Back to <nav>

Author: Jack Poorte
Published: Jun 20 2021
Last Reviewed: Sep 30 2023


Quick question: what's your favorite/least favorite part of Dofactory?


Guides