
Light and Dark Mode in CSS with prefers-color-scheme
03/08/2023
Fluid Typography with CSS clamp()
04/05/2024
A tooltip is a short description that appears next to an element, often when a pointer hovers over it or it receives keyboard focus. It supplements the element’s label with a little context.
1. CSS tooltips
Use a tooltip for brief supplementary information about a link, button, or similar control. Put essential instructions in visible text. The following example illustrates how the description appears.
Show a tooltip
2. Browser-provided tooltips with <abbr>
Use <abbr title="…"> to mark an abbreviation and provide its expansion. Some browsers expose the title as a tooltip on hover. The browser controls its appearance, and the tooltip may be unavailable to keyboard or touch users. Spell out important terms in visible text.
Try the default browser tooltip in CodePen
<abbr title="HyperText Markup Language">HTML</abbr>
3. Build a custom tooltip with CSS
Place the description in a container and hide it initially. CSS can show it in response to hover or keyboard focus by changing visibility and opacity.
Position the tooltip inside a wrapper with position: relative. Use position: absolute on the tooltip to place it on the desired side of the trigger.
Use visibility: hidden and opacity: 0 for the hidden state. Show it when the wrapper is hovered or contains keyboard focus.
You can adjust the colors, shape, spacing, and transition, while keeping the label and text readable.
Follow these steps to build the example:
- Create a wrapper, a focusable trigger, and a description. Connect the trigger to the description with
aria-describedby.
<div class="tooltip-container">
<button type="button" aria-describedby="tooltip-example">Example trigger</button>
<span class="tooltip-text" id="tooltip-example" role="tooltip">This is a tooltip.</span>
</div>
- Style the wrapper and tooltip in CSS. Position the description close to the trigger.
.tooltip-container {
position: relative;
display: inline-block;
}
.tooltip-text {
visibility: hidden;
width: 120px;
background-color: #000;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px;
position: absolute;
z-index: 1;
bottom: 125%; /* Position the tooltip above the trigger */
left: 50%;
transform: translateX(-50%);
opacity: 0;
transition: opacity 0.3s;
}
- Reveal the description with
:hoverand:focus-withinso readers can reach it with either a pointer or a keyboard.
.tooltip-container:hover .tooltip-text,
.tooltip-container:focus-within .tooltip-text {
visibility: visible;
opacity: 1;
}
This CSS example illustrates appearance and placement. A production tooltip may need JavaScript to support Escape dismissal and other interaction requirements. For longer or essential text, consider visible text or a disclosure control.
4. Tooltip positions
Use absolute positioning to place the description above, to the right, below, or to the left of the trigger.
- Start with the same wrapper, focusable trigger, and description.
- Apply one of these rules according to the desired position:
- Above the trigger:
.tooltip-text {
bottom: 100%; /* Adjust the offset as needed */
left: 50%;
transform: translateX(-50%);
}
- To the right of the trigger:
.tooltip-text {
top: 50%;
left: 100%; /* Adjust the offset as needed */
transform: translateY(-50%);
}
- Below the trigger:
.tooltip-text {
top: 100%; /* Adjust the offset as needed */
left: 50%;
transform: translateX(-50%);
}
- To the left of the trigger:
.tooltip-text {
top: 50%;
right: 100%; /* Adjust the offset as needed */
transform: translateY(-50%);
}
- Adjust the width, background, text color, radius, and padding to fit your site. Check near viewport edges and at large zoom levels.
Test the position with pointer and keyboard input. Keep the tooltip from covering content the reader needs to act on.