Pen Settings



CSS Base

Vendor Prefixing

Add External Stylesheets/Pens

Any URL's added here will be added as <link>s in order, and before the CSS in the editor. If you link to another Pen, it will include the CSS from that Pen. If the preprocessor matches, it will attempt to combine them before processing.

+ add another resource


Babel includes JSX processing.

Add External Scripts/Pens

Any URL's added here will be added as <script>s in order, and run before the JavaScript in the editor. You can use the URL of any other Pen and it will include the JavaScript from that Pen.

+ add another resource


Add Packages

Search for and use JavaScript packages from npm here. By selecting a package, an import statement will be added to the top of the JavaScript editor for this package.


Save Automatically?

If active, Pens will autosave every 30 seconds after being saved once.

Auto-Updating Preview

If enabled, the preview panel updates automatically as you code. If disabled, use the "Run" button to update.

Format on Save

If enabled, your code will be formatted when you actively save your Pen. Note: your code becomes un-folded during formatting.

Editor Settings

Code Indentation

Want to change your Syntax Highlighting theme, Fonts and more?

Visit your global Editor Settings.


                <div class="section">
  <h1>Tooltip Accessibility Demo</h1>
    <h2>IMPORTANT NOTE:</h2>
    <p>I made this as an informal demo a while ago, and since then further user testing has shown that <kbd>Escape</kbd> is not a good key to dismiss tooltips, for <a href="">a variety of reasons</a>. I'd recommend using the <kbd>Control</kbd> key to dismiss instead.

  <p>A few examples of simple tooltips serving as the name or description of buttons and form fields. They work with mouse and keyboard, and are dismissable with <code>Escape</code> or by clicking the "x". This allows the tooltips to meet <a href="">WCAG 2.1 Content on Hover or Focus</a>.</p>

<div class="section">
  <h2>A text input with a tooltip helper</h2>
  <label for="test1">A Name</label>
  <div class="tooltip-wrapper">
    <input id="test1" type="text" aria-describedby="tip1" class="tooltip-trigger">
    <div class="tooltip" id="tip1">Any name you want</div>

<div class="section">
  <h2>An icon button with name in tooltip</h2>
  <p>This tooltip is a child of the button element, but could also live outside the button in conjunction with <code>aria-labelledby</code>.</p>
  <button type="button" aria-labelledby="tip2" class="tooltip-wrapper tooltip-trigger">
    <span class="tooltip" id="tip2">Delete</span>
    <svg class="icon" viewBox="0 0 32 32" aria-hidden="true">
      <path d="M4 10v20c0 1.1 0.9 2 2 2h18c1.1 0 2-0.9 2-2v-20h-22zM10 28h-2v-14h2v14zM14 28h-2v-14h2v14zM18 28h-2v-14h2v14zM22 28h-2v-14h2v14z"></path>
      <path d="M26.5 4h-6.5v-2.5c0-0.825-0.675-1.5-1.5-1.5h-7c-0.825 0-1.5 0.675-1.5 1.5v2.5h-6.5c-0.825 0-1.5 0.675-1.5 1.5v2.5h26v-2.5c0-0.825-0.675-1.5-1.5-1.5zM18 4h-6v-1.975h6v1.975z"></path>

<div class="section">
  <h2>Make it better</h2>
  <p>This pen does not do viewport collision detection, which would be a good idea to add for production-ready tooltips.</p>
  <p>It also does not handle preventing multiple overlapping tooltips (e.g. with combined focus & hover) which would be a good idea, particularly when open tooltips may overlap, e.g. within a menubar or toolbar.</p>


                /* Tooltip styles */
.tooltip-wrapper {
  position: relative;
  display: inline-block;

.tooltip {
  position: absolute;
  left: 50%;
  bottom: 0;
  transform: translate(-50%, calc(100% + 10px));
  padding: 6px calc(10px + 1rem) 6px 10px;
  border-radius: 4px;
  background: rgba(0, 0, 0, 0.9);
  color: #fff;

.tooltip::before {
  content: "";
  position: absolute;
  top: -10px;
  left: calc(50% - 10px);
  border-bottom: 10px solid rgba(0, 0, 0, 0.9);
  border-left: 10px solid transparent;
  border-right: 10px solid transparent;

/* a designer could certainly style this better :) */
.tooltip-close {
  position: absolute;
  top: 0.25rem;
  right: 0.25rem;
  width: 1rem;
  height: 1rem;
  padding: 0;
  border: 0;
  border-radius: 50%;
  box-shadow: 0 0 1px 1px rgba(200, 200, 200, 0.5);
  background-color: transparent;
  color: #fff;
  font-size: 0.75rem;
  line-height: 1rem;

/* Page/content styles: */
body {
  background-color: #d4e4f6;
  color: #0d2744;
  flex-direction: column;
  font-size: 18px;
  padding: 20px;

h2 {
  font-size: 1.4em;
  font-weight: bold;

p {
  line-height: 1.4;
  margin-bottom: 1em;

blockquote {
  border-left: 3px solid black;
  margin-left: 0;
  padding-left: 1.5em;

label {
  display: block;
  margin-bottom: 0.25em;

button {
  padding: 0.5em 0.75em;
  border: 1px solid #666;
  border-radius: 3px;
  background-color: #ddd;

input {
  padding: 0.5em 0.75em;
  border: 1px solid #666;
  border-radius: 3px;

button:focus {
  border-color: rgb(1, 104, 215);
  box-shadow: 0 0 0 6px rgba(1, 104, 215, 0.5);
  outline: none;

.section {
  margin: 0 auto 2.5em;
  max-width: 600px;

.icon {
  display: inline-block;
  width: 1em;
  height: 1em;
  stroke-width: 0;
  stroke: currentColor;
  fill: currentColor;


                // Settings:
// Timeout to hide tooltip
const TIMEOUT_LENGTH = 500;

// global timeout map; quick n dirty
const timeouts = new WeakMap();

// if the user actively dismisses a tooltip, save that setting
// and do not show the tooltip again
const dismissals = new WeakMap();

// here we attach all event listeners to control the tooltip
function initTooltip(tooltipContainer: HTMLElement) {
  const trigger = tooltipContainer.classList.contains('tooltip-trigger') ? tooltipContainer : tooltipContainer.querySelector('.tooltip-trigger');
  const tooltip = tooltipContainer.querySelector('.tooltip');

  // show tooltip on hover and focus
  tooltipContainer.addEventListener('mouseenter', () => {
  trigger.addEventListener('focus', () => {
  // hide tooltip on mouse out and blur
  // use timeout on mouse leave
  tooltipContainer.addEventListener('mouseleave', () => {
  trigger.addEventListener('blur', () => {
  // hide the tooltip on escape key press
  trigger.addEventListener('keydown', (event) => {
    if (event.key === 'Escape') {
      // save dismissal
      dismissals.set(tooltip, true);
  // create a close button for pointer dismissal
  const closeBtn = document.createElement('button');
  closeBtn.innerHTML = 'X';
  closeBtn.tabIndex = -1;
  closeBtn.setAttribute('aria-hidden', 'true');
  closeBtn.addEventListener('click', () => {
    // save dismissal
    dismissals.set(tooltip, true);

  // hide the tooltip in here so they show up without JS on
  // debateably useful = 'none';

function showTooltip(tooltip: HTMLElement) {
  // do not show if tooltip has been dismissed
  if (dismissals.has(tooltip)) {
    return false;
  } = 'block';
  // if a hide timeout exists for this tooltip, clear it
  if (timeouts.has(tooltip)) {

function hideTooltip(tooltip: HTMLElement) { = 'none';
  tooltip.setAttribute('aria-hidden', 'true');

function timeoutTooltip(tooltip: HTMLElement) {
  // hide the tooltip after a set amount of time
  const timeoutId = window.setTimeout(() => {
  // store the timeout so it can be cleared
  timeouts.set(tooltip, timeoutId);

// initiate tooltips
const tooltips = document.querySelectorAll('.tooltip-wrapper');
tooltips.forEach((tooltip) => {