search

HTML Hide Element

Sep 04, 2024 pm 04:42 PM
htmlhtml5HTML TutorialHTML PropertiesHTML tags

The hidden global attribute in HTML5 is a Boolean attribute. It stipulates that the targeted element is further relevant or not for the HTML document. One such example of using the hidden attribute is that it can be utilized to cover/uncover any particular content present on the HTML web page that is not authorised unless the user has been authenticated. Until then, browsers won’t render the elements with active hidden property (i.e. attribute is set).

Usage of Hidden Attribute

One such usage of the hidden attributes can be like keeping a user away from seeing an element until some conditions have been met (such as selecting a radio button, etc.) after which, a JavaScript code could stipulate back the hidden attribute, hence making the element visible. This attribute should not be utilized to conceal content only for an individual presentation; rather, it should remain in the same state for all presentations if any content is kept hidden.

The content which is hidden shouldn’t be associated with unhidden content or content that is descendant to a hidden content that is yet active. This ensures that the form elements can yet be submitted and script elements can yet be executed. Scripts and Elements can, however, refer to any content that is hidden in some other context.

It would be totally incorrect to utilize the attribute in a real-world scenario to connect to a section pronounced with a hidden attribute. If the linked content is neither relevant nor applicable, then there is no need to pack them together. As per the definition of the Hidden attribute, we can hide any content present within an HTML webpage using the hidden attribute and then JavaScript code can be used to access it afterwards. Target to hide an element can also be achieved by CSS with the property as display property (i.e. setting it to none) but using the hidden attribute is an easy approach.

Note: Changing the CSS display property value on an element with a hidden attribute overrides its behavior. For example, elements styled as display: flex will be displayed despite the hidden attribute’s presence.

Syntax

<element hidden> </element>

Examples of HTML Hide Element

Given below are the examples of HTML Hide Element:

Example #1

Code:



<title>HTML hide element</title>
<style>
body {
text-align:center;
}
h1 {
color:blue;
}
</style>


<h1 id="EDUCBA">EDUCBA</h1>
<h2 id="HTML-Hide-element">HTML Hide element</h2>
<!-- hidden paragraph -->
<p hidden>A learning portal</p>

Output:

HTML Hide Element

Example #2

Code:



<title>HTML hide element</title>
<style>
body {
text-align:center;
}
h1 {
color:blue;
}
</style>


<h1 id="EDUCBA">EDUCBA</h1>
<h2 id="HTML-Hide-element">HTML Hide element</h2>
<button id="btn">TOGGLE HIDDEN ELEMENTS</button>
<p id="p" hidden>This paragraph uses HTML5's             <code>hidde</code> element.</p>
<textarea id="ta" hidden rows="5" cols="40">This textarea was hidden using the hide element</textarea>
<!-- hidden paragraph -->
<p hidden>A learning portal</p>
<script>
document.getElementById("btn").addEventListener('click', function () {p.hidden = !p.hidden;ta.hidden = !ta.hidden;}, false);
</script>

Output:

HTML Hide Element

HTML Hide Element

Example #3 – Usefulness of the Attribute.

As per the definition of the Hidden attribute, we can hide any content present within an HTML webpage using the hidden attribute and then JavaScript code can be used to access it afterwards. Target to hide an element can also be achieved by CSS with the property as display property (i.e. setting it to none) but using the hidden attribute is an easy approach. Hence, we can say that content with a hidden attribute is a slice of the DOM, but the user can’t access it.

In the below example, we’ll pick the part of a hidden element using JavaScript code:

Code:



<title>HTML hide element</title>
<style>
body {
text-align:center;
}
h1 {
color:blue;
}
</style>


<h1 id="EDUCBA">EDUCBA</h1>
<h2 id="HTML-Hide-element">HTML Hide element</h2>
<button id="btn">DISPLAY HIDDEN TEXT</button>
<output id="op">(Hidden text will appear here)</output>
<textarea id="ta" hidden rows="5" cols="40">This textarea was hidden using the hide element</textarea>
<!-- hidden paragraph -->
<p hidden>A learning portal</p>
<script>
document.getElementById("btn").addEventListener('click', function () {op.innerHTML = ta.innerHTML;}, false);
</script>

Output:

HTML Hide Element

HTML Hide Element

Important Points to Remember

These are some important points that should be well known before interacting with the hidden attribute:

  • Content that can be accessible on discrete resolution and screen sizes should not use a hidden attribute to hide the content.
  • The hidden attribute should not be benefited to hide/cover the non-visible sections of a content switcher or a tab component.
  • The non-hidden element should not be hyperlinked to a hidden element.
  • Elements being marked up as hidden are still potentially active.
  • If you want to hide content from all users, use the HTML5 hidden attribute (along with CSS display: none for browsers that do not yet support hidden). There is no need to use aria-hidden.

Conclusion

Below mentioned are some of the main key points which you should remember from this topic.

Suitable use cases for hidden attribute include:

  • Content that isn’t relevant yet but may be needed later after.
  • Content that was used previously but is no longer needed anymore.
  • Content that is reusable and being utilized by various other parts of the page in a template-like fashion.
  • Creating an off-screen canvas as a drawing buffer.

Non-suitable use cases of a hidden attribute include:

  • Hiding panels within a tabbed dialog box.
  • Hiding content in an individual presentation while intending it to be visible in others.
Note: Elements that are hidden should not be linked with the other non-hidden elements until they are related.

The above is the detailed content of HTML Hide Element. For more information, please follow other related articles on the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
How do you insert an image into an HTML page?How do you insert an image into an HTML page?May 04, 2025 am 12:02 AM

ToinsertanimageintoanHTMLpage,usethetagwithsrcandaltattributes.1)UsealttextforaccessibilityandSEO.2)Implementsrcsetforresponsiveimages.3)Applylazyloadingwithloading="lazy"tooptimizeperformance.4)OptimizeimagesusingtoolslikeImageOptimtoreduc

HTML's Purpose: Enabling Web Browsers to Display ContentHTML's Purpose: Enabling Web Browsers to Display ContentMay 03, 2025 am 12:03 AM

The core purpose of HTML is to enable the browser to understand and display web content. 1. HTML defines the web page structure and content through tags, such as, to, etc. 2. HTML5 enhances multimedia support and introduces and tags. 3.HTML provides form elements to support user interaction. 4. Optimizing HTML code can improve web page performance, such as reducing HTTP requests and compressing HTML.

Why are HTML tags important for web development?Why are HTML tags important for web development?May 02, 2025 am 12:03 AM

HTMLtagsareessentialforwebdevelopmentastheystructureandenhancewebpages.1)Theydefinelayout,semantics,andinteractivity.2)SemantictagsimproveaccessibilityandSEO.3)Properuseoftagscanoptimizeperformanceandensurecross-browsercompatibility.

Explain the importance of using consistent coding style for HTML tags and attributes.Explain the importance of using consistent coding style for HTML tags and attributes.May 01, 2025 am 12:01 AM

A consistent HTML encoding style is important because it improves the readability, maintainability and efficiency of the code. 1) Use lowercase tags and attributes, 2) Keep consistent indentation, 3) Select and stick to single or double quotes, 4) Avoid mixing different styles in projects, 5) Use automation tools such as Prettier or ESLint to ensure consistency in styles.

How to implement multi-project carousel in Bootstrap 4?How to implement multi-project carousel in Bootstrap 4?Apr 30, 2025 pm 03:24 PM

Solution to implement multi-project carousel in Bootstrap4 Implementing multi-project carousel in Bootstrap4 is not an easy task. Although Bootstrap...

How does deepseek official website achieve the effect of penetrating mouse scroll event?How does deepseek official website achieve the effect of penetrating mouse scroll event?Apr 30, 2025 pm 03:21 PM

How to achieve the effect of mouse scrolling event penetration? When we browse the web, we often encounter some special interaction designs. For example, on deepseek official website, �...

How to modify the playback control style of HTML videoHow to modify the playback control style of HTML videoApr 30, 2025 pm 03:18 PM

The default playback control style of HTML video cannot be modified directly through CSS. 1. Create custom controls using JavaScript. 2. Beautify these controls through CSS. 3. Consider compatibility, user experience and performance, using libraries such as Video.js or Plyr can simplify the process.

What problems will be caused by using native select on your phone?What problems will be caused by using native select on your phone?Apr 30, 2025 pm 03:15 PM

Potential problems with using native select on mobile phones When developing mobile applications, we often encounter the need for selecting boxes. Normally, developers...

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

SublimeText3 English version

SublimeText3 English version

Recommended: Win version, supports code prompts!

PhpStorm Mac version

PhpStorm Mac version

The latest (2018.2.1) professional PHP integrated development tool

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft