Tooltips
Enhance user experience with Chassis CSS's accessible tooltip system, providing contextual information through CSS3 animations and intelligent positioning.
Introduction
Tooltips in Chassis CSS deliver contextual information and helpful hints without disrupting user workflows. Built on modern web standards with intelligent positioning via Popper.js, tooltips seamlessly integrate with your design system while maintaining accessibility and performance best practices.
Chassis CSS tooltips operate on a manual initialization model for optimal performance and control:
- Explicit initialization required to prevent performance impact on unused elements
- Intelligent positioning powered by Popper.js for dynamic placement and collision detection
- Automatic placement adjustment based on viewport constraints
- Comprehensive event integration for application logic
- Accessibility through proper ARIA attributes and keyboard navigation support
- CSS custom properties for seamless theming and brand consistency
Implementation
Before implementing tooltips, ensure these dependencies and considerations are addressed:
- Include popper.min.js before
chassis.js, or usechassis.bundle.min.js - Tooltips with empty titles will not display
- Hidden elements cannot trigger tooltips
- Use
container: 'body'for complex component layouts (input groups, button groups) - Only apply tooltips to keyboard-focusable, interactive elements for accessibility compliance
- Hide tooltips before removing their trigger elements from the DOM
By default, this component uses the built-in content sanitizer, which strips out any HTML elements that are not explicitly allowed. See the sanitizer section in our JavaScript documentation for more details.
Chassis CSS respects user accessibility preferences by automatically disabling animations when the prefers-reduced-motion media query is detected. See the reduced motion guidelines in our accessibility documentation for implementation details.
Examples
Explore practical tooltip implementations demonstrating initialization patterns, positioning strategies, and customization techniques. These examples showcase Chassis CSS's flexible tooltip system across various use cases.
Initialization patterns
Tooltips require explicit initialization before use. The recommended approach is to initialize all tooltips using their data-cx-toggle attribute:
const tooltipTriggerList = document.querySelectorAll('[data-cx-toggle="tooltip"]')
const tooltipList = [...tooltipTriggerList].map(tooltipTriggerEl => new chassis.Tooltip(tooltipTriggerEl))
Interactive examples
Hover over the links below to see tooltips in action within contextual content:
Placeholder text to demonstrate some inline links with tooltips. This is now just filler, no killer. Content placed here just to mimic the presence of real text. And all that just to give you an idea of how tooltips would look when used in real-world situations. So hopefully you've now seen how these tooltips on links can work in practice, once you use them on your own site or project.
<p class="muted">Placeholder text to demonstrate some <a href="#" data-cx-toggle="tooltip" data-cx-title="Default tooltip">inline links</a> with tooltips. This is now just filler, no killer. Content placed here just to mimic the presence of <a href="#" data-cx-toggle="tooltip" data-cx-title="Another tooltip">real text</a>. And all that just to give you an idea of how tooltips would look when used in real-world situations. So hopefully you've now seen how <a href="#" data-cx-toggle="tooltip" data-cx-title="Another one here too">these tooltips on links</a> can work in practice, once you use them on <a href="#" data-cx-toggle="tooltip" data-cx-title="The last tip!">your own</a> site or project.</p> Feel free to use either title or data-cx-title in your HTML. When title is used, Popper will replace it automatically with data-cx-title when the element is rendered.
Custom appearance
Customize tooltip appearance using CSS custom properties. Apply custom classes with data-cx-custom-class="custom-tooltip" to scope styling and override CSS variables for brand-specific theming.
.custom-tooltip {
--cx-tooltip-bg-color: var(--cx-primary-60);
--cx-tooltip-fg-color: var(--cx-primary-20);
}
<button type="button" class="button secondary"
data-cx-toggle="tooltip" data-cx-placement="top"
data-cx-custom-class="custom-tooltip"
data-cx-title="This top tooltip is themed via CSS variables.">
Custom tooltip
</button> Positioning system
Chassis CSS tooltips support four directional placements with automatic RTL adaptation. Popper.js handles collision detection and fallback positioning for optimal visibility.
<button type="button" class="button secondary" data-cx-toggle="tooltip" data-cx-placement="top" data-cx-title="Tooltip on top">
Tooltip on top
</button>
<button type="button" class="button secondary" data-cx-toggle="tooltip" data-cx-placement="right" data-cx-title="Tooltip on right">
Tooltip on right
</button>
<button type="button" class="button secondary" data-cx-toggle="tooltip" data-cx-placement="bottom" data-cx-title="Tooltip on bottom">
Tooltip on bottom
</button>
<button type="button" class="button secondary" data-cx-toggle="tooltip" data-cx-placement="left" data-cx-title="Tooltip on left">
Tooltip on left
</button>
And with custom HTML added:
<button type="button" class="button secondary" data-cx-toggle="tooltip" data-cx-html="true" data-cx-title="<em>Tooltip</em> <u>with</u> <b>HTML</b>">
Tooltip with HTML
</button>
With an SVG:
Disabled elements
Elements with the disabled attribute aren't interactive, meaning users cannot focus, hover, or click them to trigger a tooltip. As a workaround, you'll want to trigger the tooltip from a wrapper <div> or <span>, ideally made keyboard-focusable using tabindex="0".
<span class="d-inline-block" tabindex="0" data-cx-toggle="tooltip" data-cx-title="Disabled tooltip">
<button class="button primary" type="button" disabled>Disabled button</button>
</span> Accessibility
Tooltip components in Chassis CSS prioritize accessibility through semantic markup, proper ARIA attributes, and keyboard navigation support. These features ensure tooltips enhance rather than hinder the user experience for all users.
Screen readers
Tooltips utilize standard HTML title attributes and proper ARIA labeling to communicate contextual information to assistive technologies. When implemented correctly, tooltips provide supplementary information without breaking the document flow.
Keyboard navigation
Chassis CSS tooltips provide comprehensive keyboard accessibility, ensuring equal access for all users through focus management and intuitive navigation patterns:
- Tooltips appear on focus for keyboard users, ensuring equal access to interactive elements
- Press escape to dismiss visible tooltips and return focus to the trigger element
- Tooltips don't interfere with natural tab order progression
Best practices
Follow these guidelines to ensure tooltips enhance accessibility rather than create barriers for users with disabilities.
Only apply tooltips to inherently focusable, interactive elements (links, buttons, form controls) to maintain accessibility standards. While other elements can be made focusable with tabindex="0", this creates confusing tab stops for keyboard users and may not announce properly in screen readers.
Keep tooltips accessible to keyboard and assistive technology users by only adding them to HTML elements that are traditionally keyboard-focusable and interactive (such as links or form inputs). Additionally, do not rely solely on hover as the trigger for your tooltips as this will make them impossible to trigger for keyboard users.
CSS
Customize tooltip appearance and behavior through Chassis CSS's comprehensive styling system, featuring CSS custom properties and Sass variables for design system integration.
Custom properties
Chassis CSS tooltips utilize CSS custom properties for runtime customization and design token integration. Local CSS variables on .tooltip enable dynamic theming while maintaining backward compatibility with Sass customization.
--#{$prefix}zindex: var(--#{$prefix}tooltip-zindex, #{$zindex-tooltip});
--#{$prefix}max-width: var(--#{$prefix}tooltip-max-width, #{$tooltip-max-width});
--#{$prefix}padding-x: var(--#{$prefix}tooltip-padding-x, #{$tooltip-padding-x});
--#{$prefix}padding-y: var(--#{$prefix}tooltip-padding-y, #{$tooltip-padding-y});
--#{$prefix}margin: var(--#{$prefix}tooltip-margin, #{$tooltip-margin});
@include map-font($tooltip-font, tooltip);
--#{$prefix}fg-color: var(--#{$prefix}tooltip-fg-color, #{$tooltip-fg-color});
--#{$prefix}bg-color: var(--#{$prefix}tooltip-bg-color, #{$tooltip-bg-color});
--#{$prefix}border-radius: var(--#{$prefix}tooltip-border-radius, #{$tooltip-border-radius});
--#{$prefix}opacity: var(--#{$prefix}tooltip-opacity, #{$tooltip-opacity});
--#{$prefix}arrow-width: var(--#{$prefix}tooltip-arrow-width, #{$tooltip-arrow-width});
--#{$prefix}arrow-height: var(--#{$prefix}tooltip-arrow-height, #{$tooltip-arrow-height});
Sass variables
Design system integration through Sass variables provides comprehensive tooltip customization for build-time theming and brand alignment.
$tooltip-font: $cx-font-text-small-normal;
$tooltip-max-width: 12.5rem; //200px;
$tooltip-fg-color: var(--#{$prefix}fg-inverse);
$tooltip-bg-color: var(--#{$prefix}bg-inverse);
$tooltip-border-radius: var(--#{$prefix}border-radius-medium);
$tooltip-opacity: .9;
$tooltip-padding-y: $spacer * .25;
$tooltip-padding-x: $spacer * .5;
$tooltip-margin: null;
$tooltip-arrow-width: .8rem;
$tooltip-arrow-height: .4rem;
JavaScript API
Control tooltip behavior programmatically with Chassis CSS's comprehensive JavaScript interface, featuring dynamic content generation, intelligent positioning, and accessibility features.
The tooltip plugin generates content and markup on demand, placing tooltips after their trigger element by default for optimal performance and accessibility. Initialize tooltips via JavaScript:
const exampleEl = document.getElementById('example')
const tooltip = new chassis.Tooltip(exampleEl, options)
Tooltips automatically attempt to change positions when a parent container has overflow: auto or overflow: scroll, but still keeps the original placement's positioning. Set the boundary option (for the flip modifier using the popperConfig option) to any HTMLElement to override the default value, 'clippingParents', such as document.body:
const tooltip = new chassis.Tooltip('#example', {
boundary: document.body // or document.querySelector('#boundary')
})
Data attributes
Individual tooltip options can be specified through data attributes for declarative configuration without JavaScript initialization.
Tooltips require minimal markup while generating semantic HTML with proper ARIA attributes for optimal accessibility and screen reader support. The required markup for a tooltip is only a data attribute and title on the HTML element:
<!-- HTML to write -->
<a href="#" data-cx-toggle="tooltip" data-cx-title="Some tooltip text!">Hover over me</a>
<!-- Generated markup by the plugin -->
<div class="tooltip cx-tooltip-auto" role="tooltip">
<div class="tooltip-arrow"></div>
<div class="tooltip-inner">
Some tooltip text!
</div>
</div>
Options
Chassis components offer flexible configuration through both data attributes and JavaScript. To use data attributes, prefix any option with data-cx- followed by the option name in kebab-case format (using hyphens instead of camelCase). For example, write data-cx-custom-class="my-class" rather than data-cx-customClass="my-class".
For more complex configurations, Chassis provides the data-cx-config attribute which accepts a JSON string of multiple settings. This approach simplifies markup when configuring several options at once. If the same option appears in both data-cx-config and as a separate data attribute (like data-cx-title), the individual attribute takes precedence. You can also use JSON values in individual attributes, such as data-cx-delay='{"show":100,"hide":200}' for more granular control.
When initializing components, Chassis merges configurations from multiple sources in this priority order: default settings, data-cx-config values, individual data-cx-* attributes, and finally any JavaScript object options. Values defined later in this sequence override earlier ones.
Note that for security reasons the sanitize, sanitizeFn, and allowList options cannot be supplied using data attributes.
| Name | Type | Default | Description |
|---|---|---|---|
allowList | object | Default value | Object which contains allowed attributes and tags. |
animation | boolean | true | Apply a CSS fade transition to the tooltip. |
boundary | string, element | 'clippingParents' | Overflow constraint boundary of the tooltip (applies only to Popper's preventOverflow modifier). By default, it's 'clippingParents' and can accept an HTMLElement reference (via JavaScript only). For more information refer to Popper's detectOverflow docs. |
container | string, element, false | false | Appends the tooltip to a specific element. Example: container: 'body'. This option is particularly useful in that it allows you to position the tooltip in the flow of the document near the triggering element - which will prevent the tooltip from floating away from the triggering element during a window resize. |
customClass | string, function | '' | Add classes to the tooltip when it is shown. Note that these classes will be added in addition to any classes specified in the template. To add multiple classes, separate them with spaces: 'class-1 class-2'. You can also pass a function that should return a single string containing additional class names. |
delay | number, object | 0 | Delay showing and hiding the tooltip (ms)—doesn't apply to manual trigger type. If a number is supplied, delay is applied to both hide/show. Object structure is: delay: { "show": 500, "hide": 100 }. |
fallbackPlacements | array | ['top', 'right', 'bottom', 'left'] | Define fallback placements by providing a list of placements in array (in order of preference). For more information refer to Popper's behavior docs. |
html | boolean | false | Allow HTML in the tooltip. If true, HTML tags in the tooltip's title will be rendered in the tooltip. If false, innerText property will be used to insert content into the DOM. Use text if you're worried about XSS attacks. |
offset | array, string, function | [0, 6] | Offset of the tooltip relative to its target. You can pass a string in data attributes with comma separated values like: data-cx-offset="10,20". When a function is used to determine the offset, it is called with an object containing the popper placement, the reference, and popper rects as its first argument. The triggering element DOM node is passed as the second argument. The function must return an array with two numbers: skidding, distance. For more information refer to Popper's offset docs. |
placement | string, function | 'top' | How to position the tooltip: auto, top, bottom, left, right. When auto is specified, it will dynamically reorient the tooltip. When a function is used to determine the placement, it is called with the tooltip DOM node as its first argument and the triggering element DOM node as its second. The this context is set to the tooltip instance. |
popperConfig | null, object, function | null | To change Chassis CSS's default Popper config, see Popper's configuration. When a function is used to create the Popper configuration, it's called with an object that contains the Chassis CSS's default Popper configuration. It helps you use and merge the default with your own configuration. The function must return a configuration object for Popper. |
sanitize | boolean | true | Enable or disable the sanitization. If activated 'template', 'content' and 'title' options will be sanitized. |
sanitizeFn | null, function | null | Here you can supply your own sanitize function. This can be useful if you prefer to use a dedicated library to perform sanitization. |
selector | string, false | false | If a selector is provided, tooltip objects will be delegated to the specified targets. In practice, this is used to also apply tooltips to dynamically added DOM elements (jQuery.on support). See this issue and an informative example. Note: title attribute must not be used as a selector. |
template | string | '<div class="tooltip" role="tooltip"><div class="tooltip-arrow"></div><div class="tooltip-inner"></div></div>' | Base HTML to use when creating the tooltip. The tooltip's title will be injected into the .tooltip-inner. .tooltip-arrow will become the tooltip's arrow. The outermost wrapper element should have the .tooltip class and role="tooltip". |
title | string, element, function | '' | The tooltip title. If a function is given, it will be called with its this reference set to the element that the tooltip is attached to. |
trigger | string | 'hover focus' | How tooltip is triggered: click, hover, focus, manual. You may pass multiple triggers; separate them with a space. 'manual' indicates that the tooltip will be triggered programmatically via the .tooltip('show'), .tooltip('hide') and .tooltip('toggle') methods; this value cannot be combined with any other trigger. 'hover' on its own will result in tooltips that cannot be triggered via the keyboard, and should only be used if alternative methods for conveying the same information for keyboard users is present. |
Popper configuration
Advanced Popper.js configuration can be customized through a function that receives the default Chassis CSS configuration and returns a modified configuration object.
const tooltip = new chassis.Tooltip(element, {
popperConfig(defaultCxPopperConfig) {
// const newPopperConfig = {...}
// use defaultCxPopperConfig if needed...
// return newPopperConfig
}
})
Methods
Chassis CSS provides a comprehensive set of methods for programmatic tooltip control, enabling dynamic content updates and lifecycle management.
All Chassis CSS component methods are asynchronous and initiate CSS transitions. Methods return immediately when the transition begins, not when it completes. Calling methods on components that are already transitioning will be ignored to prevent conflicts. Learn more about Chassis JavaScript patterns.
| Method | Description |
|---|---|
disable | Removes the ability for an element's tooltip to be shown. The tooltip will only be able to be shown if it is re-enabled. |
dispose | Hides and destroys an element's tooltip (Removes stored data on the DOM element). Tooltips that use delegation (which are created using the selector option) cannot be individually destroyed on descendant trigger elements. |
enable | Gives an element's tooltip the ability to be shown. Tooltips are enabled by default. |
getInstance | Static method which allows you to get the tooltip instance associated with a DOM element. |
getOrCreateInstance | Static method which allows you to get the tooltip instance associated with a DOM element, or create a new one in case it wasn't initialized. |
hide | Hides an element's tooltip. Returns to the caller before the tooltip has actually been hidden (i.e. before the hidden.cx.tooltip event occurs). This is considered a "manual" triggering of the tooltip. |
setContent | Gives a way to change the tooltip's content after its initialization. |
show | Reveals an element's tooltip. Returns to the caller before the tooltip has actually been shown (i.e. before the shown.cx.tooltip event occurs). This is considered a "manual" triggering of the tooltip. Tooltips with zero-length titles are never displayed. |
toggle | Toggles an element's tooltip. Returns to the caller before the tooltip has actually been shown or hidden (i.e. before the shown.cx.tooltip or hidden.cx.tooltip event occurs). This is considered a "manual" triggering of the tooltip. |
toggleEnabled | Toggles the ability for an element's tooltip to be shown or hidden. |
update | Updates the position of an element's tooltip. |
const tooltip = chassis.Tooltip.getInstance('#example') // Returns a Chassis CSS tooltip instance
// setContent example
tooltip.setContent({ '.tooltip-inner': 'another title' })
The setContent method accepts an object argument, where each property-key is a valid string selector within the tooltip template, and each related property-value can be string | element | function | null
Events
Listen to tooltip lifecycle events to integrate with your application logic and create responsive user interactions.
| Event | Description |
|---|---|
hide.cx.tooltip | This event is fired immediately when the hide instance method has been called. |
hidden.cx.tooltip | This event is fired when the tooltip has finished being hidden from the user (will wait for CSS transitions to complete). |
inserted.cx.tooltip | This event is fired after the show.cx.tooltip event when the tooltip template has been added to the DOM. |
show.cx.tooltip | This event fires immediately when the show instance method is called. |
shown.cx.tooltip | This event is fired when the tooltip has been made visible to the user (will wait for CSS transitions to complete). |
const myTooltipEl = document.getElementById('myTooltip')
const tooltip = chassis.Tooltip.getOrCreateInstance(myTooltipEl)
myTooltipEl.addEventListener('hidden.cx.tooltip', () => {
// do something...
})
tooltip.hide()