CSS-only tooltip
A pure-CSS tooltip that appears on hover and focus, with content from a data attribute.
Tooltip color#1f2330
Text color#ffffff
Gap8px
<span class="tooltip" tabindex="0" data-tip="Pure CSS — no JavaScript">Hover or focus me</span> :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: nonekeeps the bubble from blocking the mouse.:focus-visiblemakes it appear for keyboard users too — addtabindex="0"to non-interactive triggers (as in the demo).white-space: nowrapkeeps short tips on one line; remove it and add amax-widthfor longer text.
For rich or interactive tooltips, a JS solution with proper ARIA is better — this is ideal for simple, static hints.