What is the Difference Between the hidden and aria-hidden Attributes?


Introduction to Hidden and aria-hidden Attributes

The HTML hidden attribute is used for elements that are not displayed to the user until or unless certain conditions or criteria are met, i.e., elements whose content is irrelevant to a specific user. It is a boolean attribute which when used on an element, removes all relevance to that element.

When specified on an HTML element, [the hidden attribute] denotes that the element is being used to declare content to be reused by other parts of the page rather than being directly accessed by the user, or that the element is not yet, or is no longer, directly relevant to the current state of the page.

For example, if we use a hidden attribute, the browser will not display the content of the element, but we can easily access the hidden attributes using certain JavaScript operations, and when the user meets certain conditions, the JavaScript will allow the browser to display the content of the hidden attribute.

Syntax

<element hidden>

Aria-hidden attribute indicates to the user whether the content they are reading is important and should be focused on or ignored. It denotes that elements and their child elements will not be visible or accessible to the client in the manner defined by the programmer.

The element is not removed from the browser when using aria-hidden; instead, it is removed from the accessibility tree and assisting technologies but remains visible on the browser. aria-hidden = "true ", for example, will only remove the desired element and its children from the accessibility tree. If used with binding elements such as the body, it should only be used on the specific element. The entire website will be rendered inaccessible.

Syntax

<element aria-hidden="true/false">

Difference between hidden and aria-hidden Attributes

Semantic difference

The hidden attribute hides an element from all presentations, including printers and screen readers (assuming these devices honor the HTML specs). Hidden would suffice to remove an element from the accessibility tree as well as visual media.

Elements with aria-hidden="true" are not exposed to the accessibility tree, so screen readers, for example, will not announce them. This technique should be used with caution because it will provide different experiences to different users: accessible user agents will not announce/render them, but visual agents will. When done correctly, this can be a good thing, but it also has the potential to be abused.

Syntactic difference

Hidden is a boolean attribute, which means that if the attribute is present, it is true regardless of its value, and if the attribute is absent, it is false. In the true case, the best practice is to use either no value (<div hidden>...</div>) or an empty string (<div hidden="">...</div>). It is preferrable to avoid using hidden="true" because someone reading/updating the code might conclude that hidden="false" would have the opposite effect, which is simply incorrect.

Aria-hidden, on the other hand, is an enumerated attribute with a limited set of values. If the aria-hidden attribute is present, it must have a value of "true" or "false." We must remove the attribute entirely if we want the "undefined (default)" state.

Example

This is an example of using the hidden attribute.

<!DOCTYPE html>
<html>
  <head>
    <title>Example of using hidden attribute</title>
    <style>
        body{
            margin:30px;
        }
        div{
            border:2px dashed mediumvioletred;
            height:40px;
            width:300px;
        }
        h3{
            color:navy;
        }
    </style>
  </head>
  <body>
    <h3>The paragraph underneath is hidden.</h3>
    <div>
    <p hidden>This is invisible text.</p>
    </div>
  </body>
</html>

The aria-hidden attribute indicates whether or not screen readers should ignore the element. It specifies that an element and its descendants will be invisible to users, as implemented by the author. This attribute should only be used with caution to hide visibly rendered content from assistive technology when the process of hiding this content is intended to improve the experience of assistive technology users by removing redundant content. Authors must ensure that assistive technologies can access equivalent and identical meaning and functionality.

Example

This is an example of using the aria- hidden attribute.

<!DOCTYPE html>
<html>
  <head>
    <title>Example of using aria-hidden attribute</title>
    <style>
        body{
            margin:30px;
        }
        div{
            border:2px dashed navy;
            height:40px;
            width:300px;
        }
    </style>
  </head>
  <body>
    <p>aria-hidden="false"</p>
    <div>
    <p aria-hidden="false">This is accessible text.</p>
    </div>
    <br>
    <p>aria-hidden="true"</p>
    <div>
    <p aria-hidden="true">This is inaccessible text.</p>
    </div>
  </body>
</html>

Updated on: 12-Sep-2023

74 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements