WCAG 2.2 – Levels A and AA only, with dev notes.
Enhancing the keyboard focus ring.
Use the keyboard TAB key to navigate through the test elements.
Uses :focus-visible where supported and falls back to :focus where not.
:focus-visible
:focus
Exposed details.
/* Optional: override the defaults */ body { --FocusRing-outline-color: blue; --FocusRing-outline-width: 2px; --FocusRing-outline-offset: 2px; --FocusRing-transition-offset: 5px; } /* Required for focus animation */ @media (prefers-reduced-motion: no-preference) { body { --FocusRing-transition-duration: 0.3s; } }
a, button, summary, [tabindex], input, select, textarea, [contenteditable] { outline: var(--FocusRing-outline-width, 2px) solid transparent; outline-offset: var(--FocusRing-transition-offset, 5px); transition: outline-offset var(--FocusRing-transition-duration, 0) ease-out; } a:focus, button:focus, summary:focus, [tabindex]:focus, input:focus, select:focus, textarea:focus, [contenteditable]:focus { outline-color: var(--FocusRing-outline-color, blue); outline-offset: var(--FocusRing-outline-offset, 2px); } @supports selector(:focus-visible) { a:focus:not(:focus-visible), button:focus:not(:focus-visible), summary:focus:not(:focus-visible), [tabindex]:focus:not(:focus-visible), input:focus:not(:focus-visible), select:focus:not(:focus-visible), textarea:focus:not(:focus-visible), [contenteditable]:focus:not(:focus-visible) { outline: none; } } :focus-visible { outline-color: var(--FocusRing-outline-color, blue); outline-offset: var(--FocusRing-outline-offset, 2px); }
Much cleaner CSS if your supported browser matrix supports :is()
:is()
Supported browsers: Chrome 88, Edge 88, Safari 15.1, Firefox 78, Opera 75, Safari iOS 14, Samsung 15, Opera mobile 15, Android 105
:is(a[href], button, summary, [tabindex], input, select, textarea, [contenteditable]) { outline: var(--FocusRing-outline-width, 2px) solid transparent; outline-offset: var(--FocusRing-transition-offset, 5px); transition: outline-offset var(--FocusRing-transition-duration, 0) ease-out; } :is(a[href], button, summary, [tabindex], input, select, textarea, [contenteditable]):focus { outline-color: var(--FocusRing-outline-color, blue); outline-offset: var(--FocusRing-outline-offset, 2px); } @supports selector(:focus-visible) { :is(a[href], button, summary, [tabindex], input, select, textarea, [contenteditable]):focus:not(:focus-visible) { outline: none; } } :focus-visible { outline-color: var(--FocusRing-outline-color, blue); outline-offset: var(--FocusRing-outline-offset, 2px); }