Component Library

← Back to gallery

Keyboard shortcut command map in HTML, CSS and vanilla JS

A copy-paste Keyboard shortcut command map in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.

Category: Data display · Tags: kbd, data-display, responsive

Live preview

Source (copy-ready)

<div class="dcl-kbd-shortcuts-command-map" data-dcl-scheme="light">
<header><p class="dcl-kbd-shortcuts-command-map-eyebrow">ARROW KEYS</p><h3>Move around a canvas</h3></header><dl class="dcl-kbd-shortcuts-command-map-map"><div class="dcl-kbd-shortcuts-command-map-well dcl-kbd-shortcuts-command-map-north"><dt><kbd>↑</kbd> Up</dt><dd>Move up</dd></div><div class="dcl-kbd-shortcuts-command-map-well dcl-kbd-shortcuts-command-map-west"><dt><kbd>←</kbd> Left</dt><dd>Move left</dd></div><div class="dcl-kbd-shortcuts-command-map-well dcl-kbd-shortcuts-command-map-south"><dt><kbd>↓</kbd> Down</dt><dd>Move down</dd></div><div class="dcl-kbd-shortcuts-command-map-well dcl-kbd-shortcuts-command-map-east"><dt><kbd>→</kbd> Right</dt><dd>Move right</dd></div></dl>
<script>
(() => {
  const root = document.currentScript.closest(".dcl-kbd-shortcuts-command-map");
  const preference = matchMedia("(prefers-color-scheme: dark)");
  function setScheme() { root.dataset.dclScheme = preference.matches ? "dark" : "light"; }
  preference.addEventListener("change", setScheme);
  setScheme();
})();
</script>
</div>
<style>
.dcl-kbd-shortcuts-command-map, .dcl-kbd-shortcuts-command-map * { box-sizing: border-box; }
.dcl-kbd-shortcuts-command-map { max-inline-size: 44rem; margin-inline: auto; padding: clamp(.875rem, 4vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui, sans-serif; overflow-wrap: anywhere; }
.dcl-kbd-shortcuts-command-map h3, .dcl-kbd-shortcuts-command-map h4, .dcl-kbd-shortcuts-command-map p, .dcl-kbd-shortcuts-command-map figure, .dcl-kbd-shortcuts-command-map dl, .dcl-kbd-shortcuts-command-map ul, .dcl-kbd-shortcuts-command-map ol { margin: 0; }
.dcl-kbd-shortcuts-command-map h3 { font-size: 1.18rem; line-height: 1.25; }
.dcl-kbd-shortcuts-command-map header { margin-block-end: .875rem; }
.dcl-kbd-shortcuts-command-map .dcl-kbd-shortcuts-command-map-eyebrow { font-size: .8125rem; font-weight: 700; letter-spacing: .06em; }
.dcl-kbd-shortcuts-command-map .dcl-kbd-shortcuts-command-map-well { padding: .7rem .8rem; border: 1px solid; border-radius: .55rem; }
.dcl-kbd-shortcuts-command-map .dcl-kbd-shortcuts-command-map-muted { font-size: .875rem; }
.dcl-kbd-shortcuts-command-map .dcl-kbd-shortcuts-command-map-row { display: flex; gap: .55rem .8rem; align-items: center; flex-wrap: wrap; }
.dcl-kbd-shortcuts-command-map .dcl-kbd-shortcuts-command-map-grid { display: grid; gap: .65rem; grid-template-columns: repeat(auto-fit, minmax(min(100%, 11rem), 1fr)); }
.dcl-kbd-shortcuts-command-map dt { font-size: .875rem; font-weight: 650; }
.dcl-kbd-shortcuts-command-map dd { margin: 0; }
.dcl-kbd-shortcuts-command-map .dcl-kbd-shortcuts-command-map-value { font-size: clamp(1.35rem, 5vw, 1.9rem); font-weight: 750; font-variant-numeric: tabular-nums; line-height: 1.2; }
.dcl-kbd-shortcuts-command-map button, .dcl-kbd-shortcuts-command-map summary, .dcl-kbd-shortcuts-command-map input { font: inherit; }
.dcl-kbd-shortcuts-command-map button, .dcl-kbd-shortcuts-command-map summary { min-height: 2.75rem; cursor: pointer; }
.dcl-kbd-shortcuts-command-map button { padding: .35rem .7rem; border: 1px solid; border-radius: .4rem; }
.dcl-kbd-shortcuts-command-map button:active { transform: translateY(1px); }
.dcl-kbd-shortcuts-command-map button:disabled { cursor: default; border-style: dashed; }
.dcl-kbd-shortcuts-command-map [hidden] { display: none; }

.dcl-kbd-shortcuts-command-map kbd { font:650 .875rem/1.25 ui-monospace, monospace; display:inline-block; padding:.27rem .42rem; border:1px solid; border-block-end-width:.15rem; border-radius:.28rem; }
.dcl-kbd-shortcuts-command-map-chord { display:inline-flex; flex-wrap:wrap; gap:.25rem; align-items:center; }
.dcl-kbd-shortcuts-command-map-map { display:grid; grid-template-columns:repeat(3,minmax(0,1fr)); gap:.35rem; text-align:center; } .dcl-kbd-shortcuts-command-map-map .dcl-kbd-shortcuts-command-map-well { padding:.4rem .2rem; } .dcl-kbd-shortcuts-command-map-map dd { font-size:.8rem; } .dcl-kbd-shortcuts-command-map-north { grid-column:2; } .dcl-kbd-shortcuts-command-map-west { grid-column:1; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] .dcl-kbd-shortcuts-command-map-muted, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] .dcl-kbd-shortcuts-command-map-eyebrow { color: #405a60; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] .dcl-kbd-shortcuts-command-map-well, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] button, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] kbd { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] .dcl-kbd-shortcuts-command-map-accent, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] button:hover { color: #ffffff; background-color: #00535a; border-color: #00535a; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] :focus-visible { outline: 3px solid #005c62; outline-offset: 2px; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] meter, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] progress, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="light"] input { accent-color: #00535a; }

.dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] .dcl-kbd-shortcuts-command-map-muted, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] .dcl-kbd-shortcuts-command-map-eyebrow { color: #b0c9ce; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] .dcl-kbd-shortcuts-command-map-well, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] button, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] kbd { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] .dcl-kbd-shortcuts-command-map-accent, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] button:hover { color: #11292f; background-color: #8fe1da; border-color: #8fe1da; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] :focus-visible { outline: 3px solid #8fe1da; outline-offset: 2px; }
.dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] meter, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] progress, .dcl-kbd-shortcuts-command-map[data-dcl-scheme="dark"] input { accent-color: #8fe1da; }
</style>

When to use

Use a key map when spatial layout helps people remember directional commands. Write the key name and the action in text.

Avoid

Do not use the map for actions that lack a spatial relationship.

Accessibility

A definition list reads each key name with its action; the commands are descriptive, not active. Main text contrast is 15.2:1 in light and 13.1:1 in dark.

Was this useful? Send feedback.