← Back to all articles Performance & accessibility 4 min

WCAG web accessibility and WebP/AVIF media optimization without layout shift

Combine digital accessibility (WCAG) with lightweight imagery: WebP and AVIF formats, explicit width/height attributes to prevent CLS, strict Hn order, and meaningful alt text.

An accessible, visually stable website serves visitors with visual or motor impairments, mobile users reading outdoors, and search crawlers parsing document hierarchy.

Yet many websites still ship low-contrast text, headings chosen purely for font size, and unoptimized images causing jarring layout shifts (CLS). Here are the engineering rules to apply on every build.

1. Semantic HTML landmarks and strict heading hierarchy

Screen readers (NVDA, VoiceOver) rely on HTML5 landmarks (<header>, <nav>, <main>, <article>, <footer>) and heading outlines to navigate long pages efficiently.

Follow four straightforward rules:

  • One single <h1> per page defining the primary topic cleanly aligned with <title>.
  • Sequential hierarchy (H1 → H2 → H3) without skipping levels for styling reasons—control font sizes via CSS classes rather than misusing <h4>.
  • Native interactive elements: use <button> for in-page actions and <a href="..."> for URL navigation rather than non-focusable clickable <div> elements.
  • Explicit labels: bind every form input to a <label> and add descriptive aria-label attributes on icon-only controls.

2. WCAG AA color contrast and keyboard focus management

WCAG 2.1 Level AA sets clear contrast thresholds so copy stays legible in both light and dark themes:

  • At least 4.5:1 contrast ratio for normal body text against its background.
  • At least 3:1 contrast ratio for large headings, meaningful UI icons, and form input borders.
  • Visible keyboard focus indicators (:focus-visible) preserved across Tab navigation without sticky headers obscuring focused controls.

3. Modern WebP/AVIF formats, explicit dimensions, and zero CLS

Images frequently account for more than half of total transferred page bytes. Technical optimization requires balancing smaller payloads with strict layout stability:

Media ruleAttribute or formatUser & SEO benefit
Modern codecsAVIF and WebP inside <picture> or <img>Substantially smaller byte payloads than legacy JPEG/PNG
Space reservationExplicit width + height and CSS aspect-ratioPrevents layout shift (CLS close to 0)
Deferred loadingloading="lazy" on below-the-fold imageryPrioritizes bandwidth for immediate viewport elements
Alternative textFactual descriptive alt (alt="" if purely decorative)Screen-reader clarity and Google Image context

4. Writing useful alt attributes without keyword stuffing

An alt attribute is not a container for repeating SEO keyword lists: it must describe what the visual depicts for users unable to view the graphic.

  • Informative photo or UI screenshot: state what is shown concisely (e.g., alt="ZenTime leave-management dashboard interface").
  • Purely decorative graphic: assign an empty alt="" so assistive screen readers skip it smoothly.
  • Avoid lazy-loading above the fold: never apply loading="lazy" on the main hero image at the top of the viewport, as this delays Largest Contentful Paint (LCP).
  • ✓ Single <h1> per page with uninterrupted H2 / H3 nesting
  • ✓ Images exported in WebP or AVIF with width, height, and decoding="async"
  • ✓ loading="lazy" reserved strictly for images below the initial fold
  • ✓ Light and dark theme contrast ratios verified against WCAG AA baselines

Key takeaway

Pairing WCAG accessibility standards with disciplined WebP/AVIF media handling delivers interfaces that remain legible across devices, free of layout shifts, and clearer for search crawlers.

Frequently asked questions

Should loading="lazy" be applied to every image on a page?

No. Above-the-fold visuals such as hero banners and header logos should load eagerly without loading="lazy" to protect LCP. Reserve loading="lazy" for offscreen images further down the document.

Why set width and height HTML attributes when CSS uses w-full h-auto?

HTML width and height attributes allow browsers to compute the intrinsic aspect ratio before image bytes arrive. Combined with height: auto, the layout reserves exact vertical space immediately, preventing CLS jumps.

Ali Ouarzazi

Web developer in Marrakech committed to WCAG accessibility, semantic HTML, and disciplined front-end media engineering.

✨ Professional services

Services & technical solutions related to this guide

Discover tailored engineering services by Ali Ouarzazi to implement these strategies:

Ready to discuss your web project?

Direct exchange with Ali Ouarzazi. Clear itemized quote.