CSS Tooltip Generator
Generate a CSS-only tooltip with an arrow, in any of four positions, using a data attribute for the text so no JavaScript and no extra markup are needed.
Loading tool…
The CSS is generated by JavaScript in your browser and never leaves the page. There is nothing to upload and nothing is stored.
What the CSS Tooltip Generator does
The CSS tooltip generator on this page will generate a CSS-only tooltip with an arrow, in any of four positions, using a data attribute for the text so no JavaScript and no extra markup are needed. Everything runs inside this page, so the work happens on your own device rather than on a server. It is most often used for a hint on an icon button whose meaning is not obvious from the icon alone. There are more free online tools on FreeToolsWorld.org.
How to use it
- Choose which side the tooltip appears on.
- Set the colours, padding, radius and maximum width.
- Set the arrow size, and keep the distance from the element a pixel or two larger than it.
- Copy the rules, add class="tooltip" and data-tip="your text" to the element, and give it tabindex="0" if it is not already focusable.
How it works
The tooltip is two pseudo-elements on the element itself: ::after is the bubble and ::before is the arrow. That means no extra markup — no wrapper div, no hidden span — and the text lives in a data attribute which content: attr(data-tip) pulls into the bubble. Putting the text in the markup rather than in the stylesheet matters more than it sounds: a string buried in a CSS file cannot be translated, cannot be edited by whoever maintains the content, and does not show up when someone greps for it.
The bubble is positioned against the element, which needs position: relative for that to work — it is the one declaration on the element itself and the commonest thing missing when a tooltip appears in the top-left corner of the page instead. Centring it uses the two-step trick: left: 50% puts its left edge at the element’s midpoint, and transform: translateX(-50%) pulls it back by half its own width, which works without knowing how wide the bubble is. width: max-content lets it size to its text, and max-width caps it so a long sentence wraps rather than running off the screen.
The arrow is the border triangle, which means no image to load and no SVG to inline. It is placed so that its tip meets the bubble, and that is why the distance from the element should be slightly larger than the arrow size: at equal values the arrow reaches all the way back to the element and the whole thing looks glued on. The generator warns when those two numbers collide.
The reveal is on :hover and :focus-visible together, and the second one is not optional. A tooltip that only appears on hover is unreachable by keyboard and unreachable on a touchscreen, where there is no hover state at all. For a focus state to fire, the element has to be focusable: a link or a button already is, but a span needs tabindex="0". The transition is on opacity rather than display or visibility, because display cannot be transitioned and a visibility change jumps.
The honest limitation is accessibility, and it is worth being clear about. A CSS-generated tooltip is not announced by assistive technology: content: attr() output is decorative as far as the accessibility tree is concerned, and many screen readers skip it entirely. So this is right for a hint that merely repeats or embellishes something already visible, and wrong for information a reader actually needs. For that, put the text in a real element and connect it with aria-describedby, or simply show it. The other limitation is clipping: a tooltip on an element inside a container with overflow: hidden will be cut off, which is the usual reason one mysteriously disappears inside a scrolling panel or a table cell.
When to use it
- A hint on an icon button whose meaning is not obvious from the icon alone.
- Expanding an abbreviation or a unit on hover.
- Showing the full text of something truncated with an ellipsis.
- A label on a chart element or a small control.
- Any hover hint where loading a tooltip library would be more weight than the feature deserves.
Common mistakes to avoid
- Leaving position: relative off the element, so the tooltip positions against the page instead.
- Showing it on :hover only, which excludes keyboard and touch users entirely.
- Forgetting tabindex="0" on a non-focusable element, so :focus-visible never fires.
- Making the arrow as large as the gap, so it touches the element and looks glued on.
- Transitioning display or visibility instead of opacity, which cannot animate smoothly.
- Relying on a CSS tooltip for information a screen reader needs. It is not announced.
- Putting one inside a container with overflow: hidden, which clips it.
Frequently asked questions
How do I make a tooltip with only CSS?
Put the text in a data attribute, pull it into a ::after pseudo-element with content: attr(data-tip), position it against the element — which needs position: relative — and reveal it on :hover and :focus-visible. The arrow is a second pseudo-element made from borders.
Why is my tooltip appearing in the corner of the page?
Because the element it belongs to has no position: relative, so the absolutely positioned pseudo-element is positioning itself against the nearest ancestor that does — often the page itself. It is the single most common mistake with this technique.
How do I centre a tooltip without knowing its width?
left: 50% then transform: translateX(-50%). The first puts the bubble’s left edge at the element’s centre, the second shifts it back by half its own width. The percentage in a translate resolves against the element’s own size, which is what makes it work at any width.
Are CSS tooltips accessible?
Only partly, and the gap matters. Adding :focus-visible makes them reachable by keyboard, which this generator does. But content generated by CSS is treated as decorative and many screen readers do not announce it, so a CSS tooltip is fine for a hint and wrong for information. For that, use a real element with aria-describedby.
Why does my tooltip get cut off?
An ancestor has overflow: hidden, scroll or auto, which clips anything positioned outside it. Common culprits are cards with rounded corners, table wrappers and scrolling panels. Either move the tooltip’s positioning context outside the clipped container, or use a library that portals it to the end of the body.
Does this work on touchscreens?
Partly. There is no hover on touch, so the :focus-visible rule is what gives you anything at all, and it fires only on focusable elements after a tap. If a touch audience needs the information, show it rather than hiding it behind an interaction.