Course contentsShow
← Web Development
Lesson 1049 of 2,98219. Keyboard Navigation and FocusFree lesson

Hidden Content and Focus Management

Ensuring hidden elements don't receive focus using display:none, visibility:hidden, or hidden attribute.

Hidden Content and Focus Management

You'll learn how to prevent hidden elements from receiving keyboard focus using CSS and HTML attributes.

Why Hidden Elements Need Special Handling

When content is visually hidden but still in the DOM, screen readers and keyboard navigation can still access it—creating confusion when users tab to "invisible" elements. Your job is to ensure that when something shouldn't be seen, it also shouldn't be reached by keyboard.

Think of it like a museum after hours: not only are the lights off (visual hiding), but the doors are also locked (removing from tab order).

Three Techniques for Truly Hiding Content

display: none

When you set display: none, the element is completely removed from both the visual layout and the accessibility tree. It cannot receive focus.

.modal.hidden {
  display: none; /* Invisible AND unfocusable */
}

visibility: hidden

Similar to display: none, this hides the element and removes it from focus order. Unlike display, it still occupies layout space (like an invisible placeholder).

.dropdown.closed {
  visibility: hidden; /* Invisible AND unfocusable */
}

The hidden Attribute

HTML's hidden attribute does the same job as display: none and is semantically clearer:

<div class="tooltip" hidden>
  <!-- This content cannot be focused -->
</div>

When This Matters Most

Use these techniques for collapsed menus, closed modals, inactive tabs, or any content that users shouldn't interact with in its current state. If you hide something only with opacity: 0 or positioning off-screen, it remains focusable—a common accessibility bug.

Key Takeaway: Always hide interactive elements from the focus order when they're visually hidden using display: none, visibility: hidden, or the hidden attribute—never rely on visual-only techniques like opacity or positioning alone.