Hiding elements in your UI is common, but doing so in an accessible manner ensures you don’t unintentionally exclude some users. In this article, we’ll cover three scenarios:
- Hiding elements from screen readers.
- Showing elements to only screen readers.
- Hiding elements from all users, including screen readers.
Each section includes code examples and practical use case
Hiding elements from screen readers
If you want an element to be visible to sighted users but hidden from screen readers, you can use the aria-hidden="true"
attribute or CSS.
<div aria-hidden="true">This content is hidden from screen readers.</div>
Use cases
- Hiding background images
- Decorative icons or visual elements that don’t convey meaningful information.
<button>
<span aria-hidden="true">+</span>
Add Item
</button>
Showing elements to only screen readers
To make an element visible to screen readers but invisible to sighted users, you can use the "visually hidden" technique. Tailwind CSS provides a prebuilt utility class, sr-only
, for this purpose.
<div class="sr-only">
This content is only visible to screen readers.
</div>
Writing the sr-only
class in CSS would look like this
.sr-only {
position: absolute;
width: 1px;
height: 1px;
margin: -1px;
padding: 0;
overflow: hidden;
clip: rect(0, 0, 0, 0);
white-space: nowrap;
border: 0;
}
Use cases
-
For labeling interactive elements like inputs without visually displaying the text:
<label for="username" class="sr-only">Username</label> <input type="text" id="username" name="username">
-
Providing additional information for screen reader users using aria-describedby
<div class="sr-only" id="instructions"> Password must include 8 characters, one number, and one special symbol. </div> <input type="password" aria-describedby="instructions">
Hiding Input Elements for Custom Radio Buttons
When creating custom radio buttons (or checkboxes), you often hide the native input element and replace it with a visually styled element. To do this accessibly, the hidden input must remain visible to screen readers.
Hiding Elements from All Users
To completely hide an element from both sighted users and screen readers, you can use display: none
or visibility: hidden
.
.hidden {
display: none;
/* or visibility: hidden; */
}
Apply the class:
<div class="hidden">This content is hidden from all users.</div>
TL;DR
Here’s a table summarizing how various CSS attributes affect visibility for different types of users:
CSS Attribute | Hides for All Users | Hides for Sighted Users | Hides for Screen Readers | Notes |
---|---|---|---|---|
display: none |
✅ | ✅ | ✅ | Completely removes the element from the visual and accessibility tree. |
visibility: hidden |
✅ | ✅ | ✅ | Hides the element visually but keeps it in the layout and accessibility tree. |
opacity: 0 |
❌ | ✅ | ❌ | Makes the element fully transparent but still visible to screen readers and interactive. |
clip: rect(0, 0, 0, 0) |
✅ | ✅ | ❌ | Commonly used in the "visually hidden" technique; removes visual rendering but accessible. |
position: absolute; width: 1px; height: 1px; |
✅ | ✅ | ❌ | Used with the "visually hidden" technique; keeps elements accessible to screen readers. |
aria-hidden="true" |
❌ | ❌ | ✅ | Removes the element from the accessibility tree but leaves it visually present. |
overflow: hidden |
❌ | ✅ (if out of bounds) | ❌ | Hides content visually if it overflows the container but does not affect screen readers. |
height: 0; width: 0; |
✅ | ✅ | ❌ | Hides content visually while keeping it accessible to screen readers. |
z-index: -1 |
❌ | ✅ | ❌ | Moves the element behind others, making it invisible to sighted users but screen reader-accessible. |
opacity: 0; pointer-events: none; |
✅ | ✅ | ❌ | Makes an element fully transparent and non-interactive, but accessible to screen readers. |
Top comments (4)
This article is a very useful reference. A couple of months ago, my team and I had to make many changes to a website to make it more accessible and comply with local regulation. This kinds of tools were very useful back then.
There is also the
hidden
global attribute which hides the element to any presentation, SR e AT included. Is the equivalent ofdisplay: none
but for HTMLYes, they are quite similar. But
display
property has a higher priority. If you set the hidden attribute on an element but set the display to block, the element is still visibleIt is still a valid option that people often overlook.