Back to Web Development
2026-02-165 min read

CSS Tooltips (Web Development)

Learn CSS Tooltips (Web Development) step by step with clear examples and exercises.

Why This Matters

In this guide, we'll delve into creating interactive and informative tooltips using CSS. This tutorial is designed to equip you with practical skills that can help you impress during job interviews, troubleshoot real-world issues, and enhance your web development projects.

Why This Matters

Tooltips are small popup boxes that provide additional information about a specific element on a webpage when the user hovers over it. They are essential for improving user experience by making websites more intuitive and accessible. Tooltips can help users understand complex interfaces, provide context for unfamiliar elements, and offer helpful tips or instructions.

Prerequisites

Core Concept

Creating a Basic Tooltip

Let's start by creating a simple tooltip for an HTML element using CSS:

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: dotted 1px black; /* Add a bottom border to trigger the tooltip */
}

.tooltip .tooltiptext {
visibility: hidden;
width: 120px;
background-color: #555;
color: #fff;
text-align: center;
padding: 5px 0;
position: absolute;
z-index: 1;
}

.tooltip .tooltiptext::after {
content: "";
position: absolute;
border-solid transparent;
border-width: 6px 6px 0 6px; /* Arrow shape */
border-color: #555 transparent transparent transparent;
top: 100%; /* Position the arrow below the tooltip */
left: 50%; /* Center the arrow horizontally */
margin-left: -6px; /* Adjust the position of the arrow tip */
}

.tooltip:hover .tooltiptext {
visibility: visible;
}
</style>
</head>
<body>
<h1 class="tooltip">Hover over me to see the tooltip!</h1>
<div class="tooltiptext">This is a tooltip example.</div>
</body>
</html>

In this example, we've created a simple HTML structure with an ` element that has been given the CSS class tooltip. We've also added a separate for the tooltip content, which is hidden by default using the visibility: hidden` property.

The .tooltiptext::after pseudo-element creates the arrow shape for our tooltip. By adjusting the border properties, we can create an arrow that points downwards and slightly to the left of the tooltip box.

When you hover over the ` element, the tooltip becomes visible due to the CSS rule .tooltip:hover .tooltiptext { visibility: visible; }`.

Customizing Your Tooltips

You can customize your tooltips by modifying the CSS properties for the .tooltip, .tooltiptext, and .tooltiptext::after pseudo-elements. For example, you can change the background color, font, padding, or arrow shape to better suit your design needs.

.tooltip {
border-color: red; /* Change the border color */
}

.tooltip .tooltiptext {
width: 200px; /* Increase tooltip width */
background-color: #3e8e41; /* Change tooltip background color */
color: white; /* Change tooltip text color */
}

Worked Example

Let's create a more complex tooltip example that includes multiple tooltips and custom styles.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<style>
.tooltip {
position: relative;
display: inline-block;
border-bottom: dotted 1px black; /* Add a bottom border to trigger the tooltip */
}

.tooltip .tooltiptext {
visibility: hidden;
width: 200px;
background-color: #3e8e41;
color: white;
text-align: center;
padding: 10px;
position: absolute;
z-index: 1;
}

.tooltip .tooltiptext::after {
content: "";
position: absolute;
border-solid transparent;
border-width: 8px 8px 0 8px; /* Arrow shape */
border-color: #3e8e41 transparent transparent transparent;
top: 100%; /* Position the arrow below the tooltip */
left: 50%; /* Center the arrow horizontally */
margin-left: -8px; /* Adjust the position of the arrow tip */
}

.tooltip:hover .tooltiptext {
visibility: visible;
}

.container {
display: flex;
justify-content: space-around;
width: 100%;
}

.tooltiptitle {
cursor: pointer;
}
</style>
</head>
<body>
<div class="container">
<div class="tooltip tooltiptitle" title="Tooltip 1">Tooltip 1</div>
<div class="tooltip tooltiptitle" title="Tooltip 2">Tooltip 2</div>
<div class="tooltip tooltiptitle" title="Tooltip 3">Tooltip 3</div>
<div class="tooltiptext" id="tooltip1">This is Tooltip 1.</div>
<div class="tooltiptext" id="tooltip2">This is Tooltip 2.</div>
<div class="tooltiptext" id="tooltip3">This is Tooltip 3.</div>
</div>
</body>
</html>

In this example, we've created a container ` that holds multiple tooltips. Each tooltip has been given the CSS class tooltip, and we've added a custom CSS class tooltiptitle to make them clickable (although hovering is more common for tooltips). We've also separated the tooltip content into individual ` elements with unique IDs, which allows us to target each tooltip individually using CSS.

Common Mistakes

  1. Not setting a width or max-width for the tooltip: This can cause the tooltip to overflow its container if the content is too long.
  2. Incorrectly positioning the tooltip arrow: Adjust the border-width and margin-left properties to ensure the arrow points in the correct direction and is properly aligned with the tooltip box.
  3. Not making the tooltip visible on hover: Make sure you have a rule like .tooltip:hover .tooltiptext { visibility: visible; } in your CSS.
  4. Using an outdated or incorrect pseudo-element for the arrow: Some older browsers may not support the ::after pseudo-element, so consider using the ::before pseudo-element as a fallback.
  5. Forgetting to close the tooltip on mouseout: You can add a CSS rule like .tooltip:hover .tooltiptext { visibility: visible; } and another one like .tooltip:not(:hover) .tooltiptext { visibility: hidden; } to ensure the tooltip hides when the user stops hovering over it.

Practice Questions

  1. Create a tooltip that displays a message when the user hovers over an image.
  2. Modify the tooltip example to include a custom arrow shape using an SVG icon.
  3. Add a delay to the tooltip's appearance and disappearance using CSS transitions or animations.
  4. Style the tooltip so that it appears as a speech bubble, with a curved arrow pointing towards the element being hovered over.
  5. Create a tooltip that changes its content based on the element being hovered over (e.g., different tooltips for different HTML elements).

FAQ

  1. Why should I use CSS tooltips instead of JavaScript ones?
  • CSS tooltips are simpler, faster, and require less code than their JavaScript counterparts. They also provide better accessibility out-of-the-box because they can be styled using ARIA roles for screen readers.
  1. Can I use CSS to create tooltips that appear on click instead of hover?
  • Yes, you can achieve this by adding a click event listener to the element and toggling the visibility of the tooltip accordingly. You can also use JavaScript to make this process easier.
  1. How do I prevent tooltips from overlapping other elements on the page?
  • You can add CSS rules to position the tooltip relative to its parent container, or you can use JavaScript to adjust the tooltip's position dynamically based on the layout of the page.
  1. Can I create tooltips that follow the user's cursor as they move it around the page?
  • Yes, this can be achieved using JavaScript by updating the tooltip's position based on the current mouse coordinates. This technique is known as "follow-cursor" or "tracking tooltips."
  1. How do I make my tooltips accessible for screen reader users?
  • You can use ARIA roles and properties to make your tooltips more accessible. For example, you can add aria-describedby to the element being hovered over, which points to the ID of the tooltip's content. This allows screen readers to announce the tooltip when the user hovers over the associated element.
CSS Tooltips (Web Development) | Web Development | XQA Learn