Accessibility (a11y) – HTML & CSS
Keyboard, screen readers, contrast and ARIA – making web pages usable for everyone.
Accessibility (a11y) means that people with visual, hearing, motor or cognitive impairments can use your site too – and it helps everyone (sunlight, a broken arm, keyboard operation). The WCAG guidelines name four principles: perceivable, operable, understandable, robust. First rule of ARIA: use a native HTML element if one exists (<button>, <a>, <label>, <dialog>) – native elements come with role, focus and keyboard control built in.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.feld { margin-bottom: 12px; }
label { display: block; font-weight: 600; }
input { padding: 6px; border: 2px solid #666; }
input[aria-invalid="true"] { border-color: #b00020; }
.fehler { color: #b00020; font-size: .9rem; }
</style>
<form novalidate onsubmit="event.preventDefault();var i=document.getElementById('mail'),e=document.getElementById('mail-fehler'),ok=i.value.indexOf('@')>0;i.setAttribute('aria-invalid',!ok);e.textContent=ok?'':'Please enter a valid email address.'">
<div class="feld">
<label for="mail">Email address</label>
<input id="mail" type="email" autocomplete="email" aria-describedby="mail-fehler">
<div id="mail-fehler" class="fehler" role="alert"></div>
</div>
<button>Submit</button>
</form>Keyboard & focus: everything interactive must be reachable and operable via Tab, and the focus must stay visible. Never remove outline without a replacement. :focus-visible shows the ring only for keyboard operation. A skip link jumps straight to the content; a "visually hidden" class hides text only visually (screen readers keep reading it).
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.skip { position: absolute; left: -999px; top: 8px; background: #000; color: #fff; padding: 8px 12px; z-index: 10; }
.skip:focus { left: 8px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
a, button { font: inherit; }
a:focus-visible, button:focus-visible { outline: 3px solid #ff9800; outline-offset: 3px; }
.icon-btn { background: #eee; border: 1px solid #999; border-radius: 8px; padding: 8px 12px; cursor: pointer; }
</style>
<a class="skip" href="#inhalt">Skip to content</a>
<nav><a href="#x">Home</a> · <a href="#y">Contact</a></nav>
<button class="icon-btn">🔍<span class="sr-only"> Open search</span></button>
<main id="inhalt" tabindex="-1"><p>Press Tab: first the skip link appears, then every focus with a ring.</p></main>Contrast: normal text needs at least 4.5:1 (WCAG AA), large text and UI components 3:1. Never rely on color alone (mark errors additionally with text or an icon). When something changes dynamically (error message, loading state), role="alert", role="status" or aria-live report it to screen readers. aria-expanded, aria-controls and aria-current describe states.
<!DOCTYPE html>
<style>body{font:16px system-ui,sans-serif;margin:12px;background:#fff;color:#222}
.tog { padding: 8px 12px; border: 2px solid #333; background: #fff; border-radius: 8px; cursor: pointer; }
.tog[aria-expanded="true"] { background: #222; color: #fff; }
#info { padding: 10px; background: #eef; margin-top: 8px; }
.gut { color: #1a1a1a; background: #fff; padding: 4px; } .schlecht { color: #aaa; background: #fff; padding: 4px; }
</style>
<button class="tog" aria-expanded="false" aria-controls="info"
onclick="var o=this.getAttribute('aria-expanded')==='true';this.setAttribute('aria-expanded',!o);document.getElementById('info').hidden=o">More info</button>
<div id="info" hidden>This region is linked to the button via <code>aria-expanded</code>.</div>
<p class="gut">Good contrast (approx. 17:1)</p><p class="schlecht">Poor contrast (approx. 2.3:1) – hard to read</p>alt texts (decorative images: alt=""), links saying "click here", <div> buttons, form fields without a label, color as the only information and a removed focus ring. Automated tests (Lighthouse, axe) find only about a third of the problems – also test with the keyboard and a screen reader (VoiceOver, NVDA).rem) so that user settings for font size take effect.