Dofactory.com
Dofactory.com

HTML <li> hidden Attribute

A hidden attribute on an <li> tag hides that the item.

Although the list item is not visible, its position on the page is maintained.

Example

#

A hidden attribute on an <li> element.
The first list element (Designers) is not visible. Note the second list item starts at number 1

  1. Developers
  2. Managers
<ol>
  <li hidden>Designers</li>
  <li>Developers</li>
  <li>Managers</li>
</ol>

Using hidden

The hidden attribute hides the <li> element.

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

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

Removing the hidden attribute makes it re-appear.


Syntax

<li hidden>
or
<li hidden="hidden">

Values

#

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

More Examples

Clicking the button will toggle A hidden attribute on the first <li> element.

  1. Designers
  2. Developers
  3. Managers

<ol>
  <li id="myli">Designers</li>
  <li>Developers</li>
  <li>Managers</li>
</ol>

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

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

Code explanation

Initially, all <li> elements have no hidden attribute and are all visible.

Clicking the button calls JavaScript that toggles the hidden attribute.

Notice that the list item numbering also changes accordingly after the first <li> is hidden.


Browser support

Here is when hidden support started for each browser:

Chrome
1.0 Sep 2008
Firefox
1.0 Sep 2002
IE/Edge
1.0 Aug 1995
Opera
1.0 Jan 2006
Safari
1.0 Jan 2003

You may also like

 Back to <li>

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


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


Guides