Pen Settings

HTML

CSS

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

JavaScript

Babel is required to process package imports. If you need a different preprocessor remove all packages first.

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

Behavior

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.

Editor Settings

Code Indentation

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

Visit your global Editor Settings.

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.

HTML

            
              <main>
    <h1>Tooltips</h1>
    <p>Two different implementations of the tooltip design pattern.</p>
    <section>
        <h2>Todo:</h2>
        <ul>
            <li><input type="checkbox" id="td1" checked><label for="td1"> Comply with <a href="https://www.w3.org/TR/WCAG21/#content-on-hover-or-focus">WCAG 2.1 SC 1.4.13 Content on Hover or Focus</a></label></li>
            <li><input type="checkbox" id="td2"><label for="id2"> Do some bounding box calculations</label></li>
        </ul>
    </section>
    <section>
        <h2>Tooltip as main label</h2>
        <p>In this version the label for the button is privded via a <code>aria-labelledby</code> attribute that points to tooltip text.</p>
        <div class="tooltip-container">
            <button type="button" aria-labelledby="tooltip">
                <span aria-hidden>💛</span>
            </button>
            <p id="tooltip" role="tooltip" hidden>Like</p>
        </div>
        <h3>Source</h3>
<pre><code>&lt;button type="button" aria-labelledby="tooltip"&gt;
  &lt;span aria-hidden&gt;💛&lt;/span&gt;
&lt;/button&gt;
&lt;p id="tooltip" role="tooltip" hidden&gt;Like&lt;/p&gt;
</code></pre>
    </section>
    <section>
        <h2>Tooltip with additional information</h2>
        <p>This implementation follows the <a href="https://w3c.github.io/aria-practices/#tooltip">guidance of the ARIA Authoring Practices Guide</a>.</p>
        <div class="tooltip-container">
            <button type="button" aria-describedby="description">
                <span role="img" aria-label="Settings">⚙️</span>
            </button>
            <p id="description" role="tooltip" hidden>View and manage settings</p>
        </div>
        <h3>Source</h3>
<pre><code>&lt;button type="button" aria-describedby="description"&gt;
  &lt;span role="img" aria-label="Settings"&gt;⚙️&lt;/span&gt;
&lt;/button&gt;
&lt;p id="description" role="tooltip" hidden&gt;View and manage settings&lt;/p&gt;
</code></pre>
    </section>
    <section>
        <h2>Keyboard support</h2>
        <p>Pressing <kbd>Escape</kbd> will close the tooltip.</p>
    </section>
    <section>
        <h2>Test results</h2>
        <p>The ARIA bits of this work in Safari with VoiceOver on iOS. But the tooltips themselves aren’t visible before you trigger the action. This is a limitation of the design pattern.</p>
        <p>Tested to work in these browsers:</p>
        <ul>
            <li>Chrome 71.0.3578.98 on macOS 10.14.3 / Windows 10</li>
            <li>Safari 12.0.3 on macOS 10.14.3</li>
            <li>Firefox 65 on macOS 10.14.3 / Windows 10</li>
            <li>Edge 41.16299.820.0 / EdgeHTML 16.16299 on Windows 10</li>
        </ul>
        <p>Tested to work with these browser/assistive technology combinations:</p>
        <ul>
            <li>Safari 12.0.3 and VoiceOver on macOS 10.14.3</li>
            <li>Chrome 71.0.3578.98 and VoiceOver on macOS 10.14.3</li>
            <li>Chrome 71.0.3578.98 and NVDA 2018.4 on Windows 10 <a href="#fn3">3</a></li>
            <li>Chrome 71.0.3578.98 and JAWS 2019 on Windows 10  <a href="#fn3">3</a></li>
            <li>Edge 41.16299.820.0 / EdgeHTML 16.16299 and Narrator on Windows 10  <a href="#fn1">1</a></li>
            <li>Edge 41.16299.820.0 / EdgeHTML 16.16299 and NVDA 2018.4 on Windows 10  <a href="#fn1">1</a></li>
            <li>Edge 41.16299.820.0 / EdgeHTML 16.16299 and JAWS 2019 on Windows 10  <a href="#fn1">1</a>  <a href="#fn4">4</a></li>
            <li>Firefox 65 and NVDA 2018.4 on Windows 10  <a href="#fn2">2</a></li>
            <li>Firefox 65 and JAWS 2019 on Windows 10  <a href="#fn3">3</a></li>
            
        </ul>
        <ol class="footnotes">
            <li id="fn1">The settings button is announced as “gear, button”. This is most likely because Edge ignores the <code>aria-label</code> on the <code>span</code> with <code>role=img</code>. Perhaps Edge doesn’t support the <code>img</code>-role.</li>
            <li id="fn2">NVDA announces the settings button as “Settings, graphic, button, view and manage settings”. There’s no delay between the announcement of the button name and its description.</li>
            <li id="fn3">There’s no delay between the announcement of the button name and its description.</li>
            <li id="fn4">The description isn’t announced.</li>
        </ol>
    </section>
    <section>
        <h2>Resources</h2>
        <ul>
            <li><a href="https://inclusive-components.design/tooltips-toggletips/">Tooltips and Toggletips</a> by Heydon Works</li>
            <li><a href="https://w3c.github.io/aria-practices/#tooltip">ARIA APG: Tooltip design pattern</a></li>
            <li><a href="https://github.com/w3c/aria-practices/issues/128">GitHub APG: Draft tooltip design pattern</a></li>
            <li><a href="https://github.com/w3c/aria-practices/issues/127">GitHub APG: Draft tooltip code example</a></li>
        </ul>
    </section>
    <footer>
        <p>Words by Zoë</p>
    </footer>
</main>
            
          
!

CSS

            
              :root {
    --tooltip-thingy-height: .5em;
}

.tooltip-container {
    position: relative;
    display: inline-block;
}

[role=tooltip] {
    position: absolute;
    top: 100%;
    left: 50%;
    transform: translateX(-50%);
    padding: .5em 1em;
    border-radius: .25em;
    color: white;
    background: black;
    min-width: max-content;
    max-width: 10em;
    box-shadow: 0 1px 2px hsl(0, 0%, 55%);
    
    &::before {
        position: absolute;
        bottom: 100%;
        left: 50%;
        transform: translateX(-50%);
        border: var(--tooltip-thingy-height) solid transparent;
        border-bottom-color: black;
        content: '';
    }
    
    &::after {
        position: absolute;
        right: 0;
        bottom: 100%;
        left: 0;
        display: block;
        height: calc(var(--tooltip-thingy-height) * 2);
        content: '';
    }
}

button {
  display: inline-block;
  position: relative;
  padding: .4em .7em;
  border: 1px solid hsl(213, 71%, 49%);
  border-radius: 5px;
  box-shadow: 0 1px 2px hsl(216, 27%, 55%);
  color: #fff;
    font-size: inherit;
  text-shadow: 0 -1px 1px hsl(216, 27%, 25%);
  background-color: hsl(216, 82%, 51%);
  background-image: linear-gradient(to bottom, hsl(216, 82%, 53%), hsl(216, 82%, 47%));
}

button:hover {
  border-color: hsl(213, 71%, 29%);
  background-color: hsl(216, 82%, 31%);
  background-image: linear-gradient(to bottom, hsl(216, 82%, 33%), hsl(216, 82%, 27%));
  cursor: default;
}

button:focus {
  outline: none;
}

button:focus::before {
  position: absolute;
  z-index: -1;
  /* button border width - outline width - offset */
  top: calc(-1px - 3px - 3px);
  right: calc(-1px - 3px - 3px);
  bottom: calc(-1px - 3px - 3px);
  left: calc(-1px - 3px - 3px);
  border: 3px solid hsl(213, 71%, 49%);
  /* button border radius + outline width + offset */
  border-radius: calc(5px + 3px + 3px);
  content: '';
}

button:active {
  border-color: hsl(213, 71%, 49%);
  background-color: hsl(216, 82%, 31%);
  background-image: linear-gradient(to bottom, hsl(216, 82%, 53%), hsl(216, 82%, 47%));
  box-shadow: inset 0 3px 5px 1px hsl(216, 82%, 30%);
}

            
          
!

JS

            
              class Tooltip {
    constructor(element) {
        this.element = element
        this.button = element.querySelector('button')
        this.tooltip = element.querySelector('[role=tooltip]')
        this.globalEscapeBound = this.globalEscape.bind(this)
        this.bindEvents()
    }
    
    bindEvents() {
        this.element.addEventListener('mouseenter', this.open.bind(this))
        this.button.addEventListener('focus', this.open.bind(this))
        this.element.addEventListener('mouseleave', this.close.bind(this))
        this.button.addEventListener('blur', this.close.bind(this))
    }
    
    open() {
        this.showTooltip()
        this.attachGlobalListener()
    }
    
    close() {
        this.hideTooltip()
        this.removeGlobalListener()
    }
    
    attachGlobalListener() {
        document.addEventListener('keydown', this.globalEscapeBound)
    }
    
    removeGlobalListener() {
        document.removeEventListener('keydown', this.globalEscapeBound)
    }
    
    globalEscape(event) {
        if (event.key === 'Escape' || event.key === 'Esc') {
            this.close()
        }
    }
    
    showTooltip() {
        this.tooltip.removeAttribute('hidden')
    }
    
    hideTooltip() {
        this.tooltip.setAttribute('hidden', 'hidden')
    }
}

Array.from(document.querySelectorAll('.tooltip-container')).forEach(element => new Tooltip(element));
            
          
!
999px

Console