Status legend tooltips in HTML, CSS and vanilla JS
A copy-paste Status legend 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-visual-legend" data-dcl-scheme="light">
<h2>Status legend tooltips</h2>
<div data-grid data-card><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-visual-legend-ready" data-result="Ready visits selected.">Ready</button><span id="dcl-tooltip-visual-legend-ready" role="tooltip" data-card hidden>All required checks have passed.</span></div><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-visual-legend-hold" data-result="On-hold visits selected.">On hold</button><span id="dcl-tooltip-visual-legend-hold" role="tooltip" data-card hidden>Waiting for a confirmation.</span></div><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-visual-legend-done" data-result="Completed visits selected.">Complete</button><span id="dcl-tooltip-visual-legend-done" role="tooltip" data-card hidden>The visit was recorded and closed.</span></div></div><p role="status" data-status></p>
<script>
(() => {
const root = document.currentScript.closest('.dcl-tooltip-visual-legend');
const instance = Array.from(document.getElementsByClassName('dcl-tooltip-visual-legend')).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-visual-legend { 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-visual-legend *, .dcl-tooltip-visual-legend *::before, .dcl-tooltip-visual-legend *::after { box-sizing: border-box; }
.dcl-tooltip-visual-legend h2, .dcl-tooltip-visual-legend h3, .dcl-tooltip-visual-legend p, .dcl-tooltip-visual-legend ul, .dcl-tooltip-visual-legend ol, .dcl-tooltip-visual-legend figure { margin: 0; }
.dcl-tooltip-visual-legend h2 { font-size: 1.25rem; margin-bottom: .875rem; }
.dcl-tooltip-visual-legend h3 { font-size: 1rem; }
.dcl-tooltip-visual-legend li + li, .dcl-tooltip-visual-legend p + p { margin-top: .375rem; }
.dcl-tooltip-visual-legend ul, .dcl-tooltip-visual-legend ol { padding-inline-start: 1.35rem; }
.dcl-tooltip-visual-legend a { text-underline-offset: .18em; }
.dcl-tooltip-visual-legend a:hover { text-decoration-thickness: .18em; }
.dcl-tooltip-visual-legend :focus-visible { outline-style: solid; outline-width: 3px; outline-offset: 2px; }
.dcl-tooltip-visual-legend button, .dcl-tooltip-visual-legend summary, .dcl-tooltip-visual-legend input, .dcl-tooltip-visual-legend select { font: inherit; max-width: 100%; }
.dcl-tooltip-visual-legend button, .dcl-tooltip-visual-legend input, .dcl-tooltip-visual-legend select { padding: .35rem .65rem; border: 1px solid; border-radius: .375rem; }
.dcl-tooltip-visual-legend button, .dcl-tooltip-visual-legend summary, .dcl-tooltip-visual-legend select { cursor: pointer; }
.dcl-tooltip-visual-legend button:active, .dcl-tooltip-visual-legend summary:active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-tooltip-visual-legend button:disabled, .dcl-tooltip-visual-legend button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; }
.dcl-tooltip-visual-legend [hidden] { display: none !important; }
.dcl-tooltip-visual-legend [data-line], .dcl-tooltip-visual-legend [data-actions] { display: flex; gap: .5rem .75rem; align-items: center; flex-wrap: wrap; }
.dcl-tooltip-visual-legend [data-grid] { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,9rem),1fr)); gap: .75rem; }
.dcl-tooltip-visual-legend [data-stack], .dcl-tooltip-visual-legend [data-targets] { display: grid; gap: .75rem; }
.dcl-tooltip-visual-legend [data-targets] { border-top: 1px solid; padding-top: .75rem; margin-top: .75rem; }
.dcl-tooltip-visual-legend [data-card], .dcl-tooltip-visual-legend [data-status], .dcl-tooltip-visual-legend [data-panel] { padding: .75rem; border: 1px solid; border-radius: .5rem; }
.dcl-tooltip-visual-legend [data-status] { margin-top: .75rem; }
.dcl-tooltip-visual-legend [data-status]:empty { border-width: 0; padding: 0; margin: 0; }
.dcl-tooltip-visual-legend [aria-current] { font-weight: 750; }
.dcl-tooltip-visual-legend dialog, .dcl-tooltip-visual-legend [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-visual-legend dialog h3, .dcl-tooltip-visual-legend [popover] h3 { margin-bottom: .5rem; }
.dcl-tooltip-visual-legend dialog [data-actions], .dcl-tooltip-visual-legend [popover] [data-actions] { margin-top: .75rem; }
.dcl-tooltip-visual-legend svg { display: block; width: 100%; height: auto; }
.dcl-tooltip-visual-legend[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-tooltip-visual-legend[data-dcl-scheme="light"] a { color: #005c62; }
.dcl-tooltip-visual-legend[data-dcl-scheme="light"] button, .dcl-tooltip-visual-legend[data-dcl-scheme="light"] input, .dcl-tooltip-visual-legend[data-dcl-scheme="light"] select, .dcl-tooltip-visual-legend[data-dcl-scheme="light"] summary, .dcl-tooltip-visual-legend[data-dcl-scheme="light"] [data-card], .dcl-tooltip-visual-legend[data-dcl-scheme="light"] [data-status], .dcl-tooltip-visual-legend[data-dcl-scheme="light"] [data-panel] { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-tooltip-visual-legend[data-dcl-scheme="light"] button:hover, .dcl-tooltip-visual-legend[data-dcl-scheme="light"] summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-tooltip-visual-legend[data-dcl-scheme="light"] dialog, .dcl-tooltip-visual-legend[data-dcl-scheme="light"] [popover] { color: #11292f; background-color: #ffffff; border-color: #697f84; }
.dcl-tooltip-visual-legend[data-dcl-scheme="light"] :focus-visible { outline-color: #005c62; }
.dcl-tooltip-visual-legend[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-tooltip-visual-legend[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-tooltip-visual-legend[data-dcl-scheme="dark"] button, .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] input, .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] select, .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] summary, .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] [data-card], .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] [data-status], .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] [data-panel] { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-tooltip-visual-legend[data-dcl-scheme="dark"] button:hover, .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-tooltip-visual-legend[data-dcl-scheme="dark"] dialog, .dcl-tooltip-visual-legend[data-dcl-scheme="dark"] [popover] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; }
.dcl-tooltip-visual-legend[data-dcl-scheme="dark"] :focus-visible { outline-color: #8fe1da; }
.dcl-tooltip-visual-legend [data-tool] { position: relative; min-width: 0; }
.dcl-tooltip-visual-legend [role="tooltip"] { display: block; margin: 0; width: min(14rem,100%); padding: .35rem .55rem; font-size: .875rem; }
.dcl-tooltip-visual-legend [data-tool] button { max-width: 100%; }
.dcl-tooltip-visual-legend [aria-pressed="true"] { border-width: 2px; font-weight: 750; }
.dcl-tooltip-visual-legend [data-grid] { align-items: start; } .dcl-tooltip-visual-legend [data-tool] button { width: 100%; border-radius: 1rem; } .dcl-tooltip-visual-legend [role="tooltip"] { margin-top: .35rem; border-style: dashed; }
</style>
When to use
A horizontal row of labelled status controls gets short focus/hover legend hints below each control so hints don't cover the status itself. Replace the example content and wire the controls or destination IDs to your own screen.
Avoid
Don't make the legend the only source of a status or rely on color.
Accessibility
Keyboard focus is visibly outlined. Preserve normal Tab order and dismiss on Escape. The main text contrast is 15.2:1/13.1:1 in light/dark mode, respectively.
Was this useful? Send feedback.