SyntaxStudy
Sign Up
CSS CSS Tooltip with Pseudo-Elements
CSS Intermediate 5 min read

CSS Tooltip with Pseudo-Elements

CSS Tooltips

Build simple tooltips using ::before (tooltip box) and ::after (arrow) on a data-tooltip attribute, with no JavaScript required.

Example
[data-tooltip] {
  position: relative;
  cursor: help;
}

[data-tooltip]::before {
  content: attr(data-tooltip);
  position: absolute;
  bottom: calc(100% + 8px);
  left: 50%;
  transform: translateX(-50%);
  background: #333;
  color: white;
  padding: 4px 8px;
  border-radius: 4px;
  white-space: nowrap;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
}

[data-tooltip]:hover::before { opacity: 1; }
Pro Tip

For accessibility, supplement CSS tooltips with aria-describedby pointing to a real hidden element for screen readers.