Every link, button, form label, or image should have a descriptive label, regardless of whether it’s a menu toggle button or a hidden anchor. Without proper labeling, screen readers used by people with visual impairments won’t be able to convey what the elements are, making it difficult for them to navigate your website.
Text Labels for Interactive Elements
There are two main ways to provide descriptive labels for elements like buttons and links:
Include Text Inside the Element: This is the most common approach. If you need to visually hide the text while still making it accessible to screen readers, avoid using display: none; or visibility: hidden;, as these will also hide the text from assistive technologies. Instead, use a CSS class like Tailwind's sr-only, which is designed for screen reader accessibility. You can replicate this with the following CSS:
position: absolute;
width: 1px;
height: 1px;
padding: 0;
margin: -1px;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border-width: 0;Use the aria-label Attribute: If you don’t want to add visible text for certain elements (perhaps to prevent it from appearing in search results), you can use the aria-label attribute to provide an accessible name. For example:
<button aria-label="Open menu">...</button>
This approach ensures that screen readers can still convey the purpose of the element to users, even if the text is not displayed on the screen.
Making Images Accessible with the alt Attribute
To make images accessible, always use the alt attribute to provide a text description. This helps screen readers convey the image's content or purpose to visually impaired users.
Descriptive Text: Set the alt attribute to briefly describe what the image shows or its function. For example:
<img src="dinosaur.jpg" alt="A cartoon dinosaur holding a magnifying glass">
Using alt text ensures that all users can access and understand the content on your website.