I am trying to create tooltips using css. The idea is to make the tooltip vertically and horizontally centered to its parent element but keep it hidden before moving and displaying it on hover. Anyway, the following is my code:
.tooltip-top {
position: relative;
}
.tooltip-top::before {
content: attr(title);
padding: 1rem 1.5rem;
background-color: rgba(0, 0, 0, 0.5);
text-align: center;
position: absolute;
width: 60px;
}
<a href="#" class="tooltip-top" title="content">Tooltip</a>
As you can see, the tooltip is not centered according to its parent element. I added the styles left: 50%
, and top: 50%
to it, but it still was not perfectly centered. How can I do this?