Customise your web projects with an easy-to-use and responsive Tailwind CSS tooltip!
A tooltip is a small pop-up element that appears while the user moves the mouse pointer over an element. Use it when you want to specify extra information about something when the user moves the mouse pointer over an element.
See below our example that will help you create a simple tooltip for your project.
You can initialize a new tooltip by adding the data-tooltip-target="
data attribute to the trigger element and the {tooltipName}
"data-tooltip="
to the element that you want to use as the tooltip.{tooltipName}
"
You can change the position of the tooltip relative to it's trigger element by adding the data-tooltip-placement="
data attribute to the tooltip element by default it set's to {top}
"top
.
Check the placement values for tooltip here.
You can modify the open/close state animation for tooltip by adding the data-tooltip-mount="
, {opacity-100}
"data-tooltip-unmount="
and {opacity-0}
"data-tooltip-transition="
data attributes to the tooltip element. You can pass tailwind css classnames for those data attributes for animation the tooltip. {transition-opacity}
"
Check more about animation data attributes for tooltip here.
The following data attributes are available for tooltip trigger element.
Attribute | Description |
---|---|
data-tooltip-target | Set the tooltip target element, it should be the same as the data-tooltip data attribute. |
The following data attributes are available for tooltip element.
Attribute | Description | Default |
---|---|---|
data-tooltip | Set the name of the tooltip and reference it to the tooltip trigger element. | |
data-tooltip-offset | Set the offset between the tooltip and the tooltip trigger element. | 5 |
data-tooltip-placement | Set the position of the tooltip relative to it's trigger element. | top |
data-tooltip-mount | Set the classnaes that should be used when the tooltip is visible. | opacity-1 |
data-tooltip-unmount | Set the classnaes that should be used when the tooltip is hidden. | opacity-0 pointer-events-none |
data-tooltip-transition | Set the classnaes that should be used for transition of the tooltip. | transition-opacity duration-300 |
The tooltip component needs a required script file to work, you just need to add the below script file to the bottom of your html file.
<!-- from node_modules -->
<script
type="module"
src="node_modules/@material-tailwind/html@latest/scripts/tooltip.js"
></script>
<!-- from cdn -->
<script
type="module"
src="https://unpkg.com/@material-tailwind/html@latest/scripts/tooltip.js"
></script>