Ask any question about CSS here... and get an instant response.
Post this Question & Answer:
How can I create a CSS-only tooltip that appears on hover?
Asked on Apr 23, 2026
Answer
To create a CSS-only tooltip that appears on hover, you can use the `:hover` pseudo-class to display a hidden element containing the tooltip text. Here's a simple example to illustrate this concept.
<!-- BEGIN COPY / PASTE -->
<div class="tooltip-container">
Hover over me
<span class="tooltip-text">Tooltip content</span>
</div>
<style>
.tooltip-container {
position: relative;
display: inline-block;
cursor: pointer;
}
.tooltip-text {
visibility: hidden;
width: 120px;
background-color: black;
color: #fff;
text-align: center;
border-radius: 5px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%; /* Position above the text */
left: 50%;
margin-left: -60px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip-container:hover .tooltip-text {
visibility: visible;
opacity: 1;
}
</style>
<!-- END COPY / PASTE -->Additional Comment:
- The tooltip is initially hidden using "visibility: hidden" and "opacity: 0".
- On hover, the tooltip becomes visible with "visibility: visible" and "opacity: 1".
- The transition effect is achieved with "transition: opacity 0.3s" for a smooth fade-in effect.
- Positioning is controlled using "position: absolute" and offsets to align the tooltip correctly.
- Ensure the container has "position: relative" to keep the tooltip positioned correctly relative to the text.
Recommended Links:
