Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Selected Reading
Create tooltips with CSS
A tooltip is a small popup that appears when a user hovers over an element, providing additional information or context. CSS tooltips are lightweight, accessible, and don't require JavaScript.
Syntax
.tooltip {
position: relative;
}
.tooltip .tooltiptext {
visibility: hidden;
position: absolute;
z-index: 1;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
Example: Basic Tooltip
The following example creates a simple tooltip that appears on hover −
<!DOCTYPE html>
<html>
<head>
<style>
.tooltip {
position: relative;
display: inline-block;
cursor: pointer;
color: blue;
text-decoration: underline;
}
.tooltip .tooltiptext {
visibility: hidden;
width: 150px;
background-color: #333;
color: white;
text-align: center;
border-radius: 6px;
padding: 8px;
position: absolute;
z-index: 1;
bottom: 125%;
left: 50%;
margin-left: -75px;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
.tooltip .tooltiptext::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: #333 transparent transparent transparent;
}
</style>
</head>
<body>
<p>Hover over the <span class="tooltip">tooltip text<span class="tooltiptext">This is a tooltip!</span></span> to see the tooltip.</p>
</body>
</html>
A paragraph with underlined blue text "tooltip text" appears. When hovering over it, a dark tooltip box with white text "This is a tooltip!" appears above with a small arrow pointing down to the trigger text.
Example: Positioned Tooltips
You can position tooltips in different directions using CSS positioning −
<!DOCTYPE html>
<html>
<head>
<style>
.tooltip {
position: relative;
display: inline-block;
margin: 50px;
padding: 10px;
background-color: #f0f0f0;
border: 1px solid #ccc;
cursor: pointer;
}
.tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: white;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
opacity: 0;
transition: opacity 0.3s;
}
.tooltip:hover .tooltiptext {
visibility: visible;
opacity: 1;
}
.tooltip-top .tooltiptext {
bottom: 125%;
left: 50%;
margin-left: -60px;
}
.tooltip-right .tooltiptext {
top: -5px;
left: 125%;
}
.tooltip-bottom .tooltiptext {
top: 125%;
left: 50%;
margin-left: -60px;
}
.tooltip-left .tooltiptext {
top: -5px;
right: 125%;
}
</style>
</head>
<body>
<div class="tooltip tooltip-top">Top
<span class="tooltiptext">Tooltip on top</span>
</div>
<div class="tooltip tooltip-right">Right
<span class="tooltiptext">Tooltip on right</span>
</div>
<div class="tooltip tooltip-bottom">Bottom
<span class="tooltiptext">Tooltip on bottom</span>
</div>
<div class="tooltip tooltip-left">Left
<span class="tooltiptext">Tooltip on left</span>
</div>
</body>
</html>
Four gray boxes labeled "Top", "Right", "Bottom", and "Left" appear on the page. Hovering over each box shows a dark tooltip in the corresponding direction with appropriate positioning text.
Conclusion
CSS tooltips are created using the :hover pseudo-class with visibility and position properties. They provide an effective way to display additional information without JavaScript, and can be positioned in any direction around the trigger element.
Advertisements
