Is it possible to center a tooltip horizontally under its parent without setting a static width on the tooltip? I am facing this issue because the length of the text in the tooltip varies. Below is the code snippet for reference. Even though adjusting the absolute positioning can make it perfectly centered, the varying text length causes alignment issues.
body {
margin: 20px;
}
.tooltip {
position: relative;
display: inline-block;
border-bottom: 1px dotted black;
}
.tooltip .tooltiptext {
visibility: hidden;
background-color: black;
color: #fff;
text-align: center;
border-radius: 6px;
padding: 5px 0;
position: absolute;
z-index: 1;
top: 150%;
left: 50%;
margin-left: -60px;
}
.tooltip:hover .tooltiptext {
visibility: visible;
}
<div class="tooltip">Hover over me
<span class="tooltiptext">Notifications</span>
</div>
<br/><br/><br/><br/>
<div class="tooltip">Hover over me
<span class="tooltiptext">Account</span>
</div>
<br/><br/><br/><br/>
<div class="tooltip">Hover over me
<span class="tooltiptext">Help</span>
</div>