Hiding content
Hey ,
I'm thrilled to help you learn JavaScript. Unfortunately, you've landed on a page where you cannot access with your current purchase.
Please upgrade (use this link) access this content.
I'm super eager to help you learn more!
Hiding content
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
hidden
attribute.
Here’s how to choose between them:
- Use
hidden
or display: none
if you don’t want to animate the element.
- Use
visibility: hidden
if 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>