TExcelTAD
Home › CSS › CSS Tooltips

CSS Tooltips

A tooltip is a small label that appears when the user hovers over (or focuses) an element. It explains an icon, shows a full name, or gives a hint. You can build one with pure CSS: a hidden, absolutely positioned box that becomes visible on :hover.

The basic recipe

  1. Put the trigger in a wrapper with position: relative.
  2. Put the tooltip text inside it with position: absolute, hidden by default.
  3. Show the tooltip with :hover (and :focus-within for keyboard users).
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 80px 40px; }
  .tip { position: relative; display: inline-block; border-bottom: 2px dotted #3949ab; cursor: help; }
  .tip .text {
    visibility: hidden;
    position: absolute;
    bottom: 130%;
    left: 50%;
    transform: translateX(-50%);
    width: 160px;
    padding: 8px;
    background: #212121;
    color: white;
    text-align: center;
    border-radius: 6px;
    z-index: 1;
  }
  .tip:hover .text { visibility: visible; }
</style>
</head>
<body>
  Hover over <span class="tip">this phrase<span class="text">Hello! I'm a tooltip.</span></span> to see a hint.
</body>
</html>

bottom: 130% places the tooltip above the trigger with a little gap, and left: 50% + translateX(-50%) centres it horizontally.

Positioning options

Position Key declarations
Top bottom: 100%; left: 50%; transform: translateX(-50%);
Bottom top: 100%; left: 50%; transform: translateX(-50%);
Left right: 100%; top: 50%; transform: translateY(-50%);
Right left: 100%; top: 50%; transform: translateY(-50%);
html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; display: grid; place-items: center; height: 240px; margin: 0; }
  .grid { display: grid; grid-template-columns: repeat(2, 130px); gap: 60px 120px; }
  .tip { position: relative; padding: 10px; background: #e8eaf6; border-radius: 8px; text-align: center; }
  .tip span { position: absolute; white-space: nowrap; background: #3949ab; color: white; padding: 5px 9px; border-radius: 5px; font-size: 13px; opacity: 0; transition: opacity .2s; pointer-events: none; }
  .tip:hover span { opacity: 1; }
  .top span    { bottom: calc(100% + 6px); left: 50%; transform: translateX(-50%); }
  .bottom span { top: calc(100% + 6px);    left: 50%; transform: translateX(-50%); }
  .left span   { right: calc(100% + 6px);  top: 50%;  transform: translateY(-50%); }
  .right span  { left: calc(100% + 6px);   top: 50%;  transform: translateY(-50%); }
</style>
</head>
<body>
  <div class="grid">
    <div class="tip top">Top<span>I'm above</span></div>
    <div class="tip right">Right<span>I'm right</span></div>
    <div class="tip left">Left<span>I'm left</span></div>
    <div class="tip bottom">Bottom<span>I'm below</span></div>
  </div>
</body>
</html>

Adding an arrow

An arrow is a zero-size box with a thick border where only one side is coloured. Attach it with ::after so no extra HTML is needed.

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 90px 50px; }
  .tip { position: relative; display: inline-block; padding: 10px 16px; background: #00897b; color: white; border-radius: 8px; }
  .tip .text {
    position: absolute; bottom: calc(100% + 12px); left: 50%;
    transform: translateX(-50%) translateY(6px);
    background: #263238; color: white; padding: 8px 12px; border-radius: 6px; white-space: nowrap;
    opacity: 0; transition: opacity .2s, transform .2s;
  }
  .tip .text::after {
    content: ""; position: absolute; top: 100%; left: 50%; margin-left: -7px;
    border: 7px solid transparent; border-top-color: #263238;
  }
  .tip:hover .text, .tip:focus .text { opacity: 1; transform: translateX(-50%) translateY(0); }
</style>
</head>
<body>
  <span class="tip" tabindex="0">Hover or Tab to me<span class="text">Tooltip with an arrow ▾</span></span>
</body>
</html>

Tip: Fading with opacity + transition looks smoother than toggling visibility or display, which can't be animated. Add pointer-events: none so the invisible tooltip doesn't block clicks.

Tooltips from a data attribute

Instead of nested markup, store the text in an attribute and print it with content: attr(data-tip).

html · try it yourself
<!DOCTYPE html>
<html>
<head>
<style>
  body { font-family: system-ui, sans-serif; padding: 80px 30px; display: flex; gap: 24px; }
  [data-tip] { position: relative; font-size: 28px; cursor: pointer; }
  [data-tip]::after {
    content: attr(data-tip);
    position: absolute; bottom: 120%; left: 50%; transform: translateX(-50%);
    font-size: 13px; white-space: nowrap; background: #6a1b9a; color: white; padding: 4px 8px; border-radius: 4px;
    opacity: 0; transition: opacity .15s;
  }
  [data-tip]:hover::after { opacity: 1; }
</style>
</head>
<body>
  <span data-tip="Save">💾</span>
  <span data-tip="Delete">🗑️</span>
  <span data-tip="Share">📤</span>
</body>
</html>

Accessibility notes

  • Hover doesn't exist on touch screens, so never hide essential information in a tooltip.
  • Make triggers focusable (a <button>, a link, or tabindex="0") and show the tooltip on :focus too.
  • Screen readers won't read pseudo-element text reliably; for icon buttons also add aria-label.

Test yourself

Why does the tooltip's trigger usually need position: relative?

  1. To make it clickable
  2. So the absolutely positioned tooltip is placed relative to it
  3. To enable :hover
  4. To hide overflow

How is a CSS tooltip arrow usually drawn?

  1. With an image
  2. With a zero-size pseudo-element whose border has one coloured side
  3. With text-shadow
  4. With outline

Which approach lets a tooltip fade in smoothly?

  1. display: none → block
  2. opacity: 0 → 1 with a transition
  3. visibility only
  4. z-index changes