The hidden Attribute in HTML: How it Works with Examples

html hidden attribute

The HTML hidden attribute hides elements from users but keeps them in the code. You can use it when you want content to stay on the page but not appear.

What Does the hidden Attribute Do in HTML?

The hidden attribute tells the browser not to show the element. The tag stays in the page but does not display anything.

Browsers skip the hidden element and do not draw it on the screen. Screen readers may also ignore it based on the setup.

Here is the tag:

<div hidden>Some content</div>

You can place it on almost any element. It works on <div>, <p>, <input>, and others.

Here is another example:

<p hidden>This will not appear.</p>

This code stays in the document, but the browser does not show it.

The element becomes invisible. It also becomes unreachable by keyboard. In most cases, screen readers skip it. Some tools may still access the element.

You can store data inside a form without showing it.

<input type="hidden" name="user_id" value="67">

The user cannot see or change the value, but the server can receive it.

The Differences Between hidden and CSS display: none

hidden is an HTML tag. display: none is a CSS rule. Both hide the element from the page.

The hidden is easier and faster to apply in HTML. CSS display: none gives more control and can be changed with classes or media queries.

Use hidden for simple logic in HTML forms or with templates. Use display: none to control styles or apply changes with JavaScript.

Examples of The hidden Attribute in HTML

Hide a Section of Text:

<div hidden>This text is hidden from the user.</div>

This element does not appear on the screen. The browser does not draw it, and users can’t see or click it.

Use hidden with an Input Field

<form> <input type="hidden" name="status" value="active"> </form>

The form sends the status to the server. The user does not see the input, but it still works as part of the data.

Toggle Visibility with JavaScript

<p id="note" hidden>This note is now visible.</p> <button onclick="document.getElementById('note').hidden = false;">Show Note</button>

The hidden tag hides the message at first. When you click the button, JavaScript removes the tag and shows the note.

Use hidden for Dynamic Forms

<div id="extra" hidden> <label>More Info</label> <input type="text" name="info"> </div> <button onclick="document.getElementById('extra').hidden = false;">More</button>

This setup keeps extra form fields out of view. When the user clicks the button, more inputs appear and allow the user to enter data.

Wrapping Up

In this article you learned what the hidden attribute does and how browsers treat it. You also saw the difference between it and CSS display: none.

Here is a quick recap:

  • The hidden keeps the element in the page but does not show it.
  • It works well with inputs and static HTML.
  • It does not take space and it blocks keyboard access.
  • display: none is a CSS way that lets you change visibility with classes or media.

FAQs

What does the <div hidden> tag do in HTML?

The <div hidden> element hides content from the page visually and from scripts. It will not be displayed, and JavaScript will skip it unless accessed directly.

How to use the hidden attribute in an <input> tag?

Use <input type="hidden"> to store form data without showing it in the browser. It lets you send data when a form submits without displaying the field. Example: <form> <input type="hidden" name="user_id" value="12345"> <input type="submit"> </form>

Can the <hidden> attribute be toggled with JavaScript?

Yes, you can toggle the hidden attribute using JavaScript. Example: <div id="myBox" hidden>This is hidden</div> <button onclick="document.getElementById('myBox').hidden = false;">Show</button>

What's the difference between <hidden> and CSS <display: none>?

<hidden> is a boolean attribute that completely hides the element. <display: none> is a CSS style that hides the element but allows more styling control. Example: <div hidden>Hidden using attribute</div> <div style="display: none;">Hidden using CSS</div>

Similar Reads

How to Create HTML Nested Lists with Examples

In this article, you will learn how the browsers read nested lists in HTML and how to use them with…

The Button Tag in HTML: How it Works with Examples

The HTML button tag creates interactive controls on a web page. You can use this element to trigger actions or…

HTML <cite> Tag: How to Reference Sources

The HTML <cite> tag is a small but important tool for marking up the title of a creative work. You…

HTML style Tag: Inline CSS

The style tag in HTML defines CSS inside a web page. Click to see how it works with syntax and…

HTML List Tag: Understand the ul, ol, and dl Lists

HTML has three main list tags: <ul>, <ol>, and <dl>. Each one shows a different kind of group. In this…

HTML Compatibility: Old vs New Browsers

HTML code does not always show the same result on every browser. Old browsers follow outdated rules while modern browsers…

HTML s Tag: How it Works with Examples

The HTML s tag places a line across text that has no longer value or does not hold importance. It…

HTML details Tag: How to Toggle Hidden Content

The <details> tag hides content and shows it when needed. It helps organize long pages. The tag shows extra info…

HTML title Tag: How to Set Web Page Titles

The title tag in HTML shows the page name in the browser tab and helps search engines know the topic.…

The canvas Tag in HTML: How to Draw Graphics with Examples

The HTML canvas tag draws shapes and images. It also helps you draw text with code. It gives control over…

Previous Article

HTML Style Attribute: How it Works with Examples

Next Article

HTML lang Attribute: How it Works with Examples

Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *


Subscribe to Get Updates

Get the latest updates on Coding, Database, and Algorithms straight to your inbox.
No spam. Unsubscribe anytime.