On this page10 sections
Lecture 039: Tooltips
| Category | 7: Advanced Styling |
| Lecture | 039 of 057 |
| Estimated video length | 8 min |
| Target | CSS3 · any modern browser |
| Level | Intermediate |
A tooltip is a small hint that appears when you hover over, or focus, something. The
titleattribute gives a plain one for free; CSS builds a styled one with an arrow, placed wherever you like.
Learning Objectives
- Build a tooltip with
position,visibilityand:hover - Place it above, below, left or right
- Add an arrow with a border trick
- Fade it in, and make it keyboard-accessible
1. The Simple Way: title
<abbr title="Cascading Style Sheets">CSS</abbr>It's free and accessible, but you can't style it, and it takes a moment to appear.
2. A Styled Tooltip
Example: hover over, or Tab to, the word:
<style>
.tip { position: relative; border-bottom: 1px dotted #495057; cursor: help; }
.tip .text {
visibility: hidden;
opacity: 0;
position: absolute;
bottom: calc(100% + 8px); /* above the word */
left: 50%;
transform: translateX(-50%);
width: max-content;
max-width: 220px;
padding: 6px 10px;
border-radius: 6px;
background: #212529;
color: white;
font-size: 13px;
transition: opacity 0.2s;
}
.tip:hover .text, .tip:focus .text { visibility: visible; opacity: 1; }
</style>
<p style="margin-top: 50px;">
The <span class="tip" tabindex="0">viewport<span class="text">The visible area of the web page</span></span> changes size on every device.
</p>How it works:
- The wrapper is
position: relative; the text isposition: absolute. bottom: calc(100% + 8px)places it just above;left: 50%+translateX(-50%)centres it.visibility+opacity(rather thandisplay) lets it fade in.tabindex="0"+:focuslets keyboard users see it too.
3. Other Positions
| Position | CSS on .text |
|---|---|
| Above | bottom: calc(100% + 8px); left: 50%; transform: translateX(-50%); |
| Below | top: calc(100% + 8px); left: 50%; transform: translateX(-50%); |
| Right | left: calc(100% + 8px); top: 50%; transform: translateY(-50%); |
| Left | right: calc(100% + 8px); top: 50%; transform: translateY(-50%); |
4. Adding an Arrow
A zero-size box with a thick border, where only one side is coloured, makes a triangle.
Example
<style>
.tip { position: relative; display: inline-block; padding: 8px 14px; background: #e7f5ff; border-radius: 6px; cursor: pointer; }
.tip .text { visibility: hidden; position: absolute; top: calc(100% + 10px); left: 50%; transform: translateX(-50%);
background: #1971c2; color: white; padding: 6px 10px; border-radius: 6px; width: max-content; font-size: 13px; }
.tip .text::after {
content: "";
position: absolute;
bottom: 100%; /* on top of the tooltip */
left: 50%;
margin-left: -6px;
border: 6px solid transparent;
border-bottom-color: #1971c2; /* the visible side points up */
}
.tip:hover .text, .tip:focus-within .text { visibility: visible; }
</style>
<button class="tip" type="button">Hover me<span class="text">Tooltip with an arrow</span></button>
<div style="height: 50px;"></div>5. Tooltips on Touch Screens
There's no hover on phones. Keep tooltips for extra information only, never essential instructions. For tap-to-open help, use the Popover API (HTML Lecture 045).
Common Beginner Mistakes
1. Forgetting position: relative on the wrapper. The tooltip appears somewhere else on the page.
2. Using display: none. It can't be animated. Use visibility with opacity.
3. Hover-only tooltips. Add :focus and tabindex="0" for keyboard users.
4. Essential info in tooltips. Touch users may never see it.
Practice Questions
- What's the simplest tooltip in HTML?
- Which positions do the wrapper and the tooltip text need?
- Why use
visibilityandopacityinstead ofdisplay? - Place a tooltip below its trigger, centred.
- How do you make a CSS triangle for the arrow?
- Why shouldn't tooltips hold essential information?
Practice Answers
- The
titleattribute. - The wrapper
position: relative; the textposition: absolute. - So it can fade in with a transition.
top: calc(100% + 8px); left: 50%; transform: translateX(-50%);- A zero-size element with a thick transparent border, with one side coloured.
- Touch-screen users can't hover, so they may never see it.
Key Takeaways
titlegives a free tooltip; CSS gives styled ones.- Wrapper
relative+ textabsolute+:hover/:focusto show it. visibility+opacityfade; a transparent-border triangle makes the arrow.- Tooltips are for extras, not essentials.