Tooltips
Part of the Practical Frontend section of Coddy's HTML journey — lesson 23 of 35.
Tooltips are small, informative pop-up elements that appear when a user hovers over or focuses on an interface element. They provide additional context without cluttering the main UI.
Let's create a basic tooltip using HTML and CSS:
First, create the HTML structure:
<div class="tooltip-container">
<button>Hover over me</button>
<span class="tooltip">This is a tooltip!</span>
</div>Next, add the CSS to position and show/hide the tooltip:
.tooltip-container {
position: relative;
display: inline-block;
}
.tooltip {
visibility: hidden;
/* Position the tooltip */
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
/* Add a slight delay before showing */
opacity: 0;
transition: opacity 0.3s;
}
/* Show the tooltip when hovering over the container */
.tooltip-container:hover .tooltip {
visibility: visible;
opacity: 1;
}With this code, when you hover over the button, a tooltip will appear above it.
Challenge
EasyYou are given an HTML button with a tooltip that appears when the user hovers over the "Help" button. Your task:
- Hide the tooltip by default.
- Add a background color and text color so it stands out.
- Center the text inside the tooltip.
- Add some padding so the tooltip looks nicer.
- Round the corners with
border-radius.
Cheat sheet
Tooltips are small, informative pop-up elements that appear when hovering over interface elements.
Basic HTML structure:
<div class="tooltip-container">
<button>Hover over me</button>
<span class="tooltip">This is a tooltip!</span>
</div>CSS for positioning and showing/hiding tooltips:
.tooltip-container {
position: relative;
display: inline-block;
}
.tooltip {
visibility: hidden;
position: absolute;
bottom: 125%;
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 0.3s;
}
.tooltip-container:hover .tooltip {
visibility: visible;
opacity: 1;
}Try it yourself
<!DOCTYPE html>
<html>
<head>
<title>Tooltips</title>
<style>
.tooltip-container {
position: relative;
display: inline-block;
margin: 50px;
}
button {
padding: 8px 16px;
cursor: pointer;
}
.tooltip {
/* Animation properties */
opacity: 0;
transition: opacity 0.3s;
}
/* Show the tooltip when hovering over the container */
.tooltip-container:hover .tooltip {
visibility: visible;
opacity: 1;
}
</style>
</head>
<body>
<div class="tooltip-container">
<button>Help</button>
<span class="tooltip">Click for assistance</span>
</div>
</body>
</html>This lesson includes a short quiz. Start the lesson to answer it and track your progress.