Dofactory.com
Dofactory.com

HTML <pre> hidden Attribute

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

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

Example

#

A hidden attribute on a <pre> element.
The pre-formatted text is not visible.

A hidden pre element

<p>A hidden pre element</p>

<pre hidden>
  SELECT FirstName, LastName
  FROM Customer ORDER BY LastName
</pre>

Using hidden

The hidden attribute hides the <pre> element.

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

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

Removing the hidden attribute makes it re-appear.


Syntax

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

  SELECT FirstName, LastName
  FROM Customer ORDER BY LastName

<pre id="mypre" style="background:ghostwhite;">
  SELECT FirstName, LastName
  FROM Customer ORDER BY LastName
</pre>

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

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

Code explanation

Initially, the <pre> is visible.

Clicking the button calls JavaScript which toggles A hidden attribute on the <pre> element.

With the hidden attribute the <pre> 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 <pre>

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


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


Guides