Tooltip (CSS-only)

A CSS tooltip shows a hint on hover, with no JavaScript, using the ::after pseudo-element and an opacity transition.

Code

<span class="tooltip-wrap" data-tip="Additional info" style="position:relative;border-bottom:1px dotted #8b949e;cursor:help">
  Hover me
</span>
<style>
  .tooltip-wrap::after {
    content: attr(data-tip);
    position: absolute; bottom: 125%; left: 50%; transform: translateX(-50%);
    background: #21262d; color: #fff; padding: 0.4rem 0.7rem; border-radius: 6px;
    font-size: 0.8rem; white-space: nowrap; opacity: 0; pointer-events: none;
    transition: opacity 0.15s;
  }
  .tooltip-wrap:hover::after { opacity: 1; }
</style>

Preview

Explanation

content: attr(data-tip) pulls the tooltip text straight from an HTML attribute — no need to duplicate the text in CSS. opacity + transition give a smooth fade with no JS.

  • :hover doesn't work naturally on mobile — for touch screens, also expose the hint via title or a separate UI element.

Related tools