Published on

Here Are 6 Ways To Create A Tailwind CSS tooltip With Tailwind CSS

Tailwind CSS tooltip

What is Tailwind CSS?

Tailwind CSS is a utility-first CSS framework that focuses on creating personalized user interfaces quickly. It can gives you all the building blocks you are able to create personalized designs without having to fight to override irritating opinionated styles. Also, Tailwind CSS is a highly configurable, low-level CSS framework.

The description of Tailwind CSS tooltip ui component

Use these tooltip components to show extra information when hovering or focusing over an element flowbite.com/docs/components/tooltips/

Why use Tailwind CSS to make a Tailwind CSS tooltip ui component?

  • It can make the building process of Tailwind CSS tooltip ui component faster and more easily.
  • Enables building complex responsive layouts and components freely.
  • Minimum lines of CSS code in Tailwind CSS tooltip component file.

The preview of Tailwind CSS tooltip ui component

Free download of the Tailwind CSS tooltip's source code

The source code of Tailwind CSS tooltip ui component

<!-- This is an example component -->
<div class="max-w-lg mx-auto">

    <link rel="stylesheet" href="https://unpkg.com/@themesberg/[email protected]/dist/flowbite.min.css" />
    
    <!-- Show tooltip on top -->
    <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>
    <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" data-popper-reference-hidden="" data-popper-escaped="" data-popper-placement="top" style="position: absolute; inset: auto auto 0px 0px; margin: 0px; transform: translate3d(918px, 449px, 0px);">
        Tooltip on top
        <div class="tooltip-arrow" data-popper-arrow="" style="position: absolute; left: 0px; transform: translate3d(54px, 0px, 0px);"></div>
    </div>

    <!-- Show tooltip on right -->
    <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>
    <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" data-popper-reference-hidden="" data-popper-escaped="" data-popper-placement="right" style="position: absolute; inset: 0px auto auto 0px; margin: 0px; transform: translate3d(1162px, 1620px, 0px);">
        Tooltip on right
        <div class="tooltip-arrow" data-popper-arrow="" style="position: absolute; top: 0px; transform: translate3d(0px, 28px, 0px);"></div>
    </div>

    <!-- Show tooltip on bottom -->
    <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>
    <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" data-popper-reference-hidden="" data-popper-escaped="" data-popper-placement="top" style="position: absolute; inset: auto auto 0px 0px; margin: 0px; transform: translate3d(1156px, 449px, 0px);">
        Tooltip on bottom
        <div class="tooltip-arrow" data-popper-arrow="" style="position: absolute; left: 0px; transform: translate3d(67px, 0px, 0px);"></div>
    </div>

    <!-- Show tooltip on left -->
    <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>
    <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" data-popper-reference-hidden="" data-popper-escaped="" data-popper-placement="left" style="position: absolute; inset: 0px 0px auto auto; margin: 0px; transform: translate3d(-1266.22px, 1620px, 0px);">
        Tooltip on left
        <div class="tooltip-arrow" data-popper-arrow="" style="position: absolute; top: 0px; transform: translate3d(0px, 28px, 0px);"></div>
    </div>

    <p class="mt-5">This tooltip element is part of a larger, open-source library of Tailwind CSS components. Learn more by going to the official <a class="text-blue-600 hover:underline" href="https://flowbite.com/docs/getting-started/introduction/" target="_blank">Flowbite Documentation</a>.</p>
</div>

<script src="https://unpkg.com/@themesberg/[email protected]/dist/flowbite.bundle.js"></script>

How to make a Tailwind CSS tooltip with Tailwind CSS?

Install tailwind css of verion 2.2.4

Use the script html tag to import the script of Tailwind CSS of the version 2.2.4

<script src="https://cdn.tailwindcss.com"></script>

All the unility class needed to make a Tailwind CSS tooltip component

  • max-w-lg
  • mx-auto
  • mb-2
  • md:mb-0
  • text-white
  • bg-blue-700
  • hover:bg-blue-800
  • text-sm
  • px-5
  • py-2.5
  • text-center
  • absolute
  • z-10
  • inline-block
  • bg-gray-900
  • py-2
  • px-3
  • invisible
  • mt-5
  • text-blue-600

20 steps to make a Tailwind CSS tooltip component with Tailwind CSS

  1. Set the maximum width/height of an element using the max-w-lg utilities.

  2. Control the horizontal margin of an element to auto using the mx-auto utilities.

  3. Control the margin on bottom side of an element to 0.5rem using the mb-2 utilities.

  4. Control the margin on bottom side of an element to 0rem at only medium screen sizes using the md:mb-0 utilities.

  5. Control the text color of an element to white using the text-white utilities.

  6. Control the background color of an element to blue-700 using the bg-blue-700 utilities.

  7. Control the background color of an element to blue-800 using the hover:bg-blue-800 utilities on hover.

  8. Control the text color of an element to sm using the text-sm utilities.

  9. Control the horizontal padding of an element to 1.25rem using the px-5 utilities.

  10. Control the vertical padding of an element to 2.5 using the py-2.5 utilities.

  11. Control the text color of an element to center using the text-center utilities.

  12. Use absolute to position an element outside of the normal flow of the document, causing neighboring elements to act as if the element doesn’t exist.

  13. Control the stack order (or three-dimensional positioning) of an element to 10 in Tailwind, regardless of order it has been displayed, using the z-10 utilities.

  14. Use inline-block utilities to wrap the element to prevent the text inside from extending beyond its parent.

  15. Control the background color of an element to gray-900 using the bg-gray-900 utilities.

  16. Control the vertical padding of an element to 0.5rem using the py-2 utilities.

  17. Control the horizontal padding of an element to 0.75rem using the px-3 utilities.

  18. Use invisible to hide an element, but still maintain its place in the DOM, affecting the layout of other elements.

  19. Control the margin on top side of an element to 1.25rem using the mt-5 utilities.

  20. Control the text color of an element to blue-600 using the text-blue-600 utilities.

Conclusion

The above is a step-by-step tutorial on how to use Tailwind CSS to make a Tailwind CSS tooltip components, learn and follow along to implement your own components.