Generate a pure-CSS tooltip using ::before / ::after — pick position, colors, arrow, and offset
Position
Appearance
Live preview
Hover the button below to see the tooltip.
CSS
.tooltip {
position: relative;
display: inline-block;
}
.tooltip::after {
content: attr(data-tooltip);
position: absolute;
bottom: calc(100% + 8px);
left: 50%;
transform: translateX(-50%);
padding: 6px 10px;
background: #1f2937;
color: #ffffff;
font-size: 13px;
line-height: 1.3;
white-space: nowrap;
border-radius: 6px;
opacity: 0;
transition: opacity 0.2s ease;
pointer-events: none;
z-index: 10;
}
.tooltip:hover::after {
opacity: 1;
}
.tooltip::before {
content: "";
position: absolute;
top: 100%;
left: 50%;
transform: translateX(-50%);
border-width: 6px;
border-style: solid;
border-color: #1f2937 transparent transparent transparent;
opacity: 0;
transition: opacity 0.2s ease;
pointer-events: none;
}
.tooltip:hover::before {
opacity: 1;
}HTML
<span class="tooltip" data-tooltip="Hi there!">Hover me</span>