Skip to content
WebTricks

CSS-only tooltip

A pure-CSS tooltip that appears on hover and focus, with content from a data attribute.

  • tooltip
  • hover
  • accessibility
  • data-attribute
  • effect
Tooltip color#1f2330
Text color#ffffff
Gap8px
HTML
<span class="tooltip" tabindex="0" data-tip="Pure CSS — no JavaScript">Hover or focus me</span>
CSS
:root {
    --tip-bg: #1f2330;
    --tip-fg: #ffffff;
    --gap: 8px;
}

.tooltip {
    position: relative;
    color: #fff;
    border-bottom: 1px dashed currentColor;
    cursor: help;
}

.tooltip::after {
    content: attr(data-tip);
    position: absolute;
    left: 50%;
    bottom: 100%;
    transform: translateX(-50%);
    margin-bottom: var(--gap);

    padding: 6px 10px;
    border-radius: 6px;
    background: var(--tip-bg);
    color: var(--tip-fg);
    font-size: 0.8rem;
    white-space: nowrap;
    pointer-events: none;

    opacity: 0;
    transition: opacity 0.15s ease;
}

.tooltip:hover::after,
.tooltip:focus-visible::after {
    opacity: 1;
}

How it works

The tooltip text lives in a data-tip attribute and is pulled into a pseudo-element with content: attr(data-tip). It’s positioned above the trigger and fades in on hover.

  • pointer-events: none keeps the bubble from blocking the mouse.
  • :focus-visible makes it appear for keyboard users too — add tabindex="0" to non-interactive triggers (as in the demo).
  • white-space: nowrap keeps short tips on one line; remove it and add a max-width for longer text.

For rich or interactive tooltips, a JS solution with proper ARIA is better — this is ideal for simple, static hints.