Dofactory.com
Dofactory.com
Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

HTML <span> hidden Attribute

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

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

Example

#

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

The Great Pyramid of Giza was the tallest building in the world for over 3,800 years, until the construction of the Lincoln Cathedral in the UK in 1311.

<article>
  <p style="max-width:610px;">
    <span hidden style="background:moccasin">Did you know?</span>
    The Great Pyramid of Giza was the tallest building in the world
    for over 3,800 years, until the construction of the Lincoln 
    Cathedral in the UK in 1311.
  </p>
</article>

Using hidden

The hidden attribute hides the <span> element.

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

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

Removing the hidden attribute makes it re-appear.


Syntax

<span hidden>
or
<span 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 <span> element.

Did you know? The Great Pyramid of Giza was the tallest building in the world for over 3,800 years, until the construction of the Lincoln Cathedral in the UK in 1311.


<article>
  <p style="max-width:610px;">
    Did you know? The Great Pyramid of Giza 
    was the tallest building in the world for
    <span id="myspan" style="background:moccasin">over 3,800 years</span>, 
    until the construction of the Lincoln Cathedral in the UK in 1311.
  </p>
</article>

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

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

Code explanation

Initially, the <span> is visible.

Clicking the button calls JavaScript that toggles the hidden attribute.

With the hidden attribute the span element is invisible.


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 <span>

Last updated on Sep 30, 2023

Earn income with your HTML skills
Sign up and we'll send you the best freelance opportunities straight to your inbox.
We're building the largest freelancing marketplace for people like you.
By adding your name & email you agree to our terms, privacy and cookie policies.

Guides