On this page

Code block

Keys, what Code block gives you and what your team does.

Conformance

Code block meets WCAG 2.2 level AA when used as described on these pages.

Keyboard

Key Action
Tab Moves focus to the code, then to its buttons. Floating actions show when focus is inside the block.
Arrow keys Scroll the focused code.
Enter or Space Copies, on the copy button.

What Code block gives you

  • The code can take focus, so keyboard users can scroll it; the whole block shows the focus outline.
  • Copy button: a name from its tooltip, and a polite live region that says “Copied”.
  • Line numbers and diff signs are not selected or copied with the code.
  • Highlighting colours are the editor roles, made to sit on editor.background in light and dark. Their pairs are not yet in the site’s contrast check.
  • dir="ltr" on the code, also on right-to-left pages.

What your team does

  • Give the copy button a name that says what it copies, and translate label and copiedLabel.
  • In a diff, keep the + and − signs; don’t mark lines by colour alone.
  • Put a title or a sentence before the block that says what the code is.
  • Don’t put the only copy of an error people must act on in a code block; say it in words too.

WCAG criteria

Criterion Level How
1.4.1 Use of Color A Diff lines have signs; an error block has a title in words
2.1.1 Keyboard A The code takes focus and scrolls with the arrow keys
2.4.7 Focus Visible AA The Button focus outline around the block and on each button
4.1.2 Name, Role, Value A The copy button is named by its tooltip
4.1.3 Status Messages AA “Copied” in a polite live region