Skip to main content

How to Hide an Element with the hidden Attribute in HTML

· 4 min read
Jagdish Kumawat
Founder @ Dewiride

Add hidden to any element and the browser doesn't show it. The element stays in the page's code, so a script can show it later, but until then nobody sees it, including people who use screen readers.

Syntax​

Syntax
<element hidden><content></element>
PartWhat it does
elementThe tag name of any element, such as p or div.
hiddenHides the element. It needs no value.
<content>What's inside the element. It's hidden with it.

MDN's hidden page describes the attribute, which works on every element.

The Example​

The example is an order status page with two messages, Your order is on its way and Your order was cancelled. The second one has hidden, so only the first one shows.

Step 1: Write the Code​

Create a file named index.html in a text editor, such as Visual Studio Code:

index.html, an HTML page with the heading Order status, the paragraph Your order is on its way., and a paragraph with the hidden attribute that says Your order was cancelled.

index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Order status</title>
</head>
<body>
<h1>Order status</h1>
<p>Your order is on its way.</p>
<p hidden>Your order was cancelled.</p>
</body>
</html>

Step 2: Open It in a Browser​

Double-click index.html. It opens in your browser:

The page in Chromium: the heading Order status and the line Your order is on its way. The cancelled message isn't shown.

The screenshot comes from Chromium 159, the open-source browser that Chrome is built on.

What Happened​

  • The cancelled message isn't on the screen: Chromium gave it display: none, so it takes up no space either.
  • It's still in the page: the paragraph and its text were in the page's code, ready to be shown.
  • Screen readers skip it too. Chromium's accessibility tree, the information screen readers use, had the heading and the first message, but nothing from the hidden one.

Common Mistakes​

MistakeWhat happensDo this instead
A CSS rule that sets display on the element, such as p { display: block; }It shows anyway: the CSS rule overrides hidden, and both messages appearedDon't set display on elements you hide, or add [hidden] { display: none; } to your CSS: with that rule added, the message was hidden again
Writing hidden="false" to show the elementIt stays hidden: the attribute hides the element whatever its value isRemove the attribute
Using hidden for something secretThe text is still in the page's code, where anyone can read itLeave secret data out of the page

Troubleshooting​

SymptomLikely causeFix
An element with hidden still showsA CSS rule sets its displayRemove that rule, or add [hidden] { display: none; }
An element never showsIt has hidden, maybe with a value such as falseRemove the attribute

FAQ​

How do I show the element again?​

Remove the attribute, or have a script set the element's hidden property to false. A script that did that removed the attribute, and the message's display went from none to block.

What's the difference between hidden and display: none?​

For the browser they look the same, because hidden gets display: none from the browser's own styles. The difference is where it's set: hidden is part of the HTML, display is CSS, and a CSS rule that sets display wins.

Does a hidden element take up space?​

No. It's taken out of the layout, so the elements after it move up.

Conclusion​

hidden hides an element with nothing but HTML, from the screen and from screen readers, while keeping it in the page for later. If an element with hidden still shows, look for a CSS rule that sets its display.

Additional Resources​