Skip to main content
Login

Sign in to access your account

Tooltip - National Design System

A tooltip is a small balloon of help text that opens beside a term, a field or a button

Overview

A tooltip holds short, optional help. Its trigger is a help icon from Feedback Icons, a word in a sentence, or a button. The balloon holds a message, with an optional title and a help icon, and an arrow points at the trigger. You write the markup yourself, or put the text in data attributes and the script builds the parts.

Pick another component when:

  • the user must read the text to finish the task: the info text of a field, see Forms
  • the message must stay in view: Alert
  • the user must make a decision: Modal
  • the trigger opens a list of actions: Dropmenu

Markup

Options
Structure
Open on
Position
Status
Preview
<span class="nds-tooltip" data-tooltip-title="Iqama number" data-tooltip-message="Residents enter the 10-digit number on their residence permit."></span>

Behavior

Click Mode

By default a click on the trigger opens the balloon, and a second click closes it. The click opens only the balloon: on a link or a button, the link does not open and the button action does not run.

Hover Mode

data-tooltip-hover opens the balloon when the mouse rests on the trigger, and when the trigger gets focus from the keyboard. The balloon stays open while the mouse moves into it, and closes when the mouse leaves both. A click during the wait cancels the open. On a link or a button, a click or a tap runs the action and closes the balloon. On a text term, a tap opens and closes the balloon, and a mouse click does nothing.

Auto Markup

Put data-tooltip-title, data-tooltip-message or both on .nds-tooltip, and the script builds the parts you did not write. An empty root gets a help icon trigger. A root with text or elements is the trigger itself. The balloon gets a help icon only when it has a title. A part you wrote is always kept.

Text Term

A term is a .nds-tooltip with text inside, and nds-term adds a dotted underline and the help cursor. The script makes the term reachable with Tab. In click mode it also gives the term role="button", and Enter or Space opens and closes the balloon.

Title Attribute

With no data-tooltip-message, the root’s title is the message. Until the script loads, the browser shows title as its own tooltip. Then the script removes title, so the two never show together. An element with no other name keeps the text as its aria-label. The balloon is built on the first open, so a row of icon buttons adds no hidden balloons to the page.

Built-in Features

  • Auto-initialization

    Every .nds-tooltip on the page starts by itself, after the page first shows. Call NDS.Tooltip.init() after you add new ones.

  • Balloon Position

    While open, the balloon moves to <body>, so no container cuts it off. It opens below the trigger, and above it when there is more room there. It never runs past the side of the screen, and the arrow points at the trigger.

  • Dismissal

    A click outside or Escape closes the balloon, wherever focus is, so a balloon opened by hover closes too. While the page scrolls, the balloon stays on its trigger and closes when the trigger leaves view. A browser without CSS anchor positioning closes it on a page scroll instead. Escape on the trigger also returns focus to it. Only one tooltip is open at a time.

  • Accessibility

    The balloon gets role="tooltip" and an id. The trigger's aria-describedby points to it, so a screen reader reads the message when the trigger gets focus.

  • No Layout Shift

    An empty auto root keeps the space of its help icon until the script adds it, so the text after it does not move.

  • Programmatic Control

    Open, close and remove a tooltip from a script, and listen to its open and close events.

Best Practices

  • Put only optional help in a tooltip. Text the user needs to finish the task goes in the label or the info text.
  • Keep the message to one or two sentences. The balloon is at most 240px wide, and the text wraps.
  • Put the trigger right after the term or the label it explains. In a form field it goes in the field’s header, after the label: see the Help option in Forms.
  • Use the auto markup for plain text. Write the parts yourself only for other content in the balloon.
  • Give a help icon trigger an aria-label that names what it explains, such as “About the National ID”. An auto trigger takes the title, or “More info” when there is no title.
  • Add data-tooltip-hover to a link or a button trigger, so its own click still works.
  • In a form with many tooltips, move the text into info text. Many clicks slow down keyboard and touch users.

API

Data Attributes

Attribute Element Effect
data-tooltip-title .nds-tooltip The balloon title. The script builds the balloon from it
data-tooltip-message .nds-tooltip The balloon message. The script builds the balloon from it
data-tooltip-status .nds-tooltip The status of the help icon the script builds, such as info or error. No value gives the help look. It does not change a trigger or balloon you wrote
title .nds-tooltip The message when data-tooltip-message is absent
data-tooltip-hover .nds-tooltip Opens on hover and keyboard focus. The value is the wait in milliseconds, 120 by default
hidden .nds-tooltip-balloon Keeps the balloon closed at load. The script removes it on open
data-state .nds-tooltip The script writes open while the balloon shows. It skips a root that is a link or a button, which has its own open look
data-position-vertical .nds-tooltip, .nds-tooltip-balloon The script writes top when the balloon opens above the trigger. The arrow then points down
data-position .nds-tooltip end or start. The balloon opens beside the trigger, on that side first
data-position-horizontal .nds-tooltip, .nds-tooltip-balloon With data-position, the script writes left or right: the side the balloon opened on. The arrow points back at the trigger

Keyboard

Key Where Effect
Enter, Space trigger Opens or closes the balloon. On a text term, only in click mode
Tab trigger In hover mode, focus opens the balloon and leaving closes it
Escape anywhere Closes the open balloon. On the trigger, also returns focus to it

CSS Custom Properties

Set these on .nds-tooltip-balloon, or on :root for every tooltip. The balloon moves to <body> while open, so a value set on .nds-tooltip does not reach it.

Property Default Controls
--tooltip-max-width 240px Maximum width of the balloon
--tooltip-padding var(--spacing-md) Space inside the balloon
--tooltip-gap var(--spacing-md) Space between the icon and the text, and between the title and the message
--tooltip-arrow-size 10px Size of the arrow

Tokens

Set a token at :root, or on a wrapper to reach every tooltip inside it. Give a token with a Dark mode value a dark override too: see Tokens.

Source: the tooltip group in _sass/tokens/_components.scss.

TokenPreviewValue
--tooltip-background-default
var(--colors-base-white)
Dark mode var(--colors-neutral-800)
--tooltip-text-heading-default
var(--text-display)
Dark mode var(--colors-neutral-50)
--tooltip-text-paragraph-default
var(--text-primary-paragraph)
Dark mode var(--colors-neutral-100)
--tooltip-help-idle
var(--colors-neutral-500)
Dark mode var(--colors-neutral-600)

It also reads these shared tokens. A change to one also changes other components.

Source: the var() reads in _sass/components/_tooltip.scss.

TokenPreviewValue
--border-neutral-secondary
Read by border

var(--colors-neutral-200)
Dark mode var(--colors-neutral-700)

JavaScript

The NDS.Tooltip script loads after the page shows. Until it loads, each call loads it and returns a promise of the result, so await a call whose result you use.

The instance is on the element as el.ndsTooltip. It is there once the component starts: code that runs at page load waits for NDS.init.ready.

Method Effect
NDS.Tooltip.init() Starts every .nds-tooltip on the page that has not started yet
NDS.Tooltip.create(el) Starts one tooltip and returns its instance, or the instance it already has. Returns null when it has no balloon and no data-tooltip-title or data-tooltip-message to build one
NDS.Tooltip.destroy(el) The same as el.ndsTooltip.destroy()
el.ndsTooltip.open(), .close() Opens or closes the balloon
el.ndsTooltip.destroy() Closes the balloon, removes the listeners, and takes back what the script wrote: the title it moved, the trigger and balloon it built, aria-describedby, tabindex and role. A balloon you wrote keeps its own attributes. The tooltip can start again after it
Event Fired on Detail
nds:tooltip:opened .nds-tooltip (bubbles) { tooltip, trigger, balloon, isOpen }
nds:tooltip:closed .nds-tooltip (bubbles) { tooltip, trigger, balloon, isOpen }

A balloon built from title is not in the page before its first open. Read it from the nds:tooltip:opened detail. While open, the balloon is in <body>, so closest('.nds-tooltip') from inside it finds nothing.

var tip = document.querySelector('.nds-tooltip'); tip.addEventListener('nds:tooltip:opened', function (e) { console.log('Opened:', e.detail.balloon.textContent.trim()); }); tip.ndsTooltip.open();

The full API is in the banner of _js/nds-tooltip.js.

Last Modified Date: 19/11/2026 - 10:40 AM
Was this page useful?
60% of users said Yes from 2843 Feedbacks