Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
What is aria-label and How to Use It?
Labels are essential for users who are blind, have low vision, have mobility issues, or have memory loss. Many users will be unable to access a form if labels are missing. Visual labels are text that appears near a form control and describes what information is contained in a specific form field or group of fields. Each label must be associated with the form control or group of controls programmatically.
Introduction to aria-label
The aria-label attribute is part of the Accessible Rich Internet Applications (ARIA) specification, a W3C standard for improving web accessibility beyond what semantic HTML can achieve. This attribute provides an accessible name for elements that lack visible text or when the visible text is insufficient for screen readers and other assistive technologies.
The aria-label attribute specifies a string that will be used to label the current element. It is primarily used to provide a text alternative to screen readers when an element has no visible text or when additional context is needed. This attribute helps define descriptive text and provides crucial information about the document structure to assistive technology users.
The aria-label is typically used to replace or enhance existing labels with more precise information. It allows assistive technology to attach a meaningful label to HTML elements that would otherwise be anonymous or unclear. However, caution must be exercised when using aria-label because it does not work with all HTML elements and overrides any existing visible text.
Syntax
Following is the syntax for using the aria-label attribute
<element aria-label="descriptive text">Content</element>
The value should be a clear, concise description that conveys the element's purpose or function to assistive technology users.
Supported HTML Elements
HTML elements that support the aria-label attribute include
buttonInteractive buttonsinputForm input elementstextareaMulti-line text inputselectDropdown selection listsa(when href attribute is present) Linksaudioandvideo(when controls are present) Media elementsdivandspan(when they have interactive roles) Generic containers with ARIA roles
Important: The aria-label attribute takes precedence over any other labeling mechanism, such as visible text content or <label> elements. Screen readers will announce only the aria-label value, not the visible text.
How to Use aria-label
The aria-label attribute expects a string of characters as its value, which becomes the accessible name announced by screen readers. Use aria-label when
An element has no visible text (like icon-only buttons)
The visible text is unclear or insufficient
Additional context is needed for accessibility
Example Close Button with Icon
Following example shows a close button that uses only an "×" symbol. The aria-label provides context for screen readers
<!DOCTYPE html>
<html>
<head>
<title>aria-label Close Button Example</title>
<style>
.close-btn {
background-color: #f0f0f0;
border: 1px solid #ccc;
height: 30px;
width: 30px;
font-size: 18px;
color: #333;
cursor: pointer;
border-radius: 4px;
}
.close-btn:hover {
background-color: #dc3545;
color: white;
}
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<div style="border: 1px solid #ddd; padding: 15px; position: relative; width: 300px;">
<button class="close-btn" aria-label="Close dialog" onclick="alert('Dialog closed')">×</button>
<p>This is a dialog box with a close button in the top-right corner.</p>
</div>
</body>
</html>
The button displays "×" to users, but screen readers announce "Close dialog" making the purpose clear.
Example Enhanced Link Context
Following example shows how aria-label can provide additional context for links
<!DOCTYPE html>
<html>
<head>
<title>aria-label Link Example</title>
<style>
.read-more {
background-color: #007bff;
color: white;
padding: 8px 16px;
text-decoration: none;
border-radius: 4px;
display: inline-block;
margin: 10px 0;
}
.read-more:hover {
background-color: #0056b3;
}
</style>
</head>
<body style="font-family: Arial, sans-serif; padding: 20px;">
<article>
<h2>Introduction to HTML5</h2>
<p>HTML5 is the latest version of the HTML standard...</p>
<a href="/html5-tutorial" class="read-more" aria-label="Read the complete HTML5 tutorial">Read More</a>
</article>
</body>
</html>
While sighted users see "Read More", screen readers announce "Read the complete HTML5 tutorial", providing specific context about where the link leads.
Example Form Input with aria-label
Following example demonstrates using aria-label on form inputs when a visible label is not present
<!DOCTYPE html>
<html>
<head>
<title>aria-label Form Example</title>
<style>
.search-container {
display: flex;
align-items: center;
gap: 10px;
padding: 20px;
}
.search-input {
padding: 8px 12px;
border: 2px solid #ddd;
border-radius: 4px;
font-size: 16px;
}
.search-btn {
padding: 8px 16px;
background-color: #28a745;
color: white;
border: none;
border-radius: 4px;
cursor: pointer;
}
</style>
</head>
<body style="font-family: Arial, sans-serif;">
<form class="search-container">
<input type="search" class="search-input" aria-label="Search tutorials" placeholder="Enter keywords...">
<button type="submit" class="search-btn" aria-label="Submit search">?</button>
</form>
</body>
</html>
Both the input field and button use aria-label to provide clear descriptions for screen reader users.
Screen Reader Behavior
Different screen readers announce aria-label content in slightly different ways. Following table shows how popular screen readers handle aria-label
| Screen Reader | Announcement Pattern | Example Output |
|---|---|---|
| NVDA | Element type + aria-label | "button, Close dialog" |
| JAWS | aria-label + element type | "Close dialog, button" |
| VoiceOver | aria-label + element type | "Close dialog, button" |
| ChromeVox | aria-label + element type | "Close dialog, button" |
Benefits of Using aria-label
The aria-label attribute provides several key advantages
Enhanced Accessibility: Makes web content accessible to users with visual impairments by providing descriptive text for screen readers.
Improved User Experience: Helps assistive technology users understand the purpose and function of interactive elements.
Context Clarity: Provides additional context when visible text is ambiguous or insufficient.
Flexible Implementation: Can be added to existing elements without affecting visual design.
Best Practices
When using aria-label, follow these guidelines
Keep descriptions concise but informative
Use plain language that users can easily understand
Avoid redundant information already provided by element type
Test with actual screen readers to verify proper announcement
Consider using
aria-labelledbywhen referring to existing visible text
Conclusion
The aria-label attribute is a powerful accessibility tool that provides descriptive text for screen readers and other assistive technologies. It is essential for creating inclusive web experiences, particularly for elements with no visible text or when additional context is needed. Proper implementation of aria-label significantly improves website accessibility and user experience for people with disabilities.
