|
1 | 1 | --- |
2 | 2 | layout: home |
3 | 3 | title: Tailwind CSS Tooltip - Flowbite |
4 | | -description: Use the following Tailwind CSS powered tooltips to show extra content when hovering or clicking on an element |
| 4 | +description: Use the following Tailwind CSS powered tooltips to show extra content when hovering or focusing on an element |
5 | 5 | group: components |
6 | 6 | toc: true |
| 7 | +requires_js: true |
7 | 8 |
|
8 | 9 | previous: Tables |
9 | 10 | previousLink: components/tables |
| 11 | + |
| 12 | +next: Datepicker |
| 13 | +nextLink: plugins/datepicker |
10 | 14 | --- |
11 | 15 |
|
12 | | -FlowBite includes custom styles using Tailwind CSS and Tippy JS to enable tooltips that can be shown when hovering or clicking on an element powered by data attributes. |
| 16 | +Flowbite allows you to use the Tailwind CSS tooltip component to show extra information when hovering or focusing over an element in multiple positions, styles, and animations. |
| 17 | + |
| 18 | +Before continuing, make sure that you have the <a href="{{< ref "getting-started/quickstart" >}}" rel="nofollow" target="_blank">Flowbite JavaScript file</a> included in your project in order to make the tooltip component work. |
13 | 19 |
|
14 | 20 | ## Using tooltips |
15 | 21 |
|
16 | | -If you want to use tooltips, make sure that you have **Tippy JS** included in your project. Learn how to do that by reading the official guide on <a href="https://atomiks.github.io/tippyjs/v6/getting-started/" target="_blank">Tippy JS</a>. |
| 22 | +To get started with using tooltips all you need to do is add the `data-tooltip-target="{elementId}"` data attribute to an element where `elementId` is the id of the tooltip component. In the following example you can see the button that will trigger the `tooltip-default` element to be shown when hovered or focused. |
| 23 | + |
| 24 | +{{< example >}} |
| 25 | +<button data-tooltip-target="tooltip-default" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
| 26 | +<div id="tooltip-default" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 duration-300 transition-opacity invisible"> |
| 27 | + Tooltip content |
| 28 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 29 | +</div> |
| 30 | +{{< /example >}} |
| 31 | + |
| 32 | +## Tooltip styles |
17 | 33 |
|
18 | | -Apply the `data-tippy-content` data attribute to any element and the content inside will be applied as the tooltip text. |
| 34 | +You can use choose between dark and light version styles for the tooltip component by changing the utility classes from Tailwind CSS and by applying the `data-tooltip-style="{light|dark}"` data attribute. |
19 | 35 |
|
20 | 36 | {{< example >}} |
21 | | -<button data-tippy-content="Tooltip Content" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
| 37 | +<!-- Light style tooltip --> |
| 38 | +<button data-tooltip-target="tooltip-light" data-tooltip-style="light" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Light tooltip</button> |
| 39 | +<div id="tooltip-light" role="tooltip" class="tooltip absolute z-10 inline-block bg-white font-medium shadow-sm text-gray-900 border border-gray-200 py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 40 | + Tooltip content |
| 41 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 42 | +</div> |
| 43 | + |
| 44 | +<!-- Dark style tooltip --> |
| 45 | +<button data-tooltip-target="tooltip-dark" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Dark tooltip</button> |
| 46 | +<div id="tooltip-dark" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 47 | + Tooltip content |
| 48 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 49 | +</div> |
22 | 50 | {{< /example >}} |
23 | 51 |
|
24 | | -## Positioning |
| 52 | +## Placement |
25 | 53 |
|
26 | | -You can also set the positioning of the tooltips by using the `data-tippy-placement={placement}` data attribute. |
| 54 | +The positioning of the tooltip element relative to the triggering element (eg. button, link) can be set using the `data-tooltip-placement="{top|right|bottom"left}"` data attribute. |
27 | 55 |
|
28 | 56 | {{< example >}} |
29 | | -<button data-tippy-content="Tooltip placement top" data-tippy-placement="top" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip top</button> |
30 | | -<button data-tippy-content="Tooltip placement right" data-tippy-placement="right" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip right</button> |
31 | | -<button data-tippy-content="Tooltip placement bottom" data-tippy-placement="bottom" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip bottom</button> |
32 | | -<button data-tippy-content="Tooltip placement left" data-tippy-placement="left" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip left</button> |
| 57 | +<!-- Show tooltip on top --> |
| 58 | +<button data-tooltip-target="tooltip-top" data-tooltip-placement="top" type="button" class="mb-2 md:mb-0 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip top</button> |
| 59 | +<div id="tooltip-top" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 60 | + Tooltip on top |
| 61 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 62 | +</div> |
| 63 | + |
| 64 | +<!-- Show tooltip on right --> |
| 65 | +<button data-tooltip-target="tooltip-right" data-tooltip-placement="right" type="button" class="mb-2 md:mb-0 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip right</button> |
| 66 | +<div id="tooltip-right" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 67 | + Tooltip on right |
| 68 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 69 | +</div> |
| 70 | + |
| 71 | +<!-- Show tooltip on bottom --> |
| 72 | +<button data-tooltip-target="tooltip-bottom" data-tooltip-placement="bottom" type="button" class="mb-2 md:mb-0 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip bottom</button> |
| 73 | +<div id="tooltip-bottom" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 74 | + Tooltip on bottom |
| 75 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 76 | +</div> |
| 77 | + |
| 78 | +<!-- Show tooltip on left --> |
| 79 | +<button data-tooltip-target="tooltip-left" data-tooltip-placement="left" type="button" class="mb-2 md:mb-0 text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip left</button> |
| 80 | +<div id="tooltip-left" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 81 | + Tooltip on left |
| 82 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 83 | +</div> |
33 | 84 | {{< /example >}} |
34 | 85 |
|
35 | | -## Delay |
| 86 | +## Triggering |
36 | 87 |
|
37 | | -You can also apply a delay before the tooltip shows up using the `data-tippy-delay={amount}`. The following example applies a one second delay to the tooltip. |
| 88 | +You can choose the triggering event by using the `data-tooltip-trigger="{hover|click}"` data attributes to choose whether you want to show the tooltip when hovering or clicking on the element. By default this option is set to `hover`. |
38 | 89 |
|
39 | 90 | {{< example >}} |
40 | | -<button data-tippy-content="Tooltip Content" data-tippy-delay="1000" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
| 91 | +<!-- Show tooltip on hover --> |
| 92 | +<button data-tooltip-target="tooltip-hover" data-tooltip-trigger="hover" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip hover</button> |
| 93 | +<div id="tooltip-hover" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 94 | + Tooltip content |
| 95 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 96 | +</div> |
| 97 | + |
| 98 | +<!-- Show tooltip on click --> |
| 99 | +<button data-tooltip-target="tooltip-click" data-tooltip-trigger="click" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Tooltip click</button> |
| 100 | +<div id="tooltip-click" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 101 | + Tooltip content |
| 102 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 103 | +</div> |
41 | 104 | {{< /example >}} |
42 | 105 |
|
43 | 106 | ## Animation |
44 | 107 |
|
45 | | -You can also use an animation for the tooltips by using the `data-tippy-animation="scale"` or `data-tippy-animation="fade"` data attributes. |
| 108 | +You can set tooltip animation styles by using the transition utility classes from Tailwind CSS. Make sure that you add `transition-opacity` and `duration-{x}` to set the animation duration. |
46 | 109 |
|
47 | 110 | {{< example >}} |
48 | | -<button data-tippy-content="Tooltip Content" data-tippy-animation="scale" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
49 | | -<button data-tippy-content="Tooltip Content" data-tippy-animation="fade" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
| 111 | +<button data-tooltip-target="tooltip-animation" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Animated tooltip</button> |
| 112 | +<div id="tooltip-animation" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 duration-300 transition-opacity invisible"> |
| 113 | + Tooltip content |
| 114 | + <div class="tooltip-arrow" data-popper-arrow></div> |
| 115 | +</div> |
50 | 116 | {{< /example >}} |
51 | 117 |
|
52 | 118 | ## Disable arrow |
53 | 119 |
|
54 | | -You can also use tooltips without the arrow styling by using the `data-tippy-arrow="false"` data attribute. |
| 120 | +You can also disable the tooltip arrow by not including the `data-popper-arrow` element. |
55 | 121 |
|
56 | 122 | {{< example >}} |
57 | | -<button data-tippy-content="Tooltip Content" data-tippy-arrow="false" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
| 123 | +<button data-tooltip-target="tooltip-no-arrow" type="button" class="text-white bg-blue-700 hover:bg-blue-800 focus:ring-4 focus:ring-blue-300 font-medium rounded-lg text-sm px-5 py-2.5 text-center">Default tooltip</button> |
| 124 | +<div id="tooltip-no-arrow" role="tooltip" class="tooltip absolute z-10 inline-block bg-gray-900 font-medium shadow-sm text-white py-2 px-3 text-sm rounded-lg opacity-0 invisible"> |
| 125 | + Tooltip content |
| 126 | +</div> |
58 | 127 | {{< /example >}} |
59 | | - |
60 | | -## Additional options |
61 | | - |
62 | | -Learn more about what options you can apply using Tailwind CSS and the tooltip plugin on <a href="https://atomiks.github.io/tippyjs/v6/all-props/" target="_blank">Tippy.js</a>. |
|
0 commit comments