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 <main> hidden Attribute

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

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

Example

#

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

A hidden main element:

Impressionism is a 19th-century art movement characterized by relatively small brush strokes, emphasis on accurate depiction of light, ordinary subject matter, and unusual visual angles. Impressionism originated with a group of Paris-based artists whose independent exhibitions brought them to prominence in the 1870s and 1880s.
<p>A hidden main element:</p>

<main hidden>
  <b>Impressionism</b> is a 19th-century art movement 
  characterized by relatively small brush strokes, 
  emphasis on accurate depiction of light, ordinary
  subject  matter, and unusual visual angles. 
  Impressionism originated  with a group of 
  Paris-based artists whose independent exhibitions 
  brought them to prominence in the 1870s and 1880s.
</main>

Using hidden

The hidden attribute hides the <main> element.

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

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

Removing the hidden attribute makes it re-appear.


Syntax

<main hidden>
or
<main 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 <main> tag.

Impressionism is a 19th-century art movement characterized by relatively small brush strokes, emphasis on accurate depiction of light, ordinary subject matter, and unusual visual angles. Impressionism originated with a group of Paris-based artists whose independent exhibitions brought them to prominence in the 1870s and 1880s.

<main id="mymain">
  <b>Impressionism</b> is a 19th-century art movement 
  characterized by relatively small brush strokes, 
  emphasis on accurate depiction of light, ordinary
  subject  matter, and unusual visual angles. 
  Impressionism originated  with a group of 
  Paris-based artists whose independent exhibitions 
  brought them to prominence in the 1870s and 1880s.
</main>

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

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

Code explanation

Initially, the <main> element has no hidden attribute and can be seen.

Clicking the button calls JavaScript that toggles the hidden attribute.

When the hidden attribute is present, the <main> element will not be visible.


Browser support

Here is when hidden support started for each browser:

Chrome
6.0 Sep 2010
Firefox
4.0 Mar 2011
IE/Edge
12.0 Jul 2015
Opera
11.1 Mar 2011
Safari
5.0 Jun 2010

You may also like

 Back to <main>

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