CSSmediumCSS

Pure CSS Tooltip

Create tooltips using only CSS with no JavaScript required.

01

The problem

You need to display additional information on hover without using JavaScript.

02

The solution

CSS
.tooltip {
  position: relative;
  cursor: help;
}

.tooltip::before,
.tooltip::after {
  position: absolute;
  left: 50%;
  opacity: 0;
  visibility: hidden;
  transition: all 0.3s ease;
  z-index: 1000;
}

/* Tooltip text */
.tooltip::before {
  content: attr(data-tooltip);
  bottom: calc(100% + 10px);
  transform: translateX(-50%);
  padding: 8px 12px;
  background: #1a1a2e;
  color: #fff;
  font-size: 14px;
  white-space: nowrap;
  border-radius: 6px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.3);
}

/* Tooltip arrow */
.tooltip::after {
  content: '';
  bottom: calc(100% + 2px);
  transform: translateX(-50%);
  border: 8px solid transparent;
  border-top-color: #1a1a2e;
}

.tooltip:hover::before,
.tooltip:hover::after {
  opacity: 1;
  visibility: visible;
}

/* Bottom tooltip variant */
.tooltip.bottom::before {
  bottom: auto;
  top: calc(100% + 10px);
}

.tooltip.bottom::after {
  bottom: auto;
  top: calc(100% + 2px);
  border-top-color: transparent;
  border-bottom-color: #1a1a2e;
}

03

See it live

Isolated previewSandboxed
04

Put it to work

Example
<span class="tooltip" data-tooltip="This is helpful info!">
  Hover over me
</span>

<button class="tooltip bottom" data-tooltip="Click to save">
  Save
</button>

Worth knowing

Use data-tooltip attribute for the tooltip text. Add ARIA labels for accessibility.