Command strip tooltips in HTML, CSS and vanilla JS
A copy-paste Command strip tooltips in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-tooltip-command-strip" data-dcl-scheme="light">
<h2>Command strip tooltips</h2>
<div data-grid><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-command-strip-pin" data-result="Note pinned.">Pin note</button><span id="dcl-tooltip-command-strip-pin" role="tooltip" data-card hidden>Keep this note at the top of the list.</span></div><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-command-strip-mark" data-result="Note marked for review.">Mark for review</button><span id="dcl-tooltip-command-strip-mark" role="tooltip" data-card hidden>Add the note to the next review.</span></div><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-command-strip-archive" data-result="Note archived.">Archive note</button><span id="dcl-tooltip-command-strip-archive" role="tooltip" data-card hidden>Move this note out of the active list.</span></div></div><p role="status" data-status></p>
<script>
(() => {
const root = document.currentScript.closest('.dcl-tooltip-command-strip');
const instance = Array.from(document.getElementsByClassName('dcl-tooltip-command-strip')).indexOf(root);
if (instance) {
const suffix = '--' + instance;
const attributes = ['id','aria-labelledby','aria-describedby','aria-controls','for','name','popovertarget'];
for (const element of root.querySelectorAll('*')) {
for (const attr of attributes) {
if (element.hasAttribute(attr)) element.setAttribute(attr, element.getAttribute(attr).split(/\s+/).map(value => value + suffix).join(' '));
}
const href = element.getAttribute('href');
if (href && href.startsWith('#')) element.setAttribute('href', href + suffix);
}
}
const media = matchMedia('(prefers-color-scheme: dark)');
const theme = () => { root.dataset.dclScheme = media.matches ? 'dark' : 'light'; };
media.addEventListener('change', theme);
theme();
const slots = [...root.querySelectorAll('[data-tool]')];
let active;
const hide = slot => {
if (!slot) return;
slot.querySelector('[role="tooltip"]').hidden = true;
if (active === slot) active = null;
};
for (const slot of slots) {
const trigger = slot.querySelector('button');
let timer, hover = false, focused = false;
const clear = () => clearTimeout(timer);
const show = () => {
if (active !== slot) hide(active);
active = slot;
slot.querySelector('[role="tooltip"]').hidden = false;
};
slot.addEventListener('pointerenter', event => {
if (event.pointerType === 'touch') return;
hover = true;
clear();
const delay = Number(root.dataset.hoverDelay || 0);
if (delay) timer = setTimeout(show, delay);
else show();
});
slot.addEventListener('pointerleave', () => { hover = false; clear(); if (!focused) hide(slot); });
trigger.addEventListener('focus', () => { focused = true; clear(); show(); });
trigger.addEventListener('blur', () => { focused = false; clear(); if (!hover) hide(slot); });
trigger.addEventListener('click', () => {
if (trigger.getAttribute('aria-disabled') === 'true') return;
const status = root.querySelector('[data-status]');
if (status) status.textContent = trigger.dataset.result;
});
document.addEventListener('keydown', event => { if (event.key === 'Escape') { clear(); hide(slot); } });
}
})();
</script>
</section>
<style>
.dcl-tooltip-command-strip { box-sizing: border-box; max-width: 58rem; padding: clamp(.75rem, 3vw, 1.5rem); border: 1px solid; border-radius: .75rem; font: 1rem/1.5 system-ui,sans-serif; overflow-wrap: anywhere; }
.dcl-tooltip-command-strip *, .dcl-tooltip-command-strip *::before, .dcl-tooltip-command-strip *::after { box-sizing: border-box; }
.dcl-tooltip-command-strip h2, .dcl-tooltip-command-strip h3, .dcl-tooltip-command-strip p, .dcl-tooltip-command-strip ul, .dcl-tooltip-command-strip ol, .dcl-tooltip-command-strip figure { margin: 0; }
.dcl-tooltip-command-strip h2 { font-size: 1.25rem; margin-bottom: .875rem; }
.dcl-tooltip-command-strip h3 { font-size: 1rem; }
.dcl-tooltip-command-strip li + li, .dcl-tooltip-command-strip p + p { margin-top: .375rem; }
.dcl-tooltip-command-strip ul, .dcl-tooltip-command-strip ol { padding-inline-start: 1.35rem; }
.dcl-tooltip-command-strip a { text-underline-offset: .18em; }
.dcl-tooltip-command-strip a:hover { text-decoration-thickness: .18em; }
.dcl-tooltip-command-strip :focus-visible { outline-style: solid; outline-width: 3px; outline-offset: 2px; }
.dcl-tooltip-command-strip button, .dcl-tooltip-command-strip summary, .dcl-tooltip-command-strip input, .dcl-tooltip-command-strip select { font: inherit; max-width: 100%; }
.dcl-tooltip-command-strip button, .dcl-tooltip-command-strip input, .dcl-tooltip-command-strip select { padding: .35rem .65rem; border: 1px solid; border-radius: .375rem; }
.dcl-tooltip-command-strip button, .dcl-tooltip-command-strip summary, .dcl-tooltip-command-strip select { cursor: pointer; }
.dcl-tooltip-command-strip button:active, .dcl-tooltip-command-strip summary:active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-tooltip-command-strip button:disabled, .dcl-tooltip-command-strip button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; }
.dcl-tooltip-command-strip [hidden] { display: none !important; }
.dcl-tooltip-command-strip [data-line], .dcl-tooltip-command-strip [data-actions] { display: flex; gap: .5rem .75rem; align-items: center; flex-wrap: wrap; }
.dcl-tooltip-command-strip [data-grid] { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,9rem),1fr)); gap: .75rem; }
.dcl-tooltip-command-strip [data-stack], .dcl-tooltip-command-strip [data-targets] { display: grid; gap: .75rem; }
.dcl-tooltip-command-strip [data-targets] { border-top: 1px solid; padding-top: .75rem; margin-top: .75rem; }
.dcl-tooltip-command-strip [data-card], .dcl-tooltip-command-strip [data-status], .dcl-tooltip-command-strip [data-panel] { padding: .75rem; border: 1px solid; border-radius: .5rem; }
.dcl-tooltip-command-strip [data-status] { margin-top: .75rem; }
.dcl-tooltip-command-strip [data-status]:empty { border-width: 0; padding: 0; margin: 0; }
.dcl-tooltip-command-strip [aria-current] { font-weight: 750; }
.dcl-tooltip-command-strip dialog, .dcl-tooltip-command-strip [popover] { max-height: calc(100dvh - 2rem); max-width: calc(100% - 2rem); width: min(36rem,100%); border: 1px solid; border-radius: .75rem; padding: 1rem; overflow: auto; }
.dcl-tooltip-command-strip dialog h3, .dcl-tooltip-command-strip [popover] h3 { margin-bottom: .5rem; }
.dcl-tooltip-command-strip dialog [data-actions], .dcl-tooltip-command-strip [popover] [data-actions] { margin-top: .75rem; }
.dcl-tooltip-command-strip svg { display: block; width: 100%; height: auto; }
.dcl-tooltip-command-strip[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-tooltip-command-strip[data-dcl-scheme="light"] a { color: #005c62; }
.dcl-tooltip-command-strip[data-dcl-scheme="light"] button, .dcl-tooltip-command-strip[data-dcl-scheme="light"] input, .dcl-tooltip-command-strip[data-dcl-scheme="light"] select, .dcl-tooltip-command-strip[data-dcl-scheme="light"] summary, .dcl-tooltip-command-strip[data-dcl-scheme="light"] [data-card], .dcl-tooltip-command-strip[data-dcl-scheme="light"] [data-status], .dcl-tooltip-command-strip[data-dcl-scheme="light"] [data-panel] { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-tooltip-command-strip[data-dcl-scheme="light"] button:hover, .dcl-tooltip-command-strip[data-dcl-scheme="light"] summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-tooltip-command-strip[data-dcl-scheme="light"] dialog, .dcl-tooltip-command-strip[data-dcl-scheme="light"] [popover] { color: #11292f; background-color: #ffffff; border-color: #697f84; }
.dcl-tooltip-command-strip[data-dcl-scheme="light"] :focus-visible { outline-color: #005c62; }
.dcl-tooltip-command-strip[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-tooltip-command-strip[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-tooltip-command-strip[data-dcl-scheme="dark"] button, .dcl-tooltip-command-strip[data-dcl-scheme="dark"] input, .dcl-tooltip-command-strip[data-dcl-scheme="dark"] select, .dcl-tooltip-command-strip[data-dcl-scheme="dark"] summary, .dcl-tooltip-command-strip[data-dcl-scheme="dark"] [data-card], .dcl-tooltip-command-strip[data-dcl-scheme="dark"] [data-status], .dcl-tooltip-command-strip[data-dcl-scheme="dark"] [data-panel] { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-tooltip-command-strip[data-dcl-scheme="dark"] button:hover, .dcl-tooltip-command-strip[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-tooltip-command-strip[data-dcl-scheme="dark"] dialog, .dcl-tooltip-command-strip[data-dcl-scheme="dark"] [popover] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; }
.dcl-tooltip-command-strip[data-dcl-scheme="dark"] :focus-visible { outline-color: #8fe1da; }
.dcl-tooltip-command-strip [data-tool] { position: relative; min-width: 0; }
.dcl-tooltip-command-strip [role="tooltip"] { display: block; margin: 0; width: min(14rem,100%); padding: .35rem .55rem; font-size: .875rem; }
.dcl-tooltip-command-strip [data-tool] button { max-width: 100%; }
.dcl-tooltip-command-strip [aria-pressed="true"] { border-width: 2px; font-weight: 750; }
.dcl-tooltip-command-strip [data-grid] { align-items: start; }
</style>
When to use
Multiple tool buttons open adjacent tooltips, one at a time, for supplementary shortcut hints. Replace the example content and wire the controls or destination IDs to your own screen.
Avoid
The control's action must still be named by its own text or accessible label.
Accessibility
Keyboard focus is visibly outlined. The main text contrast is 15.2:1/13.1:1 in light/dark mode, respectively.
Was this useful? Send feedback.