Designing accessible tooltips
The user wants an advanced CSS-only tooltip system with accessible ARIA attributes. I need to provide HTML and CSS code and possibly explain how it works. Key points include using role="tooltip" and managing focus and hover triggers, keeping keyboard accessibility in mind. I'm considering advanced features like positioning, multi-line support, and theming while ensuring tooltips comply with ARIA best practices. Importantly, the content must be present in the DOM and logically associated using aria-describedby. I’ll also need to think about visually hiding elements effectively.Ensuring tooltip accessibility
I need to make sure that tooltip content is accessible for screen readers and is read when the trigger is focused. Using aria-describedby can help with this, but I have to be cautious about visually hiding elements. Options like 'display:none' or 'visibility:hidden' won't be read by screen readers. Many implementations hide tooltip text off-screen with CSS, which allows it to remain accessible. The user asked for an advanced CSS-only solution with ARIA attributes, and it seems a static association with a hidden tooltip that appears on hover would be acceptable.Crafting a CSS-only tooltip system
I want to create a CSS-only advanced tooltip system where trigger states are managed through :hover and :focus-within. I could use data attributes to define the position, like [data-tooltip-pos="top"], allowing tooltips to appear in four different positions: top, bottom, left, and right.
The HTML structure would look like this: a button with a tooltip span as its sibling, ensuring that the semantics are correct. The tooltip span would be tagged with role="tooltip" for accessibility, while we rely solely on CSS to style and position the tooltip without JavaScript.Creating a tooltip wrapper
I’m designing a tooltip system that uses a structure without a parent container for the tooltip. It would look like this: a div called "tooltip-wrapper" containing a button as the trigger and a span for the tooltip itself, which has the aria-describedby attribute for accessibility.
In my CSS, I’ll position the tooltip absolutely, setting it to be hidden by default. I'll use :focus-within to show the tooltip when the trigger is focused, ensuring keyboard accessibility. However, I realized that using visibility:hidden will hide it from screen readers, so I need to find a way to visually hide the tooltip instead, like moving it off-screen.Designing a dynamic tooltip system
I want the tooltip to fade in and out, so I’ll adjust the opacity to 0 and set pointer-events to none instead of using visibility:hidden or display:none. This keeps it accessible for screen readers while still visible. As the tooltip floats absolutely, it won’t affect layout, and using CSS transitions, I can toggle opacity and transform easily. I also need to ensure the tooltip appears on hover or focus.
I’m considering reusable classes for tooltip positioning and theming using CSS variables. The HTML structure will include aria-describedby attributes for accessibility. Following ARIA best practices, I’ll ensure the tooltip content remains accessible in the DOM. I’ll define the root CSS variables and layout the tooltip component accordingly. Each position will have unique transform properties for smooth visibility transitions!Crafting tooltip design
I'm focusing on defining the CSS for the tooltip, especially the arrow using pseudo-elements. I’ll use calculated values for positioning to ensure it works across browsers. For example, I’ll adjust the arrow's position for each tooltip variant, like top or right, by manipulating border widths and colors.
The HTML structure includes aria-describedby for accessibility, and I'll ensure elements are focusable with tabindex="0" as needed. I’m also considering advanced features for accessibility, like high contrast support.
I'll draft a complete example that includes multiple tooltips with both the necessary HTML and CSS while keeping explanations concise. I’ll also clarify that I’m avoiding the use of the aria-hidden attribute, as we want everything accessible. Lastly, I must remember the media query for reduced motion preferences to ensure the design is user-friendly! Below is a copy‑pasteable, CSS‑only tooltip system that:
- Uses correct ARIA relationships (
aria-describedby + role="tooltip").
- Works for mouse, keyboard, and touch (via
:hover and :focus-within).
- Supports top / right / bottom / left positioning and a themed arrow.
- Avoids
display:none / visibility:hidden so screen readers can always read the description text.
1. HTML structure
Use a wrapper around a focusable target and a tooltip element:
<!-- Example: bottom tooltip (default) -->
<span class="tooltip-wrapper">
<button type="button"
class="tooltip-target"
aria-describedby="tip-save">
Save
</button>
<span id="tip-save"
class="tooltip"
role="tooltip">
Save your changes to the server.
</span>
</span>
<!-- Example: top tooltip -->
<span class="tooltip-wrapper" data-tooltip-pos="top">
<button type="button"
class="tooltip-target"
aria-describedby="tip-delete">
Delete
</button>
<span id="tip-delete"
class="tooltip"
role="tooltip">
Permanently deletes this item.
</span>
</span>
<!-- Example: right tooltip on an icon-only control -->
<span class="tooltip-wrapper" data-tooltip-pos="right">
<button type="button"
class="tooltip-target"
aria-describedby="tip-settings">
⚙
</button>
<span id="tip-settings"
class="tooltip"
role="tooltip">
Open settings.
</span>
</span>
<!-- Example: make a non-focusable thing focusable with tabindex -->
<span class="tooltip-wrapper" data-tooltip-pos="left">
<span class="tooltip-target"
tabindex="0"
aria-describedby="tip-info">
ℹ Info
</span>
<span id="tip-info"
class="tooltip"
role="tooltip">
Additional information about this section.
</span>
</span>
Key points:
- The trigger/target is focusable (
button, a, input, or any element with tabindex="0").
- The target has
aria-describedby="tooltip-id".
- The tooltip has
id, class="tooltip", and role="tooltip".
- Wrapper (
.tooltip-wrapper) can take data-tooltip-pos="top|right|bottom|left" (bottom is default if omitted).
2. CSS: advanced, reusable tooltip system
/* ========== Design tokens (customizable) ========== */
:root {
--tooltip-bg: #111827; /* Background color */
--tooltip-color: #f9fafb; /* Text color */
--tooltip-border-radius: 0.375rem;
--tooltip-padding: 0.35rem 0.55rem;
--tooltip-font-size: 0.75rem; /* ~12px */
--tooltip-line-height: 1.4;
--tooltip-shadow: 0 8px 16px rgba(15, 23, 42, 0.35);
--tooltip-max-width: 16rem;
--tooltip-z: 1000;
--tooltip-offset: 0.4rem; /* Gap between trigger & tooltip */
--tooltip-arrow-size: 0.4rem; /* Size of arrow triangle */
--tooltip-transition-duration: 150ms;
--tooltip-transition-timing: ease-out;
}
/* ========== Wrapper ========== */
.tooltip-wrapper {
position: relative;
display: inline-flex; /* Works nicely around inline elements */
align-items: center;
}
/* ========== Target (trigger) ========== */
.tooltip-target {
/* Style your triggers as needed. Nothing strictly required here. */
cursor: pointer;
}
/* Make non-button triggers focus-visible friendly */
.tooltip-target:focus-visible {
outline: 2px solid #2563eb;
outline-offset: 2px;
}
/* ========== Tooltip base ========== */
.tooltip {
position: absolute;
z-index: var(--tooltip-z);
max-width: var(--tooltip-max-width);
padding: var(--tooltip-padding);
border-radius: var(--tooltip-border-radius);
background-color: var(--tooltip-bg);
color: var(--tooltip-color);
font-size: var(--tooltip-font-size);
line-height: var(--tooltip-line-height);
box-shadow: var(--tooltip-shadow);
white-space: normal; /* Allow multi-line */
pointer-events: none; /* Don't block clicks */
opacity: 0;
transition:
opacity var(--tooltip-transition-duration) var(--tooltip-transition-timing),
transform var(--tooltip-transition-duration) var(--tooltip-transition-timing);
/* Default placement: bottom */
top: 100%;
left: 50%;
transform: translate(-50%, calc(var(--tooltip-offset) + 0.25rem));
}
/* Tooltip arrow (default: arrow pointing up, tooltip below trigger) */
.tooltip::after {
content: "";
position: absolute;
width: 0;
height: 0;
left: 50%;
transform: translateX(-50%);
border-style: solid;
/* Triangle pointing up (for bottom placement) */
top: calc(-1 * var(--tooltip-arrow-size));
border-width: 0
var(--tooltip-arrow-size)
var(--tooltip-arrow-size)
var(--tooltip-arrow-size);
border-color: transparent
transparent
var(--tooltip-bg)
transparent;
}
/* ========== Show on hover and keyboard focus ========== */
.tooltip-wrapper:hover .tooltip,
.tooltip-wrapper:focus-within .tooltip {
opacity: 1;
}
/* Animate slightly toward the trigger when visible */
.tooltip-wrapper:hover .tooltip,
.tooltip-wrapper:focus-within .tooltip {
transform: translate(-50%, var(--tooltip-offset));
}
/* ========== Placement: TOP ========== */
.tooltip-wrapper[data-tooltip-pos="top"] .tooltip {
top: auto;
bottom: 100%;
left: 50%;
transform: translate(-50%, calc(-1 * var(--tooltip-offset) - 0.25rem));
}
.tooltip-wrapper[data-tooltip-pos="top"]:hover .tooltip,
.tooltip-wrapper[data-tooltip-pos="top"]:focus-within .tooltip {
transform: translate(-50%, calc(-1 * var(--tooltip-offset)));
}
/* Arrow for top placement (triangle pointing down) */
.tooltip-wrapper[data-tooltip-pos="top"] .tooltip::after {
top: auto;
bottom: calc(-1 * var(--tooltip-arrow-size));
border-width: var(--tooltip-arrow-size)
var(--tooltip-arrow-size)
0
var(--tooltip-arrow-size);
border-color: var(--tooltip-bg)
transparent
transparent
transparent;
}
/* ========== Placement: RIGHT ========== */
.tooltip-wrapper[data-tooltip-pos="right"] .tooltip {
top: 50%;
left: 100%;
transform: translate(calc(var(--tooltip-offset) + 0.25rem), -50%);
}
.tooltip-wrapper[data-tooltip-pos="right"]:hover .tooltip,
.tooltip-wrapper[data-tooltip-pos="right"]:focus-within .tooltip {
transform: translate(var(--tooltip-offset), -50%);
}
/* Arrow for right placement (triangle pointing left) */
.tooltip-wrapper[data-tooltip-pos="right"] .tooltip::after {
top: 50%;
left: calc(-1 * var(--tooltip-arrow-size));
transform: translateY(-50%);
border-width: var(--tooltip-arrow-size)
var(--tooltip-arrow-size)
var(--tooltip-arrow-size)
0;
border-color: transparent
var(--tooltip-bg)
transparent
transparent;
}
/* ========== Placement: LEFT ========== */
.tooltip-wrapper[data-tooltip-pos="left"] .tooltip {
top: 50%;
right: 100%;
left: auto;
transform: translate(calc(-1 * var(--tooltip-offset) - 0.25rem), -50%);
}
.tooltip-wrapper[data-tooltip-pos="left"]:hover .tooltip,
.tooltip-wrapper[data-tooltip-pos="left"]:focus-within .tooltip {
transform: translate(calc(-1 * var(--tooltip-offset)), -50%);
}
/* Arrow for left placement (triangle pointing right) */
.tooltip-wrapper[data-tooltip-pos="left"] .tooltip::after {
top: 50%;
left: auto;
right: calc(-1 * var(--tooltip-arrow-size));
transform: translateY(-50%);
border-width: var(--tooltip-arrow-size)
0
var(--tooltip-arrow-size)
var(--tooltip-arrow-size);
border-color: transparent
transparent
transparent
var(--tooltip-bg);
}
/* ========== Reduced motion support ========== */
@media (prefers-reduced-motion: reduce) {
.tooltip {
transition: none;
}
}
3. Accessibility notes
- Association:
aria-describedby="tooltip-id" on the trigger + id="tooltip-id" and role="tooltip" on the tooltip.
- Keyboard users:
- Tooltips appear on
:focus-within (tab to the trigger).
- Use naturally focusable elements or add
tabindex="0" to non-focusable ones.
- Screen readers:
- Tooltip content remains in the accessibility tree (we do not use
display:none or visibility:hidden), so it’s read as part of the control’s description.
- No JS needed:
- The only behaviors are CSS
:hover and :focus-within.
If you want, I can extend this with themes (light/dark classes), responsive adjustments, or a variant where tooltips are always visible on small screens.