cheat/dev
← Todas as receitas

Como fazer um tooltip só com CSS

Dica que aparece no hover e no foco do teclado, lendo o texto de um atributo.

Como funciona

  • • content: attr(data-tip) no ::after lê o texto do atributo HTML, sem JavaScript.
  • • :focus-visible faz a dica aparecer também para quem navega por teclado.
  • • pointer-events: none impede que o tooltip invisível atrapalhe cliques.

Código completo

<p>Passe o mouse ou use Tab no <button class="tip" data-tip="Texto da dica aparece aqui" type="button">botão com dica</button>.</p>
HTML
*, *::before, *::after { box-sizing: border-box; }
body { margin: 0; font-family: system-ui, sans-serif; line-height: 1.6; color: #14171a; }
body { padding: 80px 24px; }
.tip { position: relative; padding: 8px 14px; border: 1px solid #d1d5db; border-radius: 8px; background: #fff; font: inherit; cursor: help; }
.tip::after {
  content: attr(data-tip);
  position: absolute; left: 50%; bottom: calc(100% + 10px);
  translate: -50% 4px;
  padding: 6px 10px; border-radius: 8px;
  background: #14171a; color: #fff; font-size: 13px; white-space: nowrap;
  opacity: 0; pointer-events: none;
  transition: opacity .2s, translate .2s;
}
.tip:hover::after, .tip:focus-visible::after { opacity: 1; translate: -50% 0; }
CSS