zaro

How Do I Disable Hyperlinks in HTML?

Published in HTML Link Disability 3 mins read

Disabling a standard HTML hyperlink (<a> tag) is not achieved with a simple disabled attribute like form elements. Instead, it typically involves a combination of HTML attribute modifications and ARIA (Accessible Rich Internet Applications) attributes, along with CSS for visual styling. Based on best practices for accessibility, the primary way to create a non-interactive yet semantically recognizable disabled link involves the following steps:

  • Remove the href attribute: This is the fundamental step to prevent the link from being clickable and navigating to another URL.
  • Add role="link": Although the href is removed, adding role="link" helps ensure that screen readers and other assistive technologies still recognize the element's intended semantic purpose as a link, albeit one that is currently disabled.
  • Add aria-disabled="true": This essential ARIA attribute explicitly communicates to assistive technologies that the element is in a disabled state, indicating it is currently not interactive.

By combining these attributes, you create an element that looks and behaves like a non-interactive link and is properly communicated as disabled to users of assistive technologies.

Implementing a Disabled Link

Here's a breakdown of the recommended approach based on the provided guidance:

  1. Remove the href attribute: This is crucial. Without the href, the <a> element loses its default link behavior and becomes non-interactive when clicked. The reference states this is done "so that it can no longer receive the focus." While removing href primarily stops navigation and keyboard activation via Enter key, focus behavior might still require managing tabindex depending on the element's content and other attributes.

    <a href="#" class="my-link">Click Me</a>
    <!-- Becomes -->
    <a class="my-link">Click Me</a>
  2. Add role="link": As mentioned in the reference, this step ensures "it is always considered a link by screen readers." Even without the href, the role="link" attribute maintains the element's semantic role for accessibility purposes.

    <a role="link" class="my-link">Click Me</a>
  3. Add aria-disabled="true": The reference notes this attribute is added "so that it is indicated as being disabled." This attribute is specifically designed to signal to assistive technologies that an element is currently unusable or non-interactive.

    <a role="link" aria-disabled="true" class="my-link">Click Me</a>

Combined HTML and Practical Considerations

Putting it together, a disabled link using this method would look like this:

<a role="link" aria-disabled="true">Disabled Link Example</a>

Important Note on Styling: While the HTML and ARIA attributes handle the interactivity and accessibility state, you must use CSS to visually style the link to appear disabled (e.g., change text color to gray, add a "not-allowed" cursor). The HTML/ARIA attributes do not automatically apply disabled visual styling.

Here is a summary of the attributes/actions and their effects:

Action/Attribute Purpose Effect
Remove href Prevents default navigation and clicking. Reduces default focusability. Element is no longer a standard interactive link.
Add role="link" Maintains semantic role for accessibility. Assistive technologies identify it as a link.
Add aria-disabled="true" Signals disabled state to assistive technologies. Assistive technologies announce it as a disabled link.
(CSS Styling - Not part of HTML disabling itself) Provides visual indication of the disabled state. Changes appearance (color, cursor, etc.) to look disabled.

This combination provides a robust and accessible way to represent a disabled hyperlink in HTML.