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

You're using npm packages, so we've auto-selected Babel for you here, which we require to process imports and make it all work. If you need to use a different JavaScript preprocessor, remove the packages in the npm tab.

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

Use npm Packages

We can make npm packages available for you to use in your JavaScript. We use webpack to prepare them and make them available to import. We'll also process your JavaScript with Babel.

⚠️ This feature can only be used by logged in users.

Code Indentation

     

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.

HTML Settings

Here you can Sed posuere consectetur est at lobortis. Donec ullamcorper nulla non metus auctor fringilla. Maecenas sed diam eget risus varius blandit sit amet non magna. Donec id elit non mi porta gravida at eget metus. Praesent commodo cursus magna, vel scelerisque nisl consectetur et.

            
              <div class="section">
  <h1>Tooltip Accessibility Demo</h1>

  <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="https://www.w3.org/WAI/WCAG21/Understanding/content-on-hover-or-focus.html">WCAG 2.1 Content on Hover or Focus</a>.</p>
</div>

<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>
</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>
    </svg>
  </button>
</div>

<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>
</div>
            
          
!
            
              /* 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;
}

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;
}

input:focus,
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', () => {
    showTooltip(tooltip);
  });
  trigger.addEventListener('focus', () => {
    showTooltip(tooltip);
  });
  
  // hide tooltip on mouse out and blur
  // use timeout on mouse leave
  tooltipContainer.addEventListener('mouseleave', () => {
    timeoutTooltip(tooltip);
  });
  trigger.addEventListener('blur', () => {
    hideTooltip(tooltip);
  });
  
  // hide the tooltip on escape key press
  trigger.addEventListener('keydown', (event) => {
    if (event.key === 'Escape') {
      hideTooltip(tooltip);
      // 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.classList.add('tooltip-close');
  tooltip.appendChild(closeBtn);
  closeBtn.addEventListener('click', () => {
    hideTooltip(tooltip);
    // save dismissal
    dismissals.set(tooltip, true);
  });

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

function showTooltip(tooltip: HTMLElement) {
  // do not show if tooltip has been dismissed
  if (dismissals.has(tooltip)) {
    return false;
  }

  tooltip.style.display = 'block';
  tooltip.removeAttribute('aria-hidden');
  
  // if a hide timeout exists for this tooltip, clear it
  if (timeouts.has(tooltip)) {
    window.clearTimeout(timeouts.get(tooltip));
  }
}

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

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

// initiate tooltips
const tooltips = document.querySelectorAll('.tooltip-wrapper');
tooltips.forEach((tooltip) => {
  initTooltip(tooltip);
});
            
          
!
999px
🕑 One or more of the npm packages you are using needs to be built. You're the first person to ever need it! We're building it right now and your preview will start updating again when it's ready.

Console