Page Widget How to - Create Pure CSS tooltip








Question

We would like to know how to create Pure CSS tooltip.

Answer


<!DOCTYPE html>
<html>
<head>
<style type='text/css'>
body {<!--from  ww  w .ja  v a  2 s.  co  m-->
  font-family: sans-serif;
  margin: 3em;
}

[data-tooltip] {
  position: relative;
}

[data-tooltip]:hover::before {
  content: attr(data-tooltip);
  color: #fff;
  position: absolute;
  display: block;
  top: -2em;
  left: 0;
  white-space: nowrap;
  background: rgba(0, 0, 0, 0.5);
  padding: 0.2em 0.5em;
  border-radius: 2px;
}
</style>
</head>
<body>
  <p>
    <span data-tooltip="tooltip!">data</span>
  </p>
</body>
</html>

The code above is rendered as follows: