Tailwind Tooltip CSS With Arrow On Four Sides

Four buttons on a project board, each with a dark tooltip on a different side: Copy link above, Export to the left, Archive to the right and Pin below. Every tooltip has a small rotated-square arrow pointing at its button, fades in while sliding four pixels into place, and shows on keyboard focus exactly as it does on hover. Escape hides it.

Code

import { useState } from "react";

// Pure Tailwind tooltips on four sides: shown by group-hover and group-focus-within, linked by aria-describedby, closed by Escape.
const tips = [
  { id: "tt1-a", label: "Copy link", tip: "Anyone with the link can view", side: "top" },
  { id: "tt1-c", label: "Archive", tip: "Hidden from the board, not deleted", side: "right" },
  { id: "tt1-b", label: "Export", tip: "Saves a PDF of this page", side: "left" },
  { id: "tt1-d", label: "Pin", tip: "Keeps it first in the list", side: "bottom" },
] as const;

const place = {
  top: { box: "bottom-full left-1/2 mb-2.5 -translate-x-1/2 translate-y-1 group-hover:translate-y-0 group-focus-within:translate-y-0", arrow: "left-1/2 top-full -mt-1 -translate-x-1/2" },
  bottom: { box: "top-full left-1/2 mt-2.5 -translate-x-1/2 -translate-y-1 group-hover:translate-y-0 group-focus-within:translate-y-0", arrow: "left-1/2 bottom-full -mb-1 -translate-x-1/2" },
  left: { box: "right-full top-1/2 mr-2.5 -translate-y-1/2 translate-x-1 group-hover:translate-x-0 group-focus-within:translate-x-0", arrow: "left-full top-1/2 -ml-1 -translate-y-1/2" },
  right: { box: "left-full top-1/2 ml-2.5 -translate-y-1/2 -translate-x-1 group-hover:translate-x-0 group-focus-within:translate-x-0", arrow: "right-full top-1/2 -mr-1 -translate-y-1/2" },
};

// Each placement gets its own cell, with the button pushed away from the side its tooltip opens on.
const cell = {
  top: "items-end justify-center pb-5",
  bottom: "items-start justify-center pt-5",
  left: "items-center justify-end pr-5",
  right: "items-center justify-start pl-5",
};

export default function TailwindTooltipCssWithArrowOnFourSides() {
  const [closed, setClosed] = useState<string | null>(null);
  // All four start visible so the preview shows them; the first pointer move or key press hands over to hover and focus.
  const [demo, setDemo] = useState(true);
  const stopDemo = () => demo && setDemo(false);
  return (
    <section className="bg-indigo-50 px-5 py-16" onPointerMove={stopDemo} onKeyDown={stopDemo}>
      <div className="mx-auto max-w-2xl rounded-2xl bg-white p-5 sm:p-8 shadow-sm ring-1 ring-indigo-100">
        <h2 className="text-center text-lg font-semibold text-slate-900">Q3 roadmap board</h2>
        <p className="mt-1 text-center text-sm text-slate-600">Hover or tab to a button. Press Escape to hide its tooltip.</p>
        <div className="mt-8 grid gap-3 sm:grid-cols-2">
          {tips.map((t) => (
            <div key={t.id} className={`relative flex h-32 rounded-xl bg-slate-50 ring-1 ring-slate-200 ${cell[t.side]}`}>
              <span className="absolute left-3 top-2 text-[11px] font-semibold uppercase tracking-wider text-slate-500">{t.side}</span>
              <span className="group relative inline-flex" onMouseLeave={() => setClosed(null)}>
                <button
                  type="button"
                  aria-describedby={t.id}
                  onBlur={() => setClosed(null)}
                  onKeyDown={(e) => e.key === "Escape" && setClosed(t.id)}
                  className="min-h-[44px] rounded-lg border border-slate-300 bg-white px-4 text-sm font-medium text-slate-800 hover:bg-slate-50 focus-visible:outline-none focus-visible:ring-2 focus-visible:ring-indigo-600"
                >
                  {t.label}
                </button>
                <span
                  role="tooltip"
                  id={t.id}
                  className={`pointer-events-none absolute z-10 w-max max-w-[10rem] rounded-md bg-slate-900 px-2.5 py-1.5 text-xs font-medium leading-snug text-white opacity-0 shadow-lg transition duration-150 group-hover:opacity-100 group-focus-within:opacity-100 motion-reduce:transition-none ${place[t.side].box} ${demo && closed !== t.id ? "!opacity-100" : ""} ${closed === t.id ? "!opacity-0" : ""}`}
                >
                  {t.tip}
                  <span aria-hidden="true" className={`absolute h-2 w-2 rotate-45 bg-slate-900 ${place[t.side].arrow}`} />
                </span>
              </span>
            </div>
          ))}
        </div>
      </div>
    </section>
  );
}

Install with one command: npx shadcn@latest add https://we.inc/r/tailwind-tooltip-css-with-arrow-on-four-sides.json

When to use it

  • Buttons or links need a short explanation that would clutter the layout if always visible.
  • You want tooltips without a positioning library, written in Tailwind classes or a few lines of CSS.
  • A toolbar sits near a screen edge and some tooltips must open sideways or downward.
  • Your audit flagged hover-only tooltips that keyboard users never see.

How to customize it

  • Change the placement per trigger with `data-side` in the HTML version, or the `side` field in the `tips` array in React.
  • Swap `bg-slate-900` (and `#0f172a`) on both the tooltip and its arrow for another colour; the arrow inherits it in the CSS version.
  • Adjust the gap between trigger and tooltip with `--tt1-gap`, or `mb-2.5` and its siblings in the `place` map.
  • Allow longer text by raising `max-w-[11rem]`; the tooltip wraps rather than running off screen.

Accessibility

  • Each tooltip has role tooltip and an id that its button references with aria-describedby, so the text is read after the button's name.
  • Tooltips open on focus-within as well as hover, and pressing Escape hides the open one without moving focus.
  • The tooltip has pointer-events none, so it never covers or steals clicks from neighbouring controls.

Questions

Why does the tooltip use opacity instead of display none?

An element with display none cannot transition, and screen readers still read text referenced by aria-describedby when it is merely transparent. Opacity with pointer-events none keeps it invisible, unclickable and ready to fade in.

How is the arrow drawn?

It is an 8px square with the tooltip's background, rotated 45 degrees and pulled half its size under the tooltip edge. Only one triangle of it pokes out, which reads as an arrow, and it inherits the colour so you change it in one place.

Do tooltips work on touch screens?

Tapping a button gives it focus, which shows its tooltip until focus moves elsewhere. Because touch users may still miss it, put anything they must know in visible text and keep tooltips as extra help.

How do I flip a tooltip that hits the edge?

With pure CSS, pick the side per trigger, or switch data-side at a breakpoint for small screens. Automatic flipping based on available space needs script that measures the trigger, or CSS anchor positioning with position-try fallbacks in browsers that support it.

Related: HTML CSS Button Solid Outline Ghost With Hover Active Disabled States, HTML CSS Round Icon Only Buttons In A Player.

More content sections or all free UI components. Build your site free to have a full site built around it.

More in Content

Product

Who It's For

Features

Resources

Company

View Sitemap