brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: nest kbd elements to write keyboard shortcuts like Ctrl+Shift+P

Web-Development
~/tips/tip-kbd-element-key-combinations

kbd kbd {

Keyboard shortcuts get proper markup and can be styled as keys.

Tutorials are full of key combinations. The kbd element marks text the reader should type or press, and a little CSS turns it into something that looks like a key.

Press <kbd><kbd>Ctrl</kbd>+<kbd>Shift</kbd>+<kbd>P</kbd></kbd>
to open the command menu.

kbd kbd {
  padding: 0.1em 0.4em;
  border: 1px solid #d0d7de;
  border-bottom-width: 2px;
  border-radius: 4px;
  font-size: 0.85em;
}

MDN describes nesting for exactly this case: an outer kbd for the whole input and inner ones for each key. The CSS styles only the inner ones, so the plus signs stay plain text between the keys. Browsers show kbd in a monospace font by default; I’d keep that.

The element is also correct for typed text, like “type <kbd>yes</kbd> and press Enter”. For text the computer shows back, MDN suggests samp, and for code code.

The caveat: the element is about meaning and looks. A screen reader reads the content like normal text and does not announce “key”, so write the combination so it reads well aloud. “Ctrl+Shift+P” works. A lone symbol like ⌘ may be read in an odd way or skipped, so I’d write “Cmd” next to it.

Further reading: MDN: The kbd element

More Web-Development tips ab -n 200 -c 10 https://example.com/ console.assert(items.length > 0, 'cart is empty', items) document.designMode = "on" function save(item) { Save draft All Web-Development tips (67) All topics

Link