Base Components

Tailwind CSS Tooltip

Tailwind CSS Tooltip provides users with contextual information or additional details when they hover over or interact with an element on a page. It typically appears as a small pop-up box near the element being hovered over, containing relevant information such as explanations, hints, or descriptions.

Default Tooltip

Click the below toggle button to reveal the default tooltip example.
Hey, a delightful tooltip is here!

Transition

Use Tailwind CSS Transition classes to achieve smooth transitions for your tooltips' appearance.
Sleek tooltip with opacity transition effect.

Advanced Content

The tooltip content can consist of any HTML element, rather than being limited to simple text.
Trial expires in 29 days

Customization

Remove the default class tooltip and utilize Tailwind CSS classes to completely customize the appearance and style of your tooltips.

Use as Popover

Replace the default tooltip class with popover to transform a basic tooltip into a more comprehensive popover, featuring a heading and detailed content.
Popover Title
Behold this captivating popover content. It's quite engaging, wouldn't you say ?

Placement

Use data-tooltip-placement attribute with left , right , top , bottom values control the tooltip display direction.
Tooltip positioned at the top.
Tooltip positioned at the bottom.
Tooltip positioned at the left.
Tooltip positioned at the right.

Trigger Options

Enhance tooltip functionality by utilizing the data-tooltip-trigger attribute with either click or focus to tailor tooltip activation to your preference. By default, tooltips are triggered on hover .
I am triggered by hover.
I am triggered by click.
I am triggered by focus.

Offset

Use data-tooltip-offset="20px,20px" attribute to add distance between the toggle and tooltip element. The pair of x,y value in pixel translates the floating element along the x and y axes.
Tooltip with custom offset.

Delay

Set custom delay timeouts for displaying and hiding tooltips using the data-tooltip-delay-show="1000" and data-tooltip-delay-hide="1000" attributes, specified in milliseconds. The below examples shows and hides the tooltip with 1000ms(1 second) delay.
Tooltip with custom offset.

Permanent

Use the data-tooltip-permanent="true" attribute to set tooltips remain persistent and do not close when clicked outside. This feature is exclusive to tooltips triggered by clicking, specified with data-tooltip-trigger="click" .
Hey, a delightful tooltip is here!

Source Code

The following source files manage the JavaScript behavior and CSS styles of the Tailwind CSS Tooltip component. Upon building the assets using Webpack Build Tools , these files are compiled and added into the dist/assets/css/styles.css and dist/assets/js/core.bundle.js bundles, making them available globally across all pages.
File Description
src/core/components/tooltip/tooltip.ts The TypeScript source file that manages the JavaScript behavior of the Tailwind CSS Tooltip component.
src/core/plugins/components/tooltip.js The Tailwind CSS compatible plugin written in PostCSS for applying CSS styles to the Tailwind CSS Tooltip component.

HTML Markup

The following markup outlines the core structure of the Tailwind CSS Tooltip component.
				
					<button class="btn btn-primary" data-tooltip="#my_tooltip_content" id="my_tooltip">
 Toggle Tooltop
</button>
<div class="tooltip" id="my_tooltip_content">
 Hey, a delightful tooltip is here!
</div>

				
			

Data Attribute Initialization

Auto-initialize all KTTooltip instances on page load by adding the data-tooltip="true" attribute to your tooltip elements. This triggers on the fly initialization by the KTTooltip JavaScript component.
				
					<button class="btn btn-primary" data-tooltip="#my_tooltip_content" id="my_tooltip">
 Toggle Tooltop
</button>
<div class="tooltip" id="my_tooltip_content">
 Hey, a delightful tooltip is here!
</div>

				
			
These attributes allow you to set options for the KTTooltip component during initialization. The values you provide as strings are automatically converted to the appropriate KTTooltip Options .
HTML Attribute Type Default Description
data-tooltip string - Used to auto-initialize KTTooltip instances on page load and provides a string value serving as an ID selector, "#content_id" for a tooltip content element. Alternatively, you can remove it and perform initialization using JavaScript.
data-tooltip-hidden-class string "hidden" Tailwind CSS class to use for the hidden state of the tooltip elements.
data-tooltip-trigger
enum
"hover" | "click"
"hover" Specifies the tooltip display direction.
data-tooltip-placement
enum
"top" | "bottom" | "left" | "right"
"top" Specifies the trigger method for displaying tooltips.
data-tooltip-offset string "0, 5px" Sets a distance(the x and y axes) between the toggle and tooltip element.
data-tooltip-delay-show number 0 Sets a custom delay timeouts(in ms) for displaying tooltips.
data-tooltip-delay-hide number 0 Sets a custom delay timeouts(in ms) for hiding tooltips.
data-tooltip-permanent boolean false Sets tooltips remain persistent and do not close when clicked outside. This feature is exclusive to tooltips triggered by clicking.

JavaScript Initialization

To initialize a new tooltip component, pass the corresponding DOM element and configuration options to the KTTooltip class constructor.
				
					// Tooltip toggle element
const tooltipEl = document.querySelector('#my_tooltip');

// Configuration options(optional)
const options = {
	target: '#my_tooltip_content'
};

// Initialize object
const tooltip = new KTTooltip(tooltipEl, options);

				
			

To initialize the tooltip with JavaScript, use the data-tooltip="false" attribute instead. This prevents automatic initialization on page load.

Options

This table outlines the configuration options for initialization of Tailwind CSS Tooltip instances, using the KTTooltip JavaScript component.
Option Type Default Description
target string - An ID selector of a tooltips content element. Example: "#my_tooltip_element" .
trigger
enum
"hover" | "click"
"hover" Specifies the tooltip display direction.
placement
enum
"top" | "bottom" | "left" | "right"
"top" Specifies the trigger method for displaying tooltips.
offset string "0, 5px" Sets a distance(the x and y axes) between the toggle and tooltip element.
delayShow number 0 Sets a custom delay timeouts(in ms) for displaying tooltips.
delayHide number 0 Sets a custom delay timeouts(in ms) for hiding tooltips.
permanent boolean false Sets tooltips remain persistent and do not close when clicked outside. This feature is exclusive to tooltips triggered by clicking.
hiddenClass string "hidden" Tailwind CSS class to use for the hidden state of tooltip elements.

Utilities

Manage and interact with KTTooltip instances using these static methods of KTTooltip class.
Method Description
init() Automatically initializes KTTooltip object for all elements with the data-tooltip="true" attribute on page load.
createInstances() Allows to create KTTooltip instances for all elements that have been dynamically added to the DOM but haven't been activated yet.
getInstance(element) Returns the KTTooltip object associated with the given DOM element element .
getOrCreateInstance(element) Returns the existing KTTooltip object for the provided DOM element element , or creates a new instance if none exists, then returns the same.
				
					// Initialize all tooltips
KTTooltip.init()

// Initialzie pending tooltips
KTTooltip.createInstances();

// Get tooltip object
const tooltipEl = document.querySelector('#my_tooltip');
const tooltip = KTTooltip.getInstance(tooltipEl);

				
			

Methods

Use KTTooltip component's API methods to programmatically control its behavior.
Method Description
new KTTooltip(element, options) Creates an object instance of KTTooltip class for the given DOM element and configuration options .
show() Shows a tooltip element.
hide() Hides a tooltip element.
toggle() Toggles a tooltip state to shown or hidden.
getOption(name) Retrieves the value of a configuration option by name parameter from a KTTooltip instance.
getElement() Retrieves the DOM element linked to a specific KTTooltip instance.
on(eventName, handler) Allows attaching event listeners to the KTTooltip custom events using the eventName and eventId string parameters. These events enable programmatic interaction based on user actions or internal state changes of KTTooltip. The function returns string as a unique identifier for the registered listener, allowing you to remove it later if needed.
off(eventName, eventId) Removes an event listener for the eventName and eventId parameters attached with the on method.
dispose() Removes the KTTooltip instance from an element, including any associated data stored on the DOM element.
				
					const tooltipEl = document.querySelector('#my_tooltip');
const tooltip = KTTooltip.getInstance(tooltipEl);

tooltip.show();
tooltip.hide();
tooltip.toggle();

				
			

Events

KTTooltip custom events allows you to register callback functions(event listeners) that will be invoked automatically whenever specific custom events are triggered within the component.
Event Description
show Triggered immediately before a tooltip element is shown.
shown Triggered immediately after a tooltip element is shown.
hide Triggered immediately before a tooltip element is hidden.
hidden Triggered immediately after a tooltip element is hidden.
toggle Triggered immediately before a tooltip element is toggled(shown/hidden).
				
					const tooltipEl = document.querySelector('#my_tooltip');
const tooltip = KTTooltip.getInstance(tooltipEl);

tooltip.on('show', () => {
	console.log('show event');
});

tooltip.on('shown', () => {
	console.log('shown event');
});

tooltip.on('hide', (detail) => {
	detail.cancel = true;
	console.log('hide action canceled');
});