24
loading...
This website collects cookies to deliver better user experience
<p>Example CSS Tooltip <span data-tooltip="Tooltips are used to provide information about an element on a web page.">i</span></p>
<span>
element displaying the text from the data attribute. Alternatively you could apply the data attribute to a hyperlink or button and the tooltip will function the same way.[data-tooltip] {
position: relative;
cursor: pointer;
background: black;
color: white;
font-size: 12px;
border-radius: 1em;
padding: 0 0.5em;
}
[attribute]
selector which selects all elements with a specified attribute (data-tooltip
). The actual tooltip that appears on hover will be constructed using :before
and :after
pseudo elements:[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;
}
Code language: CSS (css)
Finally we need to set the opacity to be visible when the tooltip element is hovered:
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
opacity: 1;
}