如何将工具提示水平放置在链接顶部 [英] How to center a tooltip horizontally on top of a link

查看:85
本文介绍了如何将工具提示水平放置在链接顶部的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要一个工具提示框插入到a标签的顶部和中心。我有这个工作的小文本,但我似乎不能把它放在大文本的中心。

I'm trying to get a tooltip box to be inserted on top and centered of an a tag. I have this working for small Text, but I can't seem to get it to center on large text.

我知道它与定位的框和箭头,但我似乎找不到正确的组合,让箭头集中在框和中心在a标签上方的框中。

I understand it has to do with positioning the box and the arrow but I can't seem to find the right combination to get the arrow centered under the box and the box centered above the a tag.

我希望完成这只需要CSS。

I'm hoping to accomplish this with just CSS alone.

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;
}

这里是小提琴

我真的很感激能得到任何帮助。

I really would appreciate any help I can get.

推荐答案

如果工具提示没有明确的宽度可以通过使用否定 translateX 如下:

If the tooltip doesn't have an explicit width, you could align that horizontally center, by using a negative translateX as follows:

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

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

JSFiddle演示

JSFiddle Demo.

这篇关于如何将工具提示水平放置在链接顶部的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆