Hide Text Characters Around Elements: A Complete CSS Guide

Learn how to properly hide text with CSS while maintaining accessibility for screen readers and keyboard users. Master techniques from display:none to sr-only patterns.

Understanding CSS Hiding Techniques

Modern web development frequently requires hiding text while maintaining accessibility for assistive technologies. Whether you're implementing icon-only buttons, creating skip navigation links, or providing screen reader-only context, understanding the nuanced differences between CSS hiding techniques is essential.

Why Proper Text Hiding Matters

The ability to hide text visually while keeping it accessible to assistive technologies is a cornerstone of web accessibility. When implemented correctly, this technique enables developers to provide additional context to screen reader users without cluttering the visual interface. However, the wrong approach can completely remove content from the accessibility tree, breaking the experience for users who depend on assistive technologies. According to WebAIM's research on invisible content, proper technique selection is essential for accessibility.

The core challenge is that CSS provides multiple ways to make elements invisible, but these methods behave very differently with respect to accessibility. Some techniques hide content from everyone, while others preserve it for assistive technologies. Understanding these distinctions is crucial for making informed implementation decisions.

The Spectrum of Visibility

CSS hiding techniques exist on a spectrum from completely invisible (hidden from all users and assistive technologies) to visually hidden (invisible on screen but accessible to screen readers). The technique you choose depends entirely on your use case and the users you need to serve.

When content should be completely removed from the page for all users, techniques like display: none or visibility: hidden are appropriate. For icon-only buttons that need accessible labels or skip navigation links that serve keyboard users, you'll need more sophisticated techniques.

CSS Properties for Completely Hiding Content

Display None

The display: none property completely removes an element from the page flow and renders it invisible to all users, including those using assistive technologies. When an element has display: none, it is not included in the accessibility tree, meaning screen readers will not announce it, and it won't be focusable via keyboard navigation.

.hidden-completely {
 display: none;
}

This technique is appropriate when you genuinely want to remove content from the document entirely. Common use cases include hiding modal dialogs when they're closed, hiding deprecated interface elements, and hiding content that should only appear under specific conditions.

Visibility Hidden

The visibility: hidden property hides an element visually while preserving the space it occupies in the document layout. Unlike display: none, elements with visibility: hidden remain in the document flow and maintain their dimensions. However, like display: none, content is removed from the accessibility tree. Understanding how CSS selectors work is foundational knowledge for implementing these techniques correctly.

.hidden-visibility {
 visibility: hidden;
}

Opacity Zero

Setting opacity: 0 makes an element visually transparent while keeping it fully interactive and present in the accessibility tree. Unlike other hiding techniques, elements with opacity: 0 remain focusable and accessible to assistive technologies.

Techniques for Screen Reader Only Content

The Off-Screen Technique

The most widely accepted method for hiding text visually while keeping it accessible to screen readers is absolute positioning content off-screen. This technique places content beyond the visible viewport while maintaining its presence in the accessibility tree.

.sr-only {
 position: absolute;
 left: -10000px;
 top: auto;
 width: 1px;
 height: 1px;
 overflow: hidden;
}

This approach works by positioning the element 10000 pixels to the left of the viewport, effectively placing it outside the visible area. The width and height of 1 pixel ensure the element takes up minimal space, while overflow: hidden clips any content that might extend beyond those dimensions.

The Clip-Path Technique

A more modern approach to visually hiding content uses the clip-path property in combination with other CSS rules:

.sr-only-clip {
 clip-path: inset(50%);
 height: 1px;
 width: 1px;
 overflow: hidden;
 position: absolute;
 white-space: nowrap;
}

Text-Indent Technique

The text-indent: -10000px technique moves text far to the left of its container:

.text-indent-hide {
 text-indent: -10000px;
 white-space: nowrap;
 overflow: hidden;
}

Implementing Icon Buttons with Hidden Text

The Challenge of Icon-Only Buttons

Icon-only buttons present a common accessibility challenge. When you use an icon without accompanying text, screen reader users have no way of understanding the button's purpose. The solution is to provide hidden text that describes the button's function.

Accessible Icon Button Pattern

<button aria-expanded="false" aria-controls="navList">
 <span class="visually-hidden">Menu</span>
 <span class="hamburger-icon"></span>
</button>

This approach works because the button's accessible name is calculated from its text content. The hidden "Menu" text provides the accessible name, which screen readers will announce. Our web development team follows these patterns to ensure all interactive elements are accessible to users of assistive technologies.

Alternative: aria-labelledby for Hidden References

For more complex scenarios, you can use aria-labelledby to reference hidden text elsewhere:

<button aria-labelledby="menu-button-label">
 <span class="hamburger-icon"></span>
</button>
<p id="menu-button-label" style="display: none">Menu</p>

Skip Navigation Links

The Purpose of Skip Links

Skip links allow keyboard users to bypass repetitive navigation and jump directly to the main content. Without skip links, users who navigate via keyboard must tab through every navigation link on every page before reaching the main content. This is a critical accessibility requirement under WCAG guidelines.

Implementing Skip Links

.skip-link {
 position: absolute;
 left: -10000px;
 top: auto;
 width: 1px;
 height: 1px;
 overflow: hidden;
}

.skip-link:focus {
 position: static;
 width: auto;
 height: auto;
}
<a href="#main-content" class="skip-link">Skip to main content</a>

When the link is not focused, it's positioned off-screen and invisible. When a keyboard user tabs to the link, it becomes visible, providing a clear visual indicator. Incorporating skip links is one of the foundational elements of our accessibility services, ensuring compliance with WCAG 2.1 success criteria.

Performance Considerations

CSS Containment and Rendering Performance

When hiding content, consider the rendering performance implications:

  • display: none: Elements are not rendered at all, no layout or paint costs
  • visibility: hidden: Elements are rendered but not painted, maintain layout impact
  • opacity: 0: Elements are fully rendered, may incur paint costs

Reducing Layout Thrashing

For frequently toggled content, consider using visibility or opacity instead of display, as these may have better performance characteristics. However, always measure performance in your specific use case.

Our performance optimization services include analysis of CSS rendering patterns to ensure optimal page load times and smooth user interactions. Understanding CSS units like rem helps create consistent, accessible layouts.

Common Anti-Patterns to Avoid

Using display: none for Screen Reader Text

One of the most common mistakes is using display: none when you want content to be accessible to screen readers. This completely removes the content from the accessibility tree.

Mismatched Accessible and Visible Names

A critical failure occurs when the accessible name of an element doesn't match what visual users see. Voice input users who say "click settings" won't activate a button if the accessible name is different.

Redundant Screen Reader Instructions

Avoid adding redundant instructions that assume screen reader users don't understand basic interface concepts. Screen reader users know what a button is--telling them "Click this button to..." adds unnecessary verbosity.

Zero-Dimension Techniques

Using width: 0, height: 0, or font-size: 0 is not recommended. These techniques may hide content inconsistently and can be interpreted as attempts at cloaking by search engines. For guidance on valid CSS content, including appropriate use of pseudo-elements, consult our comprehensive guide.

Frequently Asked Questions

Sources

  1. WebAIM: CSS in Action - Invisible Content - Comprehensive authoritative resource on all CSS hiding techniques with accessibility implications
  2. Make Things Accessible: Visually Hiding Text - Detailed guide on accessible visually hidden text patterns and ARIA considerations
  3. W3C WAI: Technique C7 - Using CSS to Hide a Portion of the Link Text - Official WCAG technique for hiding link text portions while maintaining accessibility

Build Accessible Web Experiences

Our team specializes in creating inclusive web applications that work for all users, including those who rely on assistive technologies.