Tooltip
A message that appears when a cursor is positioned over an element.
- Each tooltip has a unique
id, which is therelof the link that opens it. - Open one with a
data-tooltiplink, or with JavaScript. smart: truekeeps the tooltip inside the window: it tries the other alignments, and then the default width.delay: truekeeps the tooltip open after the mouse leaves, fordelayTimems.- A link with a
titleand norelgets a tooltip made from the title. - Only
mouseoverandmouseoutopen and close a tooltip. Nested tooltips are not supported.
- Default settings – Default settings (width and align).
- Set align – Default width and a set align (bottom).
- Set width – 300px width and the default align.
- Settings applied – 400px width and a set align (top).
- Smart Smart – the link is at the right edge, so the tooltip would leave the window: it moves to a side that fits.
- Delay – stays open for 1.5 seconds after the mouse leaves.
-
Dynamic tooltip
– made from the
title, with no markup.
Hello! I am a tooltip
I'm some text about the tooltip.
<ul class="tooltip-list"> <li> <a href="#" rel="demo-tooltip" data-tooltip="">Default settings</a> <span> – Default settings (width and align).</span> </li> <li> <a href="#" rel="demo-tooltip" data-tooltip="align:bottom">Set align</a> <span> – Default width and a set align (bottom).</span> </li> <li> <a href="#" rel="demo-tooltip" data-tooltip="width:300">Set width</a> <span> – 300px width and the default align.</span> </li> <li> <a href="#" rel="demo-tooltip" data-tooltip="align:top:width:400">Settings applied</a> <span> – 400px width and a set align (top).</span> </li> <li class="tooltip-smart"> <a href="#" rel="demo-tooltip" data-tooltip="smart:true">Smart</a> <span>Smart – the link is at the right edge, so the tooltip would leave the window: it moves to a side that fits.</span> </li> <li> <a href="#" rel="demo-tooltip" data-tooltip="delay:true:delayTime:1500">Delay</a> <span> – stays open for 1.5 seconds after the mouse leaves.</span> </li> <li> <a href="#" data-tooltip="width:180" title="Hello, I was made from the title.">Dynamic tooltip</a> <span> – made from the <code>title</code>, with no markup.</span> </li></ul>
<div class="tooltip" id="demo-tooltip"> <div class="tooltip-content"> <h4>Hello! I am a tooltip</h4> <p>I'm some text about the tooltip.</p> </div></div>// The markup of a tooltip is placed with coordinates on the page, so it belongs directly in <body>. Inside this// site's content it would be drawn under the header and the side navigation. In your own page, put it there.document.body.appendChild(document.getElementById('demo-tooltip'));
// Settings given here are the defaults for every tooltip. Each link can override them with// data-tooltip, as the links above do.var tooltips = new ML.Tooltip();
tooltips.init();Settings
Section titled “Settings”data-tooltip="width:300:align:top" sets the options of one link, as name:value pairs separated by colons.
new ML.Tooltip(settings) sets the defaults for every link, and a link’s data-tooltip overrides them.
| Setting | Default | What it is |
|---|---|---|
width |
150 |
the width of the tooltip, in px |
align |
right |
right, left, top or bottom of the link |
smart |
false |
move the tooltip, or shrink it to the default width, if it leaves the window |
delay |
false |
keep the tooltip open after the mouse leaves |
delayTime |
3000 |
how long to keep it open, in ms, when delay is true |
A value that is not valid is replaced by the default.
var tooltips = new ML.Tooltip({ delay: true });tooltips.init();
// With JavaScript instead of, or as well as, data-tooltip. It takes the link, not the id.tooltips.show(document.getElementById('open'), { width: 50, align: 'top' });tooltips.hide();Events
Section titled “Events”Fired on document. event.detail is { tooltip, options }: the tooltip element and the options it was shown with.
| Event | Fired when |
|---|---|
tooltip.opened |
a tooltip is opened |
tooltip.closed |
a tooltip is closed |
document.addEventListener('tooltip.opened', function (event) { console.log('opened', event.detail.tooltip, event.detail.options);});show()takes the link that opens the tooltip, because that is what the tooltip is placed next to. The link’srelis the id of the tooltip.- The tooltip is placed with coordinates on the page, so its markup should be directly in
<body>, or inside elements that are not positioned. A tooltip made from a title is added to<body>for you. - The text of a title is shown as text, not as HTML.
- The
relanddata-tooltipare read from the link, so hovering an icon or other element inside the link opens its tooltip too. Moving the mouse between the parts of one link does not close the tooltip and open it again. - Only the mouse opens a tooltip. It is not accessible by keyboard.
- With
smart, a tooltip that fits nowhere is left where you asked for it. smartonly looks at the window. It does not know about a fixed header or a side navigation: a tooltip in<body>is drawn above them, and can cover them.
See the generated Tooltip class reference for every method and setting.
Enhancements
Section titled “Enhancements”A checklist. An unticked item is not built, and not promised: it says what is missing today. A ticked item has been built.
- Triggers other than
<a>. The trigger is typed and read as a link (rel), so a<button>or a<span>withdata-tooltipdoes not work. A better design points to the tooltip witharia-describedby="id", which works on any element and also tells screen readers about it. - Keyboard and screen readers. Only the mouse opens a tooltip. Opening on focus,
role="tooltip", and Esc to dismiss would make it usable without a mouse. - Touch. A tap to open and close a tooltip.
- Keeping clear of fixed elements.
smartonly looks at the window, so a tooltip can end up under a fixed header or a side navigation. An option naming what to avoid would let it try another side. - Following the window. A tooltip that is open (
delaykeeps it open) is not moved when the window is resized or the page is scrolled. - Hovering inside the link. Hovering an icon inside the link failed, because the
relwas read from the element under the mouse. It is read from the link now. - Tall links. A tooltip on the left or right is centered on a link about 16px high. Taller links are off.
- Content you can point at. Letting the mouse move onto the tooltip without closing it.