A tooltip is a short piece of supporting text that appears near a control or link. This exercise shows how HTML and CSS can reveal that text on pointer hover and keyboard focus. It is a basic styling demonstration, not a complete production tooltip component.
1. Add the HTML
Place the following markup in the body of a practice HTML page. Replace the example /courses/ destination with a real page on your own site.
<span class="tip">
<a href="/courses/"
aria-describedby="course-tip">
Browse courses
</a>
<span id="course-tip"
class="tip-text"
role="tooltip">
Explore course subjects.
</span>
</span>
The outer tip wrapper keeps the link and its description together. The link remains a normal navigation link. aria-describedby associates it with the description’s unique ID; use a different ID for each tooltip on a page.
2. Add the CSS to your stylesheet
.tip {
position: relative;
display: inline-block;
}
.tip-text {
display: none;
position: absolute;
top: 100%;
left: 0;
z-index: 1;
width: 12rem;
max-width: 80vw;
box-sizing: border-box;
padding: 0.5rem;
color: #111;
background: #fff5cc;
border: 1px solid #665500;
font-weight: normal;
}
.tip:hover .tip-text,
.tip:focus-within .tip-text {
display: block;
}
position: relative makes the wrapper the positioning reference for the tooltip. The description sits immediately below it at top: 100%. Keeping the box adjacent to its trigger lets a pointer move onto the tooltip without crossing a gap that would hide it.
The two selectors reveal the same text when the wrapper is hovered or a descendant has keyboard focus. Write :hover without a space after the colon. Keep the browser’s link underline and focus indicator so visitors can identify and use the link.
The z-index affects stacking within the relevant stacking context; it does not guarantee that a tooltip will appear above everything on a page. An ancestor’s clipping or stacking rules can still affect it.
3. Test the basic behaviour
- Move the pointer onto the link. The description should appear.
- Move directly onto the description. It should remain visible while you are over the wrapper or tooltip.
- Use Tab to focus the link. The description should appear without a mouse.
- Move focus and the pointer away. The description should disappear.
- Test near the right edge of a narrow viewport. The width limit alone does not reposition a tooltip; adjust your design if it extends beyond the screen.
What this CSS example does not solve
CSS hover and focus rules alone do not provide Escape-key dismissal. For a production tooltip that obscures other content, add appropriate dismissal behaviour, usually with JavaScript, and ensure it stays dismissed while the current hover or focus remains. Also test pointer movement, zoom, screen-reader output and placement near viewport edges.
The W3C guidance on content shown on hover or focus explains the requirements for dismissible, hoverable and persistent content. MDN’s tooltip reference describes the expected semantics and keyboard behaviour. Adding an ARIA role by itself does not implement those behaviours.
Keep essential instructions visible in the page. Touch users cannot rely on mouse hover, and a tooltip should not contain links or buttons that need their own focus. For longer or interactive help, use an explicit help control or disclosure instead of extending this small tooltip pattern.
Build your HTML and CSS skills
Explore Academy Class HTML and CSS training to practise page structure, selectors and layout. Start with small examples like this, then test their behaviour with different input methods and screen sizes.