Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@w3collective
Last active April 13, 2021 00:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save w3collective/5b665643f670fe8095c507f07a19226d to your computer and use it in GitHub Desktop.
Save w3collective/5b665643f670fe8095c507f07a19226d to your computer and use it in GitHub Desktop.
HTML tooltip on hover using CSS
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>CSS Tooltip</title>
<style>
body {
padding: 10%;
}
[data-tooltip] {
position: relative;
cursor: pointer;
background: black;
color: white;
font-size: 12px;
border-radius: 1em;
padding: 0 0.5em;
}
[data-tooltip]:before {
content: attr(data-tooltip);
position: absolute;
opacity: 0;
width: 150px;
transform:translateX(-50%);
bottom: 25px;
padding: 0.5em;
background-color: black;
border-radius: 0.25em;
color: white;
text-align: center;
transition:0.2s;
}
[data-tooltip]:after {
content: "";
position: absolute;
opacity: 0;
bottom: 15px;
margin-left: -5px;
border: 5px solid black;
border-color: black transparent transparent transparent;
transition:0.2s;
}
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
opacity: 1;
}
</style>
</head>
<body>
<p>Example CSS Tooltip <span data-tooltip="Tooltips are used to provide information about an element on a web page.">i</span></p>
</body>
</html>
@w3collective
Copy link
Author

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment