Update v4.2 - New components, 10+ framework guides, and quality improvements. Visit Changelog

Tailwind CSS Tooltip & Popover Plugin API

Headless, unstyled tooltip and popover plugin built with JavaScript and TypeScript, including installation, usage, options, methods, events, and selectors.

JavaScript Typescript

Installation

To get started, install the Tooltip & Popover plugin via npm, else you can skip this step if you are already using Preline UI as a package.

  1. Install the plugin

    Install @preline/tooltip via npm

    Terminal
                              
                                npm i @preline/tooltip
                              
                            
  2. Add the plugin CSS

    Use @source to register the plugin's JavaScript path for Tailwind CSS scanning, then @import the plugin's CSS files into your Tailwind CSS file.

    main.css
                              
                                @import "tailwindcss";
                                
                                /* @preline/tooltip */
                                /* [!code highlight:3] */
                                @source "../node_modules/@preline/tooltip/*.js";
                                @import "./node_modules/@preline/tooltip/variants.css";
                                @import "./node_modules/@preline/tooltip/theme.css";
                              
                            
  3. Add the plugin JavaScript

    Include the JavaScript <script> that powers the interactive elements near the end of your </body> tag:

    HTML
                              
                            

    Additional Initialization Options

    Use the non-auto entry if you need manual initialization. In this mode, automatic initialization on page load is not included, so the component should be initialized explicitly.

    HTML (non-auto)
                              
                            

    Via bundler

    When using a bundler (Vite, webpack, etc.), import the plugin directly as an ES module.

    JavaScript (auto)
                              
                                import "@preline/tooltip";
                              
                            
    JavaScript (non-auto)
                              
                                import HSTooltip from "@preline/tooltip/non-auto";
                                
                                HSTooltip.autoInit();
                                
                                // Or initialize a specific element manually
                                const el = document.querySelector("#tooltip");
                                if (el) new HSTooltip(el);
                              
                            

Example

Hover over the buttons below to see the four tooltip directions: top, right, bottom, and left. Remove the [--placement:*] option to enable automatic positioning.

Basic usage

Prefer to create your own style? Here is a completely unstylized example.

HTML
                      
                        <div class="hs-tooltip inline-block">
                          <button type="button" class="hs-tooltip-toggle">
                            Hover me
                            <span class="hs-tooltip-content hs-tooltip-shown:opacity-100 hs-tooltip-shown:visible opacity-0 transition-opacity inline-block absolute invisible z-10 py-1 px-2 bg-stone-900 text-white" role="tooltip">
                              Tooltip on top
                            </span>
                          </button>
                        </div>
                      
                    

Class Options

Name Description Options Default value
[--trigger:*] Event to trigger a tooltip. Should be added to the container (.hs-tooltip). "focus" | "hover" hover
[--placement:*] Tooltip placement. If "auto", the tooltip will be positioned based on the available space. Should be added to the container (.hs-tooltip). "auto" | "top" | "top-left" | "top-right" | "bottom" | "bottom-left" | "bottom-right" | "right" | "right-top" | "right-bottom" | "left" | "left-top" | "left-bottom" auto [top → bottom, left → right]
[--strategy:*] Sets the position to absolute instead of fixed, and removes the transform styles to position the menu relative to the relative parent block. Should be added to the container (.hs-tooltip). "fixed" | "absolute" absolute
[—-prevent-popper:*] Prevents Floating UI positioning calculation issue. Should be added to the container (.hs-tooltip). "true" | "false" false

Selectors

Name Description
hs-tooltip Tooltip container.
hs-tooltip-toggle Tooltip toggle.
hs-tooltip-content Tooltip content.

Methods

The HSTooltip object is contained within the global window object

Method Description
Public methods
show() Force show tooltip.
hide() Force hide tooltip.
destroy() Destroys the instance, removes generated markup (if any), removes added classes and attributes.
Static methods
HSTooltip.getInstance(target, isInstance) Returns the element associated to the target.
  • target should be a Node or string (valid selector)
  • isInstance boolean. Returns the instance instead of Node if true
HSTooltip.show(target) Shows the element associated to the target.
  • target should be a Node or string (valid selector)
HSTooltip.hide(target) Hides the element associated to the target.
  • target should be a Node or string (valid selector)

Force show tooltip (public method).

JavaScript
                      
                        const tooltip = new HSTooltip(document.querySelector('#tooltip'));
                        const showBtn = document.querySelector('#show-btn');

                        showBtn.addEventListener('click', () => {
                          tooltip.show();
                        });
                      
                    

Force show tooltip (static method).

JavaScript
                      
                        const showBtn = document.querySelector('#show-btn');

                        showBtn.addEventListener('click', () => {
                          HSTooltip.show(document.querySelector('#tooltip'));
                        });
                      
                    

Open item (mixed).

JavaScript
                      
                        const { element } = HSTooltip.getInstance('#tooltip', true);
                        const showBtn = document.querySelector('#show-btn');

                        showBtn.addEventListener('click', () => {
                          element.show();
                        });
                      
                    

Events

Method Description
on:show Called when tooltip is shown.
on:hide Called when tooltip is hidden.

An example where a function is run every time a tooltip shows.

JavaScript
                      
                        const el = HSTooltip.getInstance('#tooltip');

                        el.on('show', (instance) => {...});
                      
                    

Ready to use Components

Looking for prebuilt UI components based on the Tailwind CSS? Preline UI packs hundreds of component examples for all your website needs.

Tooltip & Popover

Explore ready-to-use Tailwind CSS examples built with Preline UI.

View Tooltip & Popover examples

Tooltip & Popover

© 2026 Preline Labs.