VisuallyHidden
Introduction
Section titled “Introduction”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.
When to use
Section titled “When to use”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
focusableprop
Quick example
Section titled “Quick example”---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.
Basic usage
Section titled “Basic usage”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>Choosing the element
Section titled “Choosing the element”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>Referencing hidden text
Section titled “Referencing hidden text”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>Focusable content
Section titled “Focusable content”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.
| Prop | Type | Default | Description |
|---|---|---|---|
as | 'span' | 'div' | 'p' | 'li' | 'span' | The element to render |
focusable | boolean | false | Shows the content while it, or an element inside it, has focus |
value | string | number | undefined | The ordinal value of the list item, only used when as="li" |
class | string | undefined | Additional 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.
Accessibility
Section titled “Accessibility”VisuallyHidden keeps content available to assistive technology while removing it from the visual layout:
- Uses the
.sr-onlytechnique: the element is clipped to a 1×1 pixel area instead of being removed withdisplay: noneorvisibility: 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-withinand 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
Styling
Section titled “Styling”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><VisuallyHidden focusable> <a href="#comments" class="inline-block px-4 py-2 bg-white dark:bg-gray-800 text-gray-900 dark:text-gray-100 focus-visible:outline-2 focus-visible:outline-offset-4" > Skip to comments </a></VisuallyHidden>Interactive example
Section titled “Interactive example”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.