Create a tooltip that looks nice with only CSS is what we will try here with the code below.
We will use 2 different HTML layouts.
1. The HTML layout with parent container with the text tooltip:
<div class="awkToolTip">Lets hover on me
<span class="awkToolTipText">Tooltip text</span>
</div>
The CSS code:
.awkToolTip {
position: relative;
display: inline-block;
}
.awkToolTipText {
visibility: hidden;
width: 120px;
background-color: rgba(51, 51, 51, 0.9);
color: #fff;
text-align: center;
padding: 10px;
border-radius: 6px;
position: absolute;
z-index: 1;
bottom: 55px;
left: 50%;
opacity: 0;
transition: opacity 0.3s;
font-size: 14px;
line-height: 1.3;
transform: translateX(-50%);
}
.awkToolTipText::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: rgba(51, 51, 51, 0.9) transparent transparent transparent;
}
.awkToolTip:hover .awkToolTipText {
opacity:1;
}
The SCSS code:
.awkToolTip {
position: relative;
display: inline-block;
.awkToolTipText {
visibility: hidden;
width: 120px;
background-color: rgba(51, 51, 51, 0.9);
color: #fff;
text-align: center;
padding: 10px;
border-radius: 6px;
position: absolute;
z-index: 1;
bottom: 55px;
left: 50%;
opacity: 0;
transition: opacity 0.3s;
font-size: 14px;
line-height: 1.3;
transform: translateX(-50%);
&::after {
content: "";
position: absolute;
top: 100%;
left: 50%;
margin-left: -5px;
border-width: 5px;
border-style: solid;
border-color: rgba(51, 51, 51, 0.9) transparent transparent transparent;
}
}
&:hover .awkToolTipText {
opacity:1;
}
}
2. Here is another approach with only one HTML container
HTML code:
<div data-text="Tooltip Text" class="awkToolTip">
Lets hover on me
</div>
And the CSS
.awkToolTip {
position: relative;
display: inline-block;
}
.awkToolTip::after {
content: attr(data-text);
position: absolute;
top: 50%;
transform: translateY(-50%);
left: 100%;
margin-left: 15px;
width: 200px;
padding: 10px;
border-radius: 10px;
background: #000;
color: #fff;
text-align: center;
opacity: 0;
}
.awkToolTip:hover::after {
opacity: 1;
}