AI CSS Animations

Definition Tooltip on Underlined Text

A dotted term inside a sentence that opens a dictionary card with the word, its type and a short definition. The dotted line turns solid, drawing across as the card rises.

Also known as: dotted underline tooltip css, glossary term tooltip, abbreviation tooltip.

Pages load from a CDN CDN nounA network of servers that keeps copies of your files close to your visitors. near you.

HTML

<span class="tip-definition-term">
  Pages load from a
  <span class="tip-definition-term-anchor">
    <dfn tabindex="0" aria-describedby="tip-definition-term-tip">CDN</dfn>
    <span class="tip-definition-term-tip" id="tip-definition-term-tip" role="tooltip"><b>CDN <i>noun</i></b>A network of servers that keeps copies of your files close to your visitors.</span>
  </span>
  near you.
</span>

CSS

.tip-definition-term {
  --c: var(--tip-color, #3b82f6);
  font-size: var(--tip-size, 16px);
  display: inline-block;
  margin-top: 5em; /* room for the card in this preview */
  font-family: system-ui, sans-serif;
  line-height: 1.6;
  pointer-events: none; /* only the term opens the card */
}
.tip-definition-term-anchor {
  position: relative;
  display: inline-block;
}
.tip-definition-term dfn {
  position: relative;
  border-bottom: 0.12em dotted color-mix(in srgb, var(--c) 60%, transparent);
  border-radius: 0.15em;
  color: var(--c);
  font-style: normal;
  font-weight: 650;
  cursor: help;
  pointer-events: auto;
  transition: background-color 0.25s ease;
}
/* the solid line that draws over the dotted one */
.tip-definition-term dfn::after {
  content: "";
  position: absolute;
  right: 0;
  bottom: -0.12em;
  left: 0;
  height: 0.12em;
  background: var(--c);
  transform: scaleX(0);
  transform-origin: left;
  transition: transform 0.35s cubic-bezier(0.65, 0, 0.35, 1);
}
.tip-definition-term dfn:focus-visible {
  outline: 2px solid var(--c);
  outline-offset: 2px;
}
.tip-definition-term:hover dfn,
.tip-definition-term:focus-within dfn { background: color-mix(in srgb, var(--c) 12%, transparent); }
.tip-definition-term:hover dfn::after,
.tip-definition-term:focus-within dfn::after { transform: scaleX(1); }
.tip-definition-term-tip {
  position: absolute;
  bottom: calc(100% + 0.7em);
  left: 50%;
  z-index: 10;
  box-sizing: border-box;
  width: 16em;
  padding: 0.7em 0.85em 0.75em;
  border: 1px solid color-mix(in srgb, var(--c) 30%, transparent);
  border-radius: 0.6em;
  background: #fff;
  box-shadow: 0 0.8em 1.6em -0.6em rgb(15 23 42 / 0.35);
  color: #475569;
  font: 400 0.8em/1.45 system-ui, sans-serif;
  text-align: left;
  text-wrap: pretty;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transform-origin: 50% 100%;
  transform: translate(-50%, 0.5em) scale(0.97);
  transition: opacity 0.2s ease 0.05s, transform 0.35s cubic-bezier(0.2, 0.8, 0.2, 1) 0.05s, visibility 0.4s;
}
.tip-definition-term-tip::before {
  content: "";
  position: absolute;
  top: 100%;
  left: 50%;
  box-sizing: border-box;
  width: 0.75em;
  height: 0.75em;
  border: inherit;
  border-top: 0;
  border-left: 0;
  border-radius: 0 0 0.12em;
  background: inherit;
  transform: translate(-50%, -55%) rotate(45deg);
}
.tip-definition-term-tip b {
  display: block;
  margin-bottom: 0.15em;
  color: #0f172a;
  font-size: 1.1em;
  font-weight: 700;
}
.tip-definition-term-tip i {
  margin-left: 0.2em;
  color: var(--c);
  font-size: 0.85em;
  font-weight: 500;
}
.tip-definition-term:hover .tip-definition-term-tip,
.tip-definition-term:focus-within .tip-definition-term-tip {
  opacity: 1;
  visibility: visible;
  transform: translate(-50%, 0) scale(1);
}

@media (prefers-reduced-motion: reduce) {
  .tip-definition-term, .tip-definition-term *, .tip-definition-term::before, .tip-definition-term::after, .tip-definition-term *::before, .tip-definition-term *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
}

Customizing this snippet

  • Color: set --tip-color on the element or a parent. Currently previewing #3b82f6.
  • Base size: set --tip-size. Every other dimension is in em, so the whole thing scales together. Currently previewing 16px.
  • Speed: change the duration on the transition or animation shorthand. Staggered delays are relative, so they keep their rhythm.
  • Reduced motion: the rule at the end of the CSS turns the motion off for people who asked their OS for less of it. Keep it.

Similar tooltips