Tooltip for an unavailable action in HTML, CSS and vanilla JS
A copy-paste Tooltip for an unavailable action in plain HTML and CSS, with vanilla JavaScript: no framework, no build step, MIT-licensed.
Live preview
Source (copy-ready)
<section class="dcl-tooltip-disabled-reason" data-dcl-scheme="light">
<h2>Tooltip for an unavailable action</h2>
<p>Publish checklist · approval pending</p><div data-tool><button type="button" data-tooltip-trigger aria-describedby="dcl-tooltip-disabled-reason-blocked" aria-disabled="true">Publish now</button><span id="dcl-tooltip-disabled-reason-blocked" role="tooltip" data-card hidden>A reviewer must approve the checklist first.</span></div>
<script>
(() => {
const root = document.currentScript.closest('.dcl-tooltip-disabled-reason');
const instance = Array.from(document.getElementsByClassName('dcl-tooltip-disabled-reason')).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-disabled-reason { 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-disabled-reason *, .dcl-tooltip-disabled-reason *::before, .dcl-tooltip-disabled-reason *::after { box-sizing: border-box; }
.dcl-tooltip-disabled-reason h2, .dcl-tooltip-disabled-reason h3, .dcl-tooltip-disabled-reason p, .dcl-tooltip-disabled-reason ul, .dcl-tooltip-disabled-reason ol, .dcl-tooltip-disabled-reason figure { margin: 0; }
.dcl-tooltip-disabled-reason h2 { font-size: 1.25rem; margin-bottom: .875rem; }
.dcl-tooltip-disabled-reason h3 { font-size: 1rem; }
.dcl-tooltip-disabled-reason li + li, .dcl-tooltip-disabled-reason p + p { margin-top: .375rem; }
.dcl-tooltip-disabled-reason ul, .dcl-tooltip-disabled-reason ol { padding-inline-start: 1.35rem; }
.dcl-tooltip-disabled-reason a { text-underline-offset: .18em; }
.dcl-tooltip-disabled-reason a:hover { text-decoration-thickness: .18em; }
.dcl-tooltip-disabled-reason :focus-visible { outline-style: solid; outline-width: 3px; outline-offset: 2px; }
.dcl-tooltip-disabled-reason button, .dcl-tooltip-disabled-reason summary, .dcl-tooltip-disabled-reason input, .dcl-tooltip-disabled-reason select { font: inherit; max-width: 100%; }
.dcl-tooltip-disabled-reason button, .dcl-tooltip-disabled-reason input, .dcl-tooltip-disabled-reason select { padding: .35rem .65rem; border: 1px solid; border-radius: .375rem; }
.dcl-tooltip-disabled-reason button, .dcl-tooltip-disabled-reason summary, .dcl-tooltip-disabled-reason select { cursor: pointer; }
.dcl-tooltip-disabled-reason button:active, .dcl-tooltip-disabled-reason summary:active { box-shadow: inset 0 0 0 2px currentColor; }
.dcl-tooltip-disabled-reason button:disabled, .dcl-tooltip-disabled-reason button[aria-disabled="true"] { cursor: not-allowed; border-style: dashed; }
.dcl-tooltip-disabled-reason [hidden] { display: none !important; }
.dcl-tooltip-disabled-reason [data-line], .dcl-tooltip-disabled-reason [data-actions] { display: flex; gap: .5rem .75rem; align-items: center; flex-wrap: wrap; }
.dcl-tooltip-disabled-reason [data-grid] { display: grid; grid-template-columns: repeat(auto-fit,minmax(min(100%,9rem),1fr)); gap: .75rem; }
.dcl-tooltip-disabled-reason [data-stack], .dcl-tooltip-disabled-reason [data-targets] { display: grid; gap: .75rem; }
.dcl-tooltip-disabled-reason [data-targets] { border-top: 1px solid; padding-top: .75rem; margin-top: .75rem; }
.dcl-tooltip-disabled-reason [data-card], .dcl-tooltip-disabled-reason [data-status], .dcl-tooltip-disabled-reason [data-panel] { padding: .75rem; border: 1px solid; border-radius: .5rem; }
.dcl-tooltip-disabled-reason [data-status] { margin-top: .75rem; }
.dcl-tooltip-disabled-reason [data-status]:empty { border-width: 0; padding: 0; margin: 0; }
.dcl-tooltip-disabled-reason [aria-current] { font-weight: 750; }
.dcl-tooltip-disabled-reason dialog, .dcl-tooltip-disabled-reason [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-disabled-reason dialog h3, .dcl-tooltip-disabled-reason [popover] h3 { margin-bottom: .5rem; }
.dcl-tooltip-disabled-reason dialog [data-actions], .dcl-tooltip-disabled-reason [popover] [data-actions] { margin-top: .75rem; }
.dcl-tooltip-disabled-reason svg { display: block; width: 100%; height: auto; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="light"] { color: #11292f; background-color: #ffffff; border-color: #697f84; color-scheme: light; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="light"] a { color: #005c62; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="light"] button, .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] input, .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] select, .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] summary, .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] [data-card], .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] [data-status], .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] [data-panel] { color: #11292f; background-color: #e4edef; border-color: #697f84; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="light"] button:hover, .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] summary:hover { color: #ffffff; background-color: #00535a; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="light"] dialog, .dcl-tooltip-disabled-reason[data-dcl-scheme="light"] [popover] { color: #11292f; background-color: #ffffff; border-color: #697f84; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="light"] :focus-visible { outline-color: #005c62; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; color-scheme: dark; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] a { color: #8fe1da; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] button, .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] input, .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] select, .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] summary, .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] [data-card], .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] [data-status], .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] [data-panel] { color: #eff8f9; background-color: #1c3c44; border-color: #839fa5; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] button:hover, .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] summary:hover { color: #11292f; background-color: #8fe1da; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] dialog, .dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] [popover] { color: #eff8f9; background-color: #142f36; border-color: #839fa5; }
.dcl-tooltip-disabled-reason[data-dcl-scheme="dark"] :focus-visible { outline-color: #8fe1da; }
.dcl-tooltip-disabled-reason [data-tool] { position: relative; min-width: 0; }
.dcl-tooltip-disabled-reason [role="tooltip"] { display: block; margin: 0; width: min(14rem,100%); padding: .35rem .55rem; font-size: .875rem; }
.dcl-tooltip-disabled-reason [data-tool] button { max-width: 100%; }
.dcl-tooltip-disabled-reason [aria-pressed="true"] { border-width: 2px; font-weight: 750; }
.dcl-tooltip-disabled-reason [data-tool] { margin-top: .75rem; }
</style>
When to use
A focusable aria-disabled control explains a temporary restriction without executing the action. Replace the example content and wire the controls or destination IDs to your own screen.
Avoid
Show remediation inline if necessary.
Accessibility
Keyboard focus is visibly outlined. Prevent activation by both pointer and keyboard. The main text contrast is 15.2:1/13.1:1 in light/dark mode, respectively.
Was this useful? Send feedback.