How to center a tooltip horizontally over a link

I am trying to get a tooltip that should be pasted at the top and center of the tag. This works for me for short text, but I cannot get it to focus on large text.

I understand that this is due to the positioning of the window and arrow, but I can’t find the right combination to get the arrow centered under the field and the field above the tag.

I hope to do this only with CSS.

HTML:

<div> 
    <a class="tooltip" href="#" title="tooltip">
        <span>Text</span>    
    </a>

</div>
<div> <a class="tooltip" href="#" title="tooltip">
        <span>TextTextText</span>    
    </a>

</div>

CSS

div {
    padding: 150px;
    display: inline-block;
    background-color: red;
}
.tooltip{
    display: inline;
    position: relative;
}
.tooltip:hover:after{
    background: #333;
    background: rgba(0,0,0,.8);
    border-radius: 5px;
    bottom: 26px;
    color: #fff;
    content: attr(title);
    padding: 10px;
    left: -50%;
    position: absolute;
    z-index: 98;
}
.tooltip:hover:before{
    border: solid;
    border-color: #333 transparent;
    border-width: 6px 6px 0 6px;
    bottom: 20px;
    content: "";
    left: 30%;
    position: absolute;
    z-index: 99;
}

And here is the violin .

I am very grateful for any help I can get.

+3
source share
4 answers

width, translateX :

.tooltip:hover:after {
    left: 50%;
    transform: translateX(-50%);
    /* other styles ... */
}

.tooltip:hover:before {
    left: 50%;
    transform: translateX(-50%);
    /* other styles ... */
}

JSFiddle Demo.

+12

, CSS3-,

<a title="Create Simple Tooltip Using CSS3" class="tooltip">Some Sample CSS3 Tooltip</a>

.tooltip
{
 display: inline;
 position: relative;
}
.tooltip:hover:after
{
 background: #333;
 background: rgba(0,0,0,.8);
 border-radius: 5px;
 bottom: 26px;
 color: #fff;
 content: attr(title);
 left: 20%;
 padding: 5px 15px;
 position: absolute;
 z-index: 98;
 width: 220px;
}

.tooltip:hover:before
{
 border: solid;
 border-color: #333 transparent;
 border-width: 6px 6px 0 6px;
 bottom: 20px;
 content: "";
 left: 50%;
 position: absolute;
 z-index: 99;
}
0

, ( 1:00:)). .

div {
padding: 150px;
display: inline-block;
background-color: red;
text-align: center;
}
.tooltip {
display: inline;
position: relative;
}
.tooltip:hover:after {
background: #333;
background: rgba(0, 0, 0, .8);
border-radius: 5px;
bottom: 26px;
color: #fff;
content: attr(title);
padding: 10px;
left: 50%;
margin-left: -50px;
position: absolute;
z-index: 98;
width: 100px;
text-align: center;
}
.tooltip:hover:before {
border: solid;
border-color: #333 transparent;
border-width: 6px 6px 0 6px;
bottom: 20px;
right: 50%;
margin-right: -15px;
content:"";
position: absolute;
z-index: 99;
}

, Ashok

-1

All Articles