Skip to the main content

Tooltip and Popover

Free Feedback

A hint on hover and keyboard focus and a click-to-open popover built on the native popover attribute, with Escape to close either.

Checked on 3 December 2026 in Chrome 143.0.7499.169: 12 automated checks. What the checks are

Preview and code

HTML

<div class="tp-page">
<section class="tp" id="tp-root" aria-labelledby="tp-title">
  <h2 id="tp-title" class="tp__title">Hints and popovers</h2>
  <p class="tp__lead">Hover or focus a button for its hint.</p>

  <div class="tp__bar" role="toolbar" aria-label="Text tools (example)">
    <span class="tp__wrap">
      <button type="button" class="tp__tool" id="tp-bold" aria-label="Bold" aria-describedby="tp-tip-bold">
        <svg viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false"><path d="M6 4h4.5a3 3 0 010 6H6zM6 10h5.2a3 3 0 010 6H6z" fill="none" stroke="currentColor" stroke-width="2" stroke-linejoin="round"/></svg>
      </button>
      <span class="tp__tip" role="tooltip" id="tp-tip-bold">Bold <kbd>Ctrl</kbd> <kbd>B</kbd></span>
    </span>
    <span class="tp__wrap">
      <button type="button" class="tp__tool" id="tp-italic" aria-label="Italic" aria-describedby="tp-tip-italic">
        <svg viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false"><path d="M8 4h6M6 16h6M11.5 4l-3 12" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
      </button>
      <span class="tp__tip" role="tooltip" id="tp-tip-italic">Italic <kbd>Ctrl</kbd> <kbd>I</kbd></span>
    </span>
    <span class="tp__wrap">
      <button type="button" class="tp__tool" id="tp-link" aria-label="Link" aria-describedby="tp-tip-link">
        <svg viewBox="0 0 20 20" width="20" height="20" aria-hidden="true" focusable="false"><path d="M8.5 11.5a3 3 0 004.2 0l2.3-2.3a3 3 0 00-4.2-4.2l-.8.8M11.5 8.5a3 3 0 00-4.2 0L5 10.8A3 3 0 009.2 15l.8-.8" fill="none" stroke="currentColor" stroke-width="2" stroke-linecap="round"/></svg>
      </button>
      <span class="tp__tip" role="tooltip" id="tp-tip-link">Add a link <kbd>Ctrl</kbd> <kbd>K</kbd></span>
    </span>
    <span class="tp__sep" aria-hidden="true"></span>
    <button type="button" class="tp__open" id="tp-open" popovertarget="tp-pop">Shortcuts</button>
  </div>

  <div class="tp__pop" id="tp-pop" popover>
    <h3 class="tp__pop-title">Keyboard shortcuts</h3>
    <dl class="tp__keys">
      <div><dt>Bold</dt><dd><kbd>Ctrl</kbd> <kbd>B</kbd></dd></div>
      <div><dt>Italic</dt><dd><kbd>Ctrl</kbd> <kbd>I</kbd></dd></div>
      <div><dt>Add a link</dt><dd><kbd>Ctrl</kbd> <kbd>K</kbd></dd></div>
    </dl>
    <button type="button" class="tp__close" id="tp-close" popovertarget="tp-pop" popovertargetaction="hide">Close</button>
  </div>

  <p class="tp__sample">Select some words in a real editor and these buttons would change them. Here they only show the hints.</p>
</section>
</div>

CSS

.tp {
  --tp-surface: #ffffff;
  --tp-fg: #1b2032;
  --tp-muted: #4a5266;
  --tp-line: #d3d8e6;
  --tp-soft: #eceeff;
  --tp-accent: #4338ca;
  --tp-on-accent: #ffffff;
  --tp-tip-bg: #1b2032;
  --tp-tip-fg: #ffffff;
  --tp-kbd: #3a4157;
  --tp-ring: #4338ca;
  box-sizing: border-box;
  width: min(100%, 30rem);
  margin: 0 auto;
  padding: 1.25rem;
  color: var(--tp-fg);
  background: var(--tp-surface);
  border: 1px solid var(--tp-line);
  border-radius: 16px;
  box-shadow: 0 10px 30px -18px rgba(27, 32, 50, 0.4);
}
.tp-page { box-sizing: border-box; min-height: 100vh; padding: 1rem; background: #f2f3fb; }
@media (prefers-color-scheme: dark) { .tp-page { background: #12141f; } }
.tp *, .tp *::before, .tp *::after { box-sizing: border-box; }
.tp__title { margin: 0; font-size: 1.25rem; letter-spacing: -0.01em; }
.tp__lead { margin: 0.3rem 0 1.1rem; color: var(--tp-muted); font-size: 0.9375rem; line-height: 1.5; }
.tp__bar { display: flex; align-items: center; gap: 0.4rem; padding: 0.4rem; background: var(--tp-soft); border: 1px solid var(--tp-line); border-radius: 12px; }
.tp__sep { align-self: stretch; width: 1px; margin: 0.3rem 0.2rem; background: var(--tp-line); }
.tp__wrap { position: relative; display: inline-flex; }
.tp__tool, .tp__open, .tp__close {
  font: inherit;
  cursor: pointer;
}
.tp__tool {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  color: var(--tp-fg);
  background: var(--tp-surface);
  border: 1px solid var(--tp-line);
  border-radius: 10px;
}
.tp__tool:hover { border-color: var(--tp-accent); color: var(--tp-accent); }
.tp__open, .tp__close {
  min-height: 44px;
  margin-left: auto;
  padding: 0.4rem 1rem;
  font-weight: 600;
  color: var(--tp-on-accent);
  background: var(--tp-accent);
  border: 2px solid var(--tp-accent);
  border-radius: 10px;
}
.tp__open:hover, .tp__close:hover { filter: brightness(1.1); }
.tp__tool:focus-visible, .tp__open:focus-visible, .tp__close:focus-visible { outline: 3px solid var(--tp-ring); outline-offset: 2px; }

/* the hint: shown on hover and keyboard focus, stays while the pointer is on it, Escape hides it */
.tp__tip {
  position: absolute;
  bottom: 100%;
  left: 50%;
  z-index: 2;
  padding-bottom: 8px;
  color: var(--tp-tip-fg);
  font-size: 0.8125rem;
  font-weight: 600;
  line-height: 1.3;
  white-space: nowrap;
  visibility: hidden;
  opacity: 0;
  transform: translate(-50%, 4px);
  transition: opacity 0.15s ease, transform 0.15s ease, visibility 0s linear 0.15s;
}
.tp__tip::before { content: ""; position: absolute; inset: 0 0 8px; z-index: -1; background: var(--tp-tip-bg); border-radius: 8px; }
.tp__tip::after { content: ""; position: absolute; bottom: 2px; left: 50%; width: 10px; height: 10px; background: var(--tp-tip-bg); transform: translateX(-50%) rotate(45deg); z-index: -2; }
.tp__tip { padding: 0.4rem 0.65rem 0.65rem; }
.tp__wrap:hover .tp__tip, .tp__wrap:focus-within .tp__tip { visibility: visible; opacity: 1; transform: translate(-50%, 0); transition-delay: 0s; }
.tp.is-dismissed .tp__tip { visibility: hidden; opacity: 0; }
.tp kbd {
  display: inline-block;
  padding: 0 0.35rem;
  font: 600 0.75rem/1.5 ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color: inherit;
  background: var(--tp-kbd);
  border: 1px solid rgba(255, 255, 255, 0.28);
  border-radius: 4px;
}

/* the popover: the browser handles open, Escape and click outside; anchor positioning places it where supported */
.tp__pop {
  width: min(18rem, calc(100vw - 2rem));
  padding: 1rem 1.1rem 1.1rem;
  color: var(--tp-fg);
  background: var(--tp-surface);
  border: 1px solid var(--tp-line);
  border-radius: 14px;
  box-shadow: 0 18px 40px -16px rgba(27, 32, 50, 0.55);
}
.tp__pop::backdrop { background: transparent; }
.tp__pop-title { margin: 0 0 0.6rem; font-size: 1rem; }
.tp__keys { display: grid; gap: 0.4rem; margin: 0 0 1rem; }
.tp__keys div { display: flex; align-items: center; justify-content: space-between; gap: 1rem; }
.tp__keys dt { color: var(--tp-muted); }
.tp__keys dd { display: flex; gap: 0.25rem; margin: 0; }
.tp__pop kbd { padding: 0.1rem 0.45rem; color: var(--tp-fg); background: var(--tp-soft); border: 1px solid var(--tp-line); border-bottom-width: 3px; border-radius: 6px; font: 600 0.8125rem ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; }
.tp__close { margin: 0; width: 100%; }
.tp__sample { margin: 1.1rem 0 0; color: var(--tp-muted); font-size: 0.875rem; line-height: 1.5; }
@supports (position-area: bottom) {
  .tp__open { anchor-name: --tp-open; }
  .tp__pop { position: fixed; position-anchor: --tp-open; position-area: bottom span-left; inset: auto; margin: 0.5rem 0 0; position-try-fallbacks: flip-block; }
}
@media (max-width: 24em) {
  .tp { padding: 1rem; }
  .tp__bar { flex-wrap: wrap; }
  .tp__open { margin-left: 0; }
}
@media (prefers-color-scheme: dark) {
  .tp {
    --tp-surface: #1a1e2e;
    --tp-fg: #edf0fa;
    --tp-muted: #b9bfd3;
    --tp-line: #3a4157;
    --tp-soft: #262b46;
    --tp-accent: #aeb1ff;
    --tp-on-accent: #14162b;
    --tp-tip-bg: #edf0fa;
    --tp-tip-fg: #14162b;
    --tp-kbd: #cfd3ea;
    --tp-ring: #c9cbff;
    box-shadow: none;
  }
  .tp kbd { border-color: rgba(20, 22, 43, 0.25); }
  .tp__pop { box-shadow: 0 18px 40px -16px rgba(0, 0, 0, 0.7); }
}
@media (prefers-reduced-motion: reduce) {
  .tp__tip { transition: none; }
}

JavaScript

// Hints and the popover work in CSS and HTML alone. This script only adds one rule from WCAG 1.4.13:
// a hint can be dismissed with Escape without moving the pointer or the focus.
(function () {
  'use strict';
  var root = document.getElementById('tp-root');
  if (!root) return;

  document.addEventListener('keydown', function (event) {
    if (event.key === 'Escape' && root.querySelector('.tp__wrap:hover, .tp__wrap:focus-within')) {
      root.classList.add('is-dismissed');
    }
  });

  function reset() { root.classList.remove('is-dismissed'); }
  root.querySelectorAll('.tp__wrap').forEach(function (wrap) {
    wrap.addEventListener('mouseleave', reset);
    wrap.addEventListener('focusout', reset);
  });
})();

Accessibility notes

Written by the author of the snippet. Each note has a test step or a markup check that reads the page.

  • Each icon button has a name and is tied to its hint with aria-describedby.
  • A hint appears on keyboard focus as well as on hover.
  • Escape hides a hint without moving focus, as WCAG 1.4.13 asks.
  • The popover is native: Escape, a click outside and the Close button all close it.