Pure CSS Tooltip
A lightweight, smooth hover tooltip engineered entirely with CSS, completely bypassing the need for JavaScript.
HTMLCSS
This snippet delivers a highly performant, accessible tooltip component built exclusively with HTML and CSS. It features a buttery-smooth fade and float animation, elegantly positioning itself above the target element upon hover.
Preview
Implementation Code
Copy the code below and drop it directly into your project.
<!-- index.html -->
<div class="tooltip-wrapper">
<button class="tooltip-trigger">Hover me</button>
<div class="tooltip-content">
This is a pure CSS tooltip!
</div>
</div>/* styles.css */
.tooltip-wrapper {
position: relative;
display: inline-block;
}
.tooltip-trigger {
padding: 10px 20px;
border-radius: 8px;
border: none;
background-color: #3b82f6;
color: #fff;
cursor: pointer;
font-weight: bold;
transition: background-color 0.2s;
}
.tooltip-trigger:hover {
background-color: #2563eb;
}
.tooltip-content {
position: absolute;
bottom: 130%;
left: 50%;
transform: translateX(-50%) translateY(10px);
opacity: 0;
visibility: hidden;
transition: all 0.3s ease;
background-color: #1f2937;
color: #fff;
padding: 8px 12px;
border-radius: 6px;
font-size: 14px;
white-space: nowrap;
z-index: 10;
box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1), 0 2px 4px -1px rgba(0, 0, 0, 0.06);
}
/* Downward triangle (arrow) */
.tooltip-content::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -6px;
border-width: 6px;
border-style: solid;
border-color: #1f2937 transparent transparent transparent;
}
/* Hover behavior */
.tooltip-wrapper:hover .tooltip-content {
opacity: 1;
visibility: visible;
transform: translateX(-50%) translateY(0);
} Explanation
- Positioning (
relative&absolute): By applyingposition: relativeto the.tooltip-wrapper, the child tooltip body (.tooltip-content) can be precisely anchored usingposition: absolute. - Visibility vs. Display: Because CSS
transitionproperties ignoredisplay: none, this implementation relies on a combination ofvisibility: hiddenandopacity: 0to manage the hidden state while still allowing for smooth fade-in animations. - Fluid Animation: The satisfying “float-up” effect is achieved by offsetting the tooltip slightly on the Y-axis (
translateY(10px)) and animating it to its natural position (translateY(0)) alongside the opacity change when hovered. - CSS Triangle Indicator: The speech-bubble arrow (or tail) is ingeniously constructed using the
.tooltip-content::afterpseudo-element and transparent CSS borders.