user
How to create a hidden HTML tag?
alphonsio

The the simplest way to create a HTML hidden tag is to use the attribute hidden:

<div hidden>This tag is hidden</div>

To create a hidden HTML tag, you can use one of the following methods depending on your specific use case:

1. Using the hidden attribute

This is the simplest and most semantic way:

<div hidden>This is hidden</div>
  • The element will not be visible and won't take up space in the layout.
  • It's still part of the DOM and can be shown later via JavaScript:
document.querySelector('div').hidden = false;

2. Using CSS with display: none

<div style="display: none;">This is hidden</div>
  • The element is not visible and does not take up space.
  • Can be shown with:
document.querySelector('div').style.display = 'block';

3. Using CSS with visibility: hidden

<div style="visibility: hidden;">This is hidden</div>
  • The element is not visible but still takes up space in the layout.

4. For form elements only: using <input type="hidden">

<input type="hidden" name="user_id" value="123">
  • Not visible in the UI.
  • Used to pass data in form submissions.

Summary

MethodVisibilityLayout Space TakenUse Case
hidden attributeHiddenNoGeneral-purpose
display: noneHiddenNoMore control via CSS/JS
visibility: hiddenHiddenYesKeep layout position
input type="hidden"HiddenNoForm data only