Skip to content

VisuallyHidden

The VisuallyHidden component hides its content from the screen while keeping it in the accessibility tree, so screen readers still announce it. Use it to add context that sighted users get from the visual layout, such as which article a “Read more” link belongs to.

It wraps the existing .sr-only utility class, so you don’t need to remember the class name or its CSS. With the focusable prop, the content becomes visible while it, or something inside it, has keyboard focus.

Use the VisuallyHidden component when you need to:

  • Add context to links or buttons whose visible text relies on the surrounding layout, such as “Read more” or “Edit”
  • Provide text for screen readers next to an icon or a visual indicator, such as a current page or a status
  • Write a description that a control can reference with aria-describedby
  • Add a skip link or another keyboard shortcut that appears only on focus, using the focusable prop
---
import { VisuallyHidden } from 'accessible-astro-components'
---
<a href="/blog/accessible-forms">
Read more<VisuallyHidden> about accessible forms</VisuallyHidden>
</a>

Sighted users see “Read more”, while screen readers announce “Read more about accessible forms”.

Learn how to implement the VisuallyHidden component in your project.

By default, the component renders a <span>, so you can place it inside a link, a button, or a sentence.

---
import { VisuallyHidden } from 'accessible-astro-components'
---
<button type="button">
Edit<VisuallyHidden> shipping address</VisuallyHidden>
</button>

Use the as prop to render a different element when a <span> isn’t the right fit, for example a paragraph or block content. It accepts 'span', 'div', 'p', and 'li'. Use 'li' only as a direct child of a <ul> or <ol>, and keep in mind that screen readers include it in the number of list items they announce.

---
import { VisuallyHidden } from 'accessible-astro-components'
---
<!-- A paragraph -->
<VisuallyHidden as="p">Results are sorted by date, newest first.</VisuallyHidden>
<!-- Block content -->
<VisuallyHidden as="div">
<p>Prices include VAT.</p>
<p>Shipping is calculated at checkout.</p>
</VisuallyHidden>

Attributes such as id are passed to the rendered element, so other elements can reference the hidden text.

---
import { VisuallyHidden } from 'accessible-astro-components'
---
<button type="button" aria-describedby="delete-note">Delete project</button>
<VisuallyHidden id="delete-note">This also deletes all files in the project.</VisuallyHidden>

Set focusable to show the content while it, or an element inside it, has focus. When focus moves on, the content is hidden again. This is useful for skip links to other parts of the page.

---
import { VisuallyHidden } from 'accessible-astro-components'
---
<VisuallyHidden focusable class="skip-comments">
<a href="#comments">Skip to comments</a>
</VisuallyHidden>

Configure the VisuallyHidden component with these available props.

PropTypeDefaultDescription
as'span' | 'div' | 'p' | 'li''span'The element to render
focusablebooleanfalseShows the content while it, or an element inside it, has focus
valuestring | numberundefinedThe ordinal value of the list item, only used when as="li"
classstringundefinedAdditional CSS classes, merged with the component’s own classes

The component also supports class:list, and any other HTML attributes are passed to the rendered element. The default slot contains the content to hide visually.

VisuallyHidden keeps content available to assistive technology while removing it from the visual layout:

  • Uses the .sr-only technique: the element is clipped to a 1×1 pixel area instead of being removed with display: none or visibility: hidden, so it stays in the accessibility tree
  • Adds no ARIA attributes or roles, so the content is announced as normal text in its reading order
  • With focusable, shows the content on focus using :focus-within and hides it again when focus leaves, so keyboard users can see where they are
  • Supports native elements only (span, div, p, li), so you can keep valid HTML structure

VisuallyHidden has no visual styles of its own beyond hiding the content. When you use focusable, style the content that appears on focus, for example the link inside it.

<VisuallyHidden focusable>
<a href="#comments" class="skip-comments-link">Skip to comments</a>
</VisuallyHidden>
<style>
.skip-comments-link {
display: inline-block;
padding-block: 0.5rem;
padding-inline: 1rem;
background: light-dark(hsl(0 0% 100%), hsl(215 25% 15%));
color: light-dark(hsl(215 25% 27%), hsl(215 25% 89%));
}
.skip-comments-link:focus-visible {
outline: 2px solid currentColor;
outline-offset: 4px;
}
</style>

See the focusable prop in action:

Use Tab to move focus to the hidden link in the box below. It appears while it has focus and is hidden again when focus moves on.