On this page10 sections
  1. Learning Objectives
  2. 1. The Simple Way: title
  3. 2. A Styled Tooltip
  4. 3. Other Positions
  5. 4. Adding an Arrow
  6. 5. Tooltips on Touch Screens
  7. Common Beginner Mistakes
  8. Practice Questions
  9. Practice Answers
  10. Key Takeaways

Lecture 039: Tooltips

Category7: Advanced Styling
Lecture039 of 057
Estimated video length8 min
TargetCSS3 · any modern browser
LevelIntermediate

A tooltip is a small hint that appears when you hover over, or focus, something. The title attribute 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, visibility and :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

html
<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:

html
<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 is position: absolute.
  • bottom: calc(100% + 8px) places it just above; left: 50% + translateX(-50%) centres it.
  • visibility + opacity (rather than display) lets it fade in.
  • tabindex="0" + :focus lets keyboard users see it too.

3. Other Positions

PositionCSS on .text
Abovebottom: calc(100% + 8px); left: 50%; transform: translateX(-50%);
Belowtop: calc(100% + 8px); left: 50%; transform: translateX(-50%);
Rightleft: calc(100% + 8px); top: 50%; transform: translateY(-50%);
Leftright: 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

html
<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

  1. What's the simplest tooltip in HTML?
  2. Which positions do the wrapper and the tooltip text need?
  3. Why use visibility and opacity instead of display?
  4. Place a tooltip below its trigger, centred.
  5. How do you make a CSS triangle for the arrow?
  6. Why shouldn't tooltips hold essential information?

Practice Answers

  1. The title attribute.
  2. The wrapper position: relative; the text position: absolute.
  3. So it can fade in with a transition.
  4. top: calc(100% + 8px); left: 50%; transform: translateX(-50%);
  5. A zero-size element with a thick transparent border, with one side coloured.
  6. Touch-screen users can't hover, so they may never see it.

Key Takeaways

  • title gives a free tooltip; CSS gives styled ones.
  • Wrapper relative + text absolute + :hover/:focus to show it.
  • visibility + opacity fade; a transparent-border triangle makes the arrow.
  • Tooltips are for extras, not essentials.