Clean Markup

Preventing Obscured Keyboard Focus under Sticky Headers

Preventing Obscured Keyboard Focus under Sticky Headers

Sticky navigation bars, persistent toolbars, and floating cookie banners have become ubiquitous in modern web layouts, yet they routinely introduce a subtle keyboard accessibility barrier. When a user navigates down or back up a long page using the Tab key or follows an in-page anchor link, the browser automatically scrolls the newly focused control into the viewport. Because the browser calculates visibility against the raw layout viewport rather than subtracting fixed or position: sticky overlays, the focused link or input often lands directly behind an opaque header or footer, leaving keyboard-only users unable to see where focus sits.

WCAG 2.2 formally codified this problem through Success Criterion 2.4.11 (Focus Not Obscured - Minimum, Level AA) and 2.4.12 (Focus Not Obscured - Enhanced, Level AAA), requiring that author-created content never hides a keyboard-focused component entirely—or even partially at Level AAA. Rather than intercepting focusin events to manually compute bounding rectangles and call window.scrollBy, CSS provides a native, declarative mechanism: scroll-padding on the scroll container paired with scroll-margin on target regions. When combined with a lightweight ResizeObserver for headers whose height changes across breakpoints, browsers automatically offset both anchor navigation and keyboard focus scrolling so every interactive element remains clearly visible.

Example

/* Reserve space on the root scrollport for sticky top and bottom bars */
:root {
  --sticky-header-height: 4.5rem;
  --sticky-banner-height: 0px;

  scroll-padding-block-start: calc(var(--sticky-header-height) + 1rem);
  scroll-padding-block-end: calc(var(--sticky-banner-height) + 1rem);
}

/* Give anchored headings extra breathing room beneath the header */
[id] {
  scroll-margin-block-start: 1rem;
}

/* Ensure high-contrast focus outlines never clip under adjacent siblings */
:focus-visible {
  outline: 3px solid #2563eb;
  outline-offset: 3px;
  position: relative;
  z-index: 1;
}

Implementation

<header class="site-header" data-sticky-header>
  <a class="skip-link" href="#main-content">Skip to main content</a>
  <nav aria-label="Primary">
    <a href="#overview">Overview</a>
    <a href="#settings">Settings</a>
    <a href="#billing">Billing</a>
  </nav>
</header>

<main id="main-content" tabindex="-1">
  <section id="overview" aria-labelledby="overview-heading">
    <h2 id="overview-heading">Account Overview</h2>
    <p>Content and interactive controls stay clear of the sticky header.</p>
    <button type="button">Edit Profile</button>
  </section>

  <section id="settings" aria-labelledby="settings-heading">
    <h2 id="settings-heading">Notification Settings</h2>
    <label>
      <input type="checkbox" name="digest" />
      Weekly summary email
    </label>
  </section>
</main>
/* ==========================================================================
   WCAG 2.2 Focus Not Obscured: Scroll Padding & Focus Layering
   ========================================================================== */

:root {
  --header-offset: 64px;
  --footer-offset: 0px;
  --focus-clearance: 16px;

  scroll-padding-block-start: calc(var(--header-offset) + var(--focus-clearance));
  scroll-padding-block-end: calc(var(--footer-offset) + var(--focus-clearance));
}

@media (prefers-reduced-motion: no-preference) {
  :root {
    scroll-behavior: smooth;
  }
}

.site-header {
  position: sticky;
  inset-block-start: 0;
  z-index: 100;
  background: #ffffff;
  border-block-end: 1px solid #e2e8f0;
  padding: 0.75rem 1.5rem;
}

/* Skip link remains offscreen until keyboard-focused */
.skip-link {
  position: absolute;
  inset-inline-start: 1rem;
  inset-block-start: -100%;
  padding: 0.5rem 0.875rem;
  background: #0f172a;
  color: #ffffff;
  border-radius: 0.375rem;
}

.skip-link:focus-visible {
  inset-block-start: 0.75rem;
  z-index: 110;
}

/* Prevent focus rings from being masked by neighboring grid/flex items */
:is(a, button, input, select, textarea, [tabindex]):focus-visible {
  outline: 3px solid #1d4ed8;
  outline-offset: 2px;
  position: relative;
  z-index: 10;
}

/* Keep scroll-margin additive for section anchors */
main :is(h2, h3, section[id]) {
  scroll-margin-block-start: 0.75rem;
}
/**
 * Synchronizes CSS scroll-padding variables with live sticky overlay dimensions
 * so responsive wrapping or dynamic banners never obscure focused controls.
 */
function syncStickyScrollOffsets({ headerSelector, footerSelector } = {}) {
  const root = document.documentElement;
  const header = headerSelector ? document.querySelector(headerSelector) : null;
  const footer = footerSelector ? document.querySelector(footerSelector) : null;

  const updateOffsets = () => {
    const headerHeight = header ? Math.ceil(header.getBoundingClientRect().height) : 0;
    const footerHeight = footer ? Math.ceil(footer.getBoundingClientRect().height) : 0;

    root.style.setProperty('--header-offset', `${headerHeight}px`);
    root.style.setProperty('--footer-offset', `${footerHeight}px`);
  };

  updateOffsets();

  if (typeof ResizeObserver === 'function') {
    const observer = new ResizeObserver(updateOffsets);
    if (header) observer.observe(header);
    if (footer) observer.observe(footer);
    return () => observer.disconnect();
  }

  window.addEventListener('resize', updateOffsets, { passive: true });
  return () => window.removeEventListener('resize', updateOffsets);
}

syncStickyScrollOffsets({
  headerSelector: '[data-sticky-header]',
  footerSelector: '[data-sticky-footer]'
});

Main Point: Combining CSS scroll-padding on the scroll container with ResizeObserver-synced offset variables prevents sticky headers and floating bars from hiding keyboard focus, satisfying WCAG 2.2 Focus Not Obscured requirements natively.

Credits

Photo by Andrii Chuzhynov on Unsplash

← Home