Difference between `aria-label` and `aria-labelledby`

The aria-label and aria-labelledby attributes are both used in web accessibility to provide labels for elements, but they serve different purposes and are used in different scenarios. Here’s a breakdown of the differences between them:

aria-l…


This content originally appeared on DEV Community and was authored by Syed Ammar

The aria-label and aria-labelledby attributes are both used in web accessibility to provide labels for elements, but they serve different purposes and are used in different scenarios. Here’s a breakdown of the differences between them:

aria-label

  • Purpose: aria-label provides an explicit label for an element. The label is a string that is directly defined within the aria-label attribute.

  • Usage: Use aria-label when you want to provide a custom, hidden label for an element that does not have a visible label in the document. It’s often used for elements like buttons or icons where the visible text does not fully describe the element’s function or when there’s no visible text at all.

  • Example:

  <button aria-label="Close">X</button>

In this example, the button visually displays "X", but the screen reader will announce "Close" because of the aria-label.

  • Consideration: The label provided by aria-label is not visible on the screen. It’s purely for assistive technologies like screen readers.

aria-labelledby

  • Purpose: aria-labelledby refers to the id of one or more elements that contain the text that labels the current element. This means the label is derived from the content of other elements in the document.

  • Usage: Use aria-labelledby when there is an existing visible element (or elements) on the page that provides a suitable label. It’s particularly useful for linking to existing text that already describes the element.

  • Example:

  <label id="username-label">Username:</label>
  <input type="text" id="username" aria-labelledby="username-label" />

In this example, the input field is labeled by the label element with id="username-label". The screen reader will announce the content of the label ("Username:") when the input field is focused.

  • Consideration: Unlike aria-label, the label provided by aria-labelledby is visible in the document, making it more natural and consistent with how most labels work on a webpage.

Key Differences

  • Content Source:

    • aria-label: The label is defined directly as a string within the attribute.
    • aria-labelledby: The label is derived from the content of another element(s) in the DOM, which is referenced by id.
  • Visibility:

    • aria-label: The label is hidden and only accessible to assistive technologies.
    • aria-labelledby: The label is visible on the screen because it references existing elements.
  • Use Case:

    • aria-label: Use when you need to provide a label for an element that has no visible label or when the visible label does not accurately describe the element’s purpose.
    • aria-labelledby: Use when there is already an appropriate visible label or text that can be associated with the element.

Practical Example:

Here’s a situation where both might be used together:

<div id="name-header">Full Name</div>
<input type="text" aria-labelledby="name-header" aria-label="Enter your full name" />

In this example:

  • aria-labelledby references id="name-header", which is the visible label.
  • aria-label provides additional instructions or clarification that is not visible but is important for accessibility.

Using both attributes together might be redundant unless you need to provide extra information not covered by the visible label. Typically, you would use one or the other, depending on the context.


This content originally appeared on DEV Community and was authored by Syed Ammar


Print Share Comment Cite Upload Translate Updates
APA

Syed Ammar | Sciencx (2024-08-28T11:31:21+00:00) Difference between `aria-label` and `aria-labelledby`. Retrieved from https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/

MLA
" » Difference between `aria-label` and `aria-labelledby`." Syed Ammar | Sciencx - Wednesday August 28, 2024, https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/
HARVARD
Syed Ammar | Sciencx Wednesday August 28, 2024 » Difference between `aria-label` and `aria-labelledby`., viewed ,<https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/>
VANCOUVER
Syed Ammar | Sciencx - » Difference between `aria-label` and `aria-labelledby`. [Internet]. [Accessed ]. Available from: https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/
CHICAGO
" » Difference between `aria-label` and `aria-labelledby`." Syed Ammar | Sciencx - Accessed . https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/
IEEE
" » Difference between `aria-label` and `aria-labelledby`." Syed Ammar | Sciencx [Online]. Available: https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/. [Accessed: ]
rf:citation
» Difference between `aria-label` and `aria-labelledby` | Syed Ammar | Sciencx | https://www.scien.cx/2024/08/28/difference-between-aria-label-and-aria-labelledby/ |

Please log in to upload a file.




There are no updates yet.
Click the Upload button above to add an update.

You must be logged in to translate posts. Please log in or register.