Tooltip

A tooltip is a small, contextual overlay that provides additional information when users interact with an element in a user interface.

UsageCodeAccessibility

Button with a tooltipThis is a tooltip with some additional information

The tooltip should be a sibling of the element it is describing (not a child element). The for attribute links it to that element by id.

<sl-button id="button">Button with a tooltip</sl-button>
<sl-tooltip for="button" type="description">This is a tooltip with some additional information</sl-tooltip>

When to use

Explanations for unfamiliar terms or jargon

Tooltips can offer brief definitions or explanations for technical terms, acronyms, or industry-specific terminology.

Long titles or descriptions

If you’re displaying a list of articles with truncated titles, a tooltip can reveal the complete title when users hover over the ellipsis.

When not to use

Essential and critical information

Avoid using tooltips for critical instructions or essential information. Users may miss tooltips, especially if they quickly interact with elements.

Mobile devices and touchscreens

Tooltips rely on hover interactions, which do not translate well to touchscreens.

Accessibility concerns

Some users rely on screen readers or have visual impairments. Ensure that tooltips are accessible and provide an alternative method (such as keyboard shortcuts) to access the same information.

Anatomy

ItemNameDescriptionOptional
1PanelContains the panel contentno
2ContentAn area to display any text contentno

Figma Options

With these options you can tweak the appearance of the tooltip in Figma. They are available in the Design Panel so you can compose the tooltip to exactly fit the user experience need for the uses case you are working on.

ItemOptionsDescription
position1-o'clock 2-o'clock 3-o'clock 4-o'clock 5-o'clock 6-o'clock 7-o'clock 8-o'clock 9-o'clock 10-o'clock 11-o'clock 12-o'clockTo indicate the direction of context that the tooltip is attributed to
tooltipvalueTo insert the text of the tooltip

The tooltip should be a sibling of the elements it belongs to (not a child element). Point the for attribute at the id of the anchor element. To share one tooltip between multiple elements, pass several ids separated by spaces.

WesharethesametooltipI am shared between different elements
<sl-button id="shared-we" fill="solid" variant="primary">We</sl-button>
<sl-button id="shared-share" fill="solid" variant="primary">share</sl-button>
<sl-button id="shared-the" fill="solid" variant="primary">the</sl-button>
<sl-button id="shared-same" fill="solid" variant="primary">same</sl-button>
<sl-button id="shared-tooltip" fill="solid" variant="primary">tooltip</sl-button>
<sl-tooltip for="shared-we shared-share shared-the shared-same shared-tooltip" type="description">I am shared between different elements</sl-tooltip>

Every element listed in for gets the tooltip's triggers, and the tooltip is positioned against whichever one the user interacted with. The tooltip looks up the ids in its own root node, so the tooltip and its anchors need to live in the same document or shadow root.

Label or description

Use type to control how the tooltip is linked for screen readers. The default, label, exposes the tooltip as the accessible name of the anchor and is what you want for icon-only buttons. Use type="description" when the anchor already has its own label and the tooltip only adds extra information.

<sl-button id="edit" fill="outline"><sl-icon name="far-pen"></sl-icon></sl-button>
<sl-tooltip for="edit">Edit</sl-tooltip>

<sl-button id="publish" fill="solid" variant="primary">Publish</sl-button>
<sl-tooltip for="publish" type="description">Makes the page visible to everyone</sl-tooltip>

You don't have to add aria-labelledby or aria-describedby yourself; the tooltip sets the relation on every anchor it belongs to, and removes it again when it is disabled or removed. See the accessibility page for more about this.

Triggers

By default the tooltip shows when the user hovers over the anchor or focuses it with the keyboard. Use trigger to change that; it takes a space separated list of hover, focus and click. Use manual when the tooltip should only be shown programmatically.

Click meClick the button again to dismiss me

<sl-button id="click-trigger">Click me</sl-button>
<sl-tooltip for="click-trigger" trigger="click" type="description">Click the button again to dismiss me</sl-tooltip>

A few details worth knowing:

  • Focusing the anchor with the mouse does not show the tooltip; only keyboard focus (:focus-visible) does. That way the tooltip stays out of the way when a dialog returns focus to the button that opened it, for example.
  • Pressing Escape hides the tooltip while it is open.
  • Hovering is delayed to prevent tooltips from flashing by when the pointer moves across the screen. The delays are static properties, so changing them applies to every tooltip in the application:
import { Tooltip } from '@sl-design-system/tooltip';

Tooltip.hoverShowDelay = 150; // default, in milliseconds
Tooltip.hoverHideDelay = 0; // default, in milliseconds

Showing and hiding programmatically

Set the open property to show or hide the tooltip regardless of its triggers. The tooltip is a popover, so you can also call showPopover() and hidePopover() on it directly.

To check whether a tooltip is showing, don't read the open property; use matches(':popover-open') instead. That also covers tooltips that were opened by one of the triggers.

const tooltip = document.querySelector('sl-tooltip');

tooltip.open = true;

if (tooltip.matches(':popover-open')) {
  // The tooltip is showing
}

Use disabled to stop a tooltip from showing altogether. A disabled tooltip hides itself if it is open, ignores its triggers and drops the ARIA relation with its anchors, so screen readers no longer announce it.

Positioning and size

The tooltip positions itself with CSS anchor positioning. It renders above the anchor by default, and flips to the other side when there is not enough room. Since this is plain CSS, you change the position by styling the tooltip:

sl-tooltip {
  /* Show the tooltip to the right of the anchor instead of above it */
  position-area: right;

  /* Prevent long tooltips from becoming too wide */
  max-inline-size: 200px;
}

Not every browser supports CSS anchor positioning yet. In those browsers you need the CSS Anchor Positioning polyfill, see add polyfills in the getting started guide.

The tooltip renders an invisible element between the anchor and the tooltip, so the pointer can travel from one to the other without the tooltip disappearing. It is available as the hover-bridge CSS part, which is handy when you want to see what it covers:

sl-tooltip::part(hover-bridge) {
  background: hotpink;
}

Migrating from older versions

The tooltip was rewritten to use the browser's popover and CSS anchor positioning APIs. If you are coming from an older version:

BeforeNow
Tooltip.lazy(element, callback, options)Render an <sl-tooltip> with a for attribute; there is nothing to create lazily anymore
The tooltip() directive from @sl-design-system/tooltipRender an <sl-tooltip> with a for attribute
aria-describedby="my-tooltip" on the anchorfor="my-anchor" on the tooltip, combined with type
position="bottom"The position-area CSS property
maxWidth="200"The max-inline-size CSS property
ariaRelation="label"type="label", which is the default

API

Tooltip component has a range of properties to define the experience in different use cases. The tooltip should be a sibling of the element it belongs to (not a child element) and is linked to that element with the for attribute.

Properties

NameAttributeTypeDefaultDescription
disableddisabledboolean | undefinedfalseStops the tooltip from being displayed.
forforstring | undefinedThe id of the element this tooltip is for. Multiple ids can be passed by separating them with a space; the tooltip then belongs to each of those elements.
static hoverHideDelay-number0The delay in milliseconds before hiding the tooltip when the mouse leaves the anchor element.
static hoverShowDelay-number150The delay in milliseconds before showing the tooltip when the mouse hovers over the anchor element.
openopenboolean | undefinedfalseSetting this will cause the tooltip to show/hide, regardless of trigger. Do not use this property to check if the tooltip is showing, use matches(':popover-open') instead.
triggertriggerstring'focus hover'Controls how the tooltip is activated. Possible options include click, hover, focus, and manual. Multiple options can be passed by separating them with a space. When manual is used, the tooltip must be activated programmatically.
typetype'description' | 'label' | undefined'label'The type of tooltip. Used to determine the ARIA relation that should be used.

Slots

NameDescription
(default)The content of the tooltip.

CSS Parts

NameDescription
hover-bridgeAn invisible element used to extend the hover area of the tooltip.

Accessibility considerations

Discoverability and readability

Ensure that tooltips are discoverable and readable using various input methods, including: Mouse or other pointer devices, keyboard navigation, screen readers, zoom software or any other assistive technology. Users with different abilities should be able to access tooltip content seamlessly.

For keyboard users to be able to see the tooltip it is important to use the tooltip only on elements that can get focus; for example a button or link. If the part of the interface you want to describe can not have the focus you can add a button with an information icon that will trigger the tooltip

Informative but non-essential content

Tooltips should provide information that enhances the user’s understanding of the UI but is not strictly necessary for operating it. Avoid critical information in tooltips, as users relying solely on assistive technologies may miss it.

Non-blocking behavior

When a tooltip is open, it should not obstruct the user from performing other tasks on the screen. Test this behavior across all responsive breakpoints to ensure consistent behavior regardless of screen size.

Keyboard interactions

Here's an overview of the common keyboard interactions associated with a tooltip:

CommandDescription
tabShows the tooltip when the element that triggers it receives focus, and hides it again when focus moves on. Focusing that element with the mouse does not show the tooltip, and neither does moving focus to it from code (when closing a dialog for example).
escapeHides the tooltip while it is showing.

WAI-ARIA

In the component itself we use multiple aria-attributes to assure the component works well with a range of assistive technologies. For some attributes however it is not possible for the Design System to add a meaningful value, because it relies on the context or way a component is used.

Attributes that we recommend you add in certain scenarios are mentioned below.

A tooltip is linked to the element it belongs to by either an aria-labelledby or an aria-describedby relation. The choice between the two depends on the context and the relationship between the tooltip and the anchor element. A good example of when to use aria-labelledby is when the tooltip provides a label or title for the anchor element, such as an icon only button (so button with only an icon) and no visible text. In this case, the tooltip serves as the accessible name for the button.

You can read more on the difference between the two attributes in the MDN article about aria-describedby

You do not set these attributes yourself. Point the tooltip at its anchor with the for attribute and pick the relation with type; the tooltip then takes care of the rest, for every element listed in for:

AttributeValueDescription
forstringThe id of the anchor element, so the element the tooltip belongs to. Pass several ids separated by spaces to link one tooltip to multiple elements.
typelabel descriptionWhich relation the tooltip sets up with its anchors: label (the default) makes the tooltip the accessible name of the anchor, description makes it the accessible description.

The tooltip sets up this relation using element references (ariaLabelledByElements and ariaDescribedByElements) rather than by writing an aria-labelledby or aria-describedby attribute on the anchor. Assistive technology sees the same thing, but you won't find the attribute on the anchor element when you inspect it in the browser.

Because the relation only exists while the tooltip is enabled, a disabled tooltip is not announced at all. Make sure an anchor that relies on the tooltip for its accessible name has another name when you disable the tooltip.