iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

CSS Tooltips

A tooltip is a tiny floating label that appears on hover or focus. Pure CSS can build a simple one in about 15 lines.

HTML

HTML
<span class="tip">
  Hover me
  <span class="tip-bubble">I'm a tooltip</span>
</span>

CSS

CSS
.tip { position: relative; cursor: help; border-bottom: 1px dashed #888; }

.tip-bubble {
  position: absolute;
  left: 50%;
  bottom: calc(100% + 6px);
  transform: translateX(-50%);
  background: #282a35;
  color: #fff;
  padding: 6px 10px;
  border-radius: 4px;
  font-size: 13px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.15s;
}
.tip:hover .tip-bubble,
.tip:focus-within .tip-bubble { opacity: 1; }

The native option: title

OptionProsCons
title="…" attributeZero CSS, accessible.Hard to style, slow to appear, mobile ignores it.
CSS tooltipStyled to match your brand.Need ARIA work for full accessibility.
Popover API (popover attribute)Modern, accessible, top-layer.Newer — check browser support.
Tip: For real product tooltips, add role="tooltip" and an aria-describedby on the trigger. Screen readers will announce the bubble correctly.

Example

Example
<!DOCTYPE html>
<html>
<head>
<style>
body { font-family: Verdana, sans-serif; }
.box { background: #04AA6D; color: #fff; padding: 20px; border-radius: 6px; }
</style>
</head>
<body>

<h1>CSS Tooltips</h1>
<div class="box">Edit the CSS on the left, then click Run &raquo;</div>

</body>
</html>
Try it Yourself »

Exercise

Stop the tooltip from intercepting mouse clicks.

.tip-bubble { opacity: 0; -events: none; }

Test yourself

Q1. A pure-CSS tooltip is usually positioned…
Q2. Which pseudo-class keeps the tooltip open while a child is focused?
Q3. Which property prevents the tooltip from intercepting clicks?

Discussion

Loading…