There are three ways to hide content:
- Hiding content BOTH visually and from screen readers.
- Hiding content visually only.
- Hiding content from screen readers only.
Hiding content BOTH visually and from screen readers
If you want to hide content completely, you can use any of these methods:
- Set element to
display: none. - Set element to
visibility: hidden. - Use the
hiddenattribute.
Hereβs how to choose between them:
- Use
hiddenordisplay: noneif you donβt want to animate the element. - Use
visibility: hiddenif you want to animate hidden element.
Hiding content visually
In theory, you can hide content visually (but not from screen readers) by setting opacity to 0.
.element { opacity: 0;}But itβs not so simple in practice. Long story short, the best way to hide content visually is with this bunch of CSS.
.sr-only { position: absolute; width: 1px; height: auto; margin: 0; padding: 0; border: 0; clip: rect(0 0 0 0); overflow: hidden; white-space: nowrap;}If you use .sr-only, users will not be able to see the content, but screen readers can still read the content.
<p>Visible content.</p><p class="sr-only">Screen reader only content.</p>Hiding content from screen readers
You can use aria-hidden to hide content from screen readers. You should hide content from screen readers only if it improves their experience.
<p>Visible content.</p><p aria-hidden="true">Visible (but not to screen readers)</p><p>Visible content.</p>Welcome! Unfortunately, you donβt have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson