Tooltip

Material Design 3 tooltip component for displaying helpful text on hover

Tooltip

Tooltips display informative text when users hover over, focus on, or tap an element. They provide brief, helpful information about an element.

Basic Usage

Wrap the trigger element with .tooltip and add .tooltip-content for the tooltip text. The tooltip appears automatically on hover.

Basic Tooltip

This is a tooltip

Positions

Top (Default)

Top Position

Tooltip on top

Bottom

Bottom Position

Tooltip on bottom

Left

Left Position

Tooltip on left

Right Position

Tooltip on right

All Positions

All Positions

Top tooltip
Bottom tooltip
Left tooltip
Right tooltip

Color Variants

Primary

Primary Tooltip

Primary colored tooltip

Secondary

Secondary Tooltip

Secondary colored tooltip

Error

Error Tooltip

Error colored tooltip

Light

Light Tooltip

Light themed tooltip

Multi-line Tooltip

For longer text that needs to wrap.

Multi-line Tooltip

This is a longer tooltip message that wraps to multiple lines for better readability.

Rich Tooltip

For tooltips with structured content like titles and descriptions.

Rich Tooltip

Keyboard Shortcut
Press Ctrl+S to save your work

Delay Variants

Add delay before showing the tooltip.

Delayed Tooltips

Appears after 300ms
Appears after 500ms

Interactive Tooltip

Allow users to interact with the tooltip content (e.g., click links).

Interactive Tooltip

Learn More
Visit our documentation for details.

Always Open (For Demo)

Use .tooltip-open class to force the tooltip to always show.

Always Open - Top

Top tooltip always visible

Always Open - Bottom

Bottom tooltip always visible

Always Open - Left

Left tooltip always visible

Always Open - Right

Right tooltip always visible

With Different Elements

Tooltips work with any element, not just buttons.

Tooltip on Various Elements

Hover this text Helpful information
ℹ️ Info icon tooltip
Enter your username

JavaScript Integration

Data Attribute Approach

// Automatically create tooltips from data-tooltip attribute
document.querySelectorAll('[data-tooltip]').forEach(element => {
  const text = element.getAttribute('data-tooltip');
  const position = element.getAttribute('data-tooltip-position') || 'top';

  // Wrap element with tooltip container
  const wrapper = document.createElement('div');
  wrapper.className = `tooltip tooltip-${position}`;
  element.parentNode.insertBefore(wrapper, element);
  wrapper.appendChild(element);

  // Add tooltip content
  const content = document.createElement('span');
  content.className = 'tooltip-content';
  content.textContent = text;
  wrapper.appendChild(content);
});

// Usage in HTML:
// <button data-tooltip="Save your changes" data-tooltip-position="bottom">Save</button>

Programmatic Control

// Show/hide tooltip programmatically
function showTooltip(element) {
  element.closest('.tooltip')?.classList.add('tooltip-open');
}

function hideTooltip(element) {
  element.closest('.tooltip')?.classList.remove('tooltip-open');
}

// Toggle tooltip
function toggleTooltip(element) {
  element.closest('.tooltip')?.classList.toggle('tooltip-open');
}

React Example

import { ReactNode } from 'react';

interface TooltipProps {
  children: ReactNode;
  content: string;
  position?: 'top' | 'bottom' | 'left' | 'right';
  variant?: 'primary' | 'secondary' | 'error' | 'light';
}

export function Tooltip({
  children,
  content,
  position = 'top',
  variant
}: TooltipProps) {
  const classes = [
    'tooltip',
    `tooltip-${position}`,
    variant && `tooltip-${variant}`
  ].filter(Boolean).join(' ');

  return (
    <div className={classes}>
      {children}
      <span className="tooltip-content">{content}</span>
    </div>
  );
}

// Usage
<Tooltip content="Save your work" position="bottom" variant="primary">
  <button className="btn btn-primary">Save</button>
</Tooltip>

Vue Example

<template>
  <div :class="tooltipClasses">
    <slot />
    <span class="tooltip-content">{{ content }}</span>
  </div>
</template>

<script setup>
import { computed } from 'vue';

const props = defineProps({
  content: { type: String, required: true },
  position: { type: String, default: 'top' },
  variant: { type: String, default: null }
});

const tooltipClasses = computed(() => [
  'tooltip',
  `tooltip-${props.position}`,
  props.variant && `tooltip-${props.variant}`
].filter(Boolean));
</script>

<!-- Usage -->
<Tooltip content="Delete this item" position="left" variant="error">
  <button class="btn btn-error">Delete</button>
</Tooltip>

API Reference

Class Names

ClassDescription
.tooltipBase tooltip wrapper (required)
.tooltip-contentTooltip text container (required)
.tooltip-topPosition above element (default)
.tooltip-bottomPosition below element
.tooltip-leftPosition to the left
.tooltip-rightPosition to the right
.tooltip-openForce tooltip to always show
.tooltip-primaryPrimary color variant
.tooltip-secondarySecondary color variant
.tooltip-errorError color variant
.tooltip-lightLight themed tooltip
.tooltip-multilineAllow text wrapping (max-width: 16rem)
.tooltip-richRich content tooltip (max-width: 20rem)
.tooltip-delay300ms delay before showing
.tooltip-delay-long500ms delay before showing
.tooltip-interactiveAllow mouse interaction with tooltip

Rich Tooltip Classes

ClassDescription
.tooltip-rich-titleTitle text in rich tooltip
.tooltip-rich-descriptionDescription text in rich tooltip

Accessibility

  • Tooltips are shown on both hover and focus for keyboard users
  • Use aria-describedby to link the tooltip to the trigger element for screen readers
  • Keep tooltip text concise and informative
  • Don’t put essential information only in tooltips
<div class="tooltip">
  <button aria-describedby="tooltip-1">Help</button>
  <span class="tooltip-content" id="tooltip-1" role="tooltip">
    Click for assistance
  </span>
</div>

Best Practices

  1. Keep it brief - Tooltips should contain short, helpful text
  2. Don’t hide critical info - Essential information should be visible without hover
  3. Consider touch devices - Tooltips require hover, so provide alternatives for mobile
  4. Use appropriate delay - Add delay for tooltips that might trigger accidentally
  5. Position thoughtfully - Ensure tooltips don’t get cut off by viewport edges
  • Popover - For richer, interactive popup content
  • Snackbar - For brief notification messages
  • Dialog - For modal interactions

See Also