简体   繁体   English

如何在我的CSS文件中为图标添加工具提示

[英]How to add a ToolTip for an icon inside my CSS file

I have the following icon , which is used mainly on most of my asp.net mvc web pages:- 我有以下图标,该图标主要在大多数asp.net mvc网页上使用:-

<i class=" icon icon-blue icon-star-on "></i>

The related CSS classes for this icon is :- 此图标的相关CSS类是:-

 .active .icon32.icon-star-on,.icon32.icon-star-on,.icon32.icon-star-on:hover {background-position : -448px -96px ;}
 .icon.icon-blue,.icons-blue .icon {background-image : url('../img/opa-icons-blue16.png') ;}

But I need to add a tootip for the icon, so that when a use move the mouse over the icon to show a tooltip. 但是我需要为该图标添加一个tootip,以便在使用时将鼠标移到该图标上以显示一个工具提示。 So is there a way to define the tooltip inside my CSS , so that I do not have to add the tooltip manually on each screen? 因此,有没有一种方法可以在CSS内定义工具提示,从而不必在每个屏幕上手动添加工具提示? Thanks 谢谢

Yes, by using :after : 是的,通过使用:after

.icon:hover:after {
    content: "Your tooltip";
    display: block;
    position: relative;
    top: -16px;
    right: -16px;
    width: 100px;
    background: lightblue;
}

Also check the demo . 还要检查演示

Please note that all .icon classes get the same tooltip. 请注意,所有.icon类都具有相同的工具提示。 This is pure CSS solution. 这是纯CSS解决方案。 If you want them to be different for each icon, you could use different classes, or use jQuery. 如果希望它们对于每个图标都不同,则可以使用不同的类,或使用jQuery。

Add this code to your css: 将此代码添加到您的CSS:

.tooltip {
    display: inline;
    position: relative;
}
.tooltip:hover {
    text-decoration: none;
}
.tooltip:hover:after{
    background: #111;
    background: rgba(0,0,0,.8);
    border-radius: 5px;
    bottom: 18px;
    color: #fff;
    content: attr(title);
    display: block;
    left: 50%;
    padding: 5px 15px;
    position: absolute;
    white-space: nowrap;
    z-index: 98;
}
.tooltip:hover:before{
    border: solid;
    border-color: #111 transparent;
    border-width: 6px 6px 0 6px;
    bottom: 12px;
    content: "";
    display: block;
    left: 75%;
    position: absolute;
    z-index: 99;
}

Add aa title to your icon title="Tooltip text" and add the class of tooltip to the icon. 将标题添加到图标title="Tooltip text" ,并将tooltip的类别添加到图标。

Source: http://forrst.com/posts/Simple_pure_CSS_tooltip_with_arrow-BkY 资料来源: http : //forrst.com/posts/Simple_pure_CSS_tooltip_with_arrow-BkY

Add an attribute with the text for the element and use it for hovering. 在元素的文本中添加属性,并将其用于悬停。 I have showed using an anchor tag. 我已经展示过使用锚标签。

html html

<a href="#" title="you have hovered">Hover here</a>

css 的CSS

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

' http://jsfiddle.net/Kxnqx/ ' ' http://jsfiddle.net/Kxnqx/ '

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM