Component

Directional Tooltip

Tooltips in all four directions - top, right, bottom, left - with arrows that reposition automatically. Direction is controlled by the data-tooltip-pos attribute. Pure CSS, no JavaScript.

  • Component
  • Tailwind
  • HTML
  • MIT

Preview

Live component

Interactive preview
Open preview
9:41 100%
devsnips.dev/library/tailwind/components/tooltips/directional-tooltip/
fluid · no fixed width 100%
No dependencies Production-ready

Install

Add to your project

terminal
npx devsnips add Tailwind/Components/Tooltips/directional-tooltip
registry path: Tailwind/Components/Tooltips/directional-tooltip

Source

Implementation

code.html
<!--
Snippet Name: Directional Tooltip
Description: Tooltips in all four directions (top, right, bottom, left) with auto-positioned arrows. Direction is controlled by the data-tooltip-pos attribute. Pure CSS, no JavaScript.
Author: DevSnips Contributors
Usage Example: Add data-tooltip="text" data-tooltip-pos="top|right|bottom|left" to any focusable element.
-->
<div class="flex flex-wrap items-center justify-center gap-8">
  <button type="button" class="tt rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2" data-tooltip="Appears on top" data-tooltip-pos="top">Top</button>
  <button type="button" class="tt rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2" data-tooltip="Appears on the right" data-tooltip-pos="right">Right</button>
  <button type="button" class="tt rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2" data-tooltip="Appears on bottom" data-tooltip-pos="bottom">Bottom</button>
  <button type="button" class="tt rounded-lg border border-gray-300 bg-white px-4 py-2 text-sm font-medium text-gray-700 hover:bg-gray-50 focus:outline-none focus-visible:ring-2 focus-visible:ring-blue-500 focus-visible:ring-offset-2" data-tooltip="Appears on the left" data-tooltip-pos="left">Left</button>
</div>

<style>
  .tt { position: relative; }
  .tt::after {
    content: attr(data-tooltip);
    position: absolute;
    padding: 6px 10px;
    border-radius: 8px;
    background: #111827;
    color: #fff;
    font-size: 12.5px;
    font-weight: 500;
    line-height: 1.3;
    white-space: nowrap;
    opacity: 0;
    visibility: hidden;
    transition: opacity .15s ease, transform .15s ease, visibility .15s;
    pointer-events: none;
    z-index: 50;
  }
  .tt::before {
    content: '';
    position: absolute;
    border: 6px solid transparent;
    opacity: 0;
    visibility: hidden;
    transition: opacity .15s ease, visibility .15s;
    pointer-events: none;
    z-index: 50;
  }
  /* TOP */
  .tt[data-tooltip-pos="top"]::after {
    left: 50%; bottom: calc(100% + 10px);
    transform: translateX(-50%) translateY(4px);
  }
  .tt[data-tooltip-pos="top"]::before {
    left: 50%; bottom: calc(100% + 4px);
    transform: translateX(-50%);
    border-top-color: #111827;
  }
  /* RIGHT */
  .tt[data-tooltip-pos="right"]::after {
    top: 50%; left: calc(100% + 10px);
    transform: translateY(-50%) translateX(-4px);
  }
  .tt[data-tooltip-pos="right"]::before {
    top: 50%; left: calc(100% + 4px);
    transform: translateY(-50%);
    border-right-color: #111827;
  }
  /* BOTTOM */
  .tt[data-tooltip-pos="bottom"]::after {
    left: 50%; top: calc(100% + 10px);
    transform: translateX(-50%) translateY(-4px);
  }
  .tt[data-tooltip-pos="bottom"]::before {
    left: 50%; top: calc(100% + 4px);
    transform: translateX(-50%);
    border-bottom-color: #111827;
  }
  /* LEFT */
  .tt[data-tooltip-pos="left"]::after {
    top: 50%; right: calc(100% + 10px);
    transform: translateY(-50%) translateX(4px);
  }
  .tt[data-tooltip-pos="left"]::before {
    top: 50%; right: calc(100% + 4px);
    transform: translateY(-50%);
    border-left-color: #111827;
  }
  /* reveal */
  .tt:hover::after, .tt:focus-visible::after { opacity: 1; visibility: visible; }
  .tt[data-tooltip-pos="top"]:hover::after, .tt[data-tooltip-pos="top"]:focus-visible::after { transform: translateX(-50%) translateY(0); }
  .tt[data-tooltip-pos="right"]:hover::after, .tt[data-tooltip-pos="right"]:focus-visible::after { transform: translateY(-50%) translateX(0); }
  .tt[data-tooltip-pos="bottom"]:hover::after, .tt[data-tooltip-pos="bottom"]:focus-visible::after { transform: translateX(-50%) translateY(0); }
  .tt[data-tooltip-pos="left"]:hover::after, .tt[data-tooltip-pos="left"]:focus-visible::after { transform: translateY(-50%) translateX(0); }
  .tt:hover::before, .tt:focus-visible::before { opacity: 1; visibility: visible; }
</style>
90 lines UTF-8 · LF · Spaces: 2

AI-ready

Available to your agent.

This component is reachable through the DevSnips CLI, MCP server and Skills integrations — one registry, one resource path.

Resource path

devsnips://components/tooltips/directional-tooltip

Continue browsing

View all