如何向span元素添加工具提示?

时间:2009-06-28 19:35:59

标签: html css

在下面的代码中,当用户悬停跨度时,我想要一个工具提示,我该怎么做?我不想使用任何链接。

<span> text </span>

5 个答案:

答案 0 :(得分:609)

这是简单的内置方式:

<span title="My tip">text</span>

这为您提供纯文本工具提示。如果您需要丰富的工具提示,并在其中包含格式化的HTML,则需要使用库来执行此操作。幸运的是there are loads of those

答案 1 :(得分:86)

使用纯CSS的自定义工具提示 - 无需JavaScript:

Example here (with code) / Full screen example

作为默认title属性工具提示的替代方法,您可以使用:before / :after pseudo elements和HTML5 data-* attributes制作自己的自定义CSS工具提示。

使用提供的CSS,您可以使用data-tooltip属性向元素添加工具提示。

您还可以使用data-tooltip-position属性控制自定义工具提示的位置(已接受的值:top / right / bottom / left)。

例如,以下内容将添加一个位于span元素底部的tooltop。

<span data-tooltip="Custom tooltip text." data-tooltip-position="bottom">Custom bottom tooltip.</span>

enter image description here

这是如何工作的?

您可以使用attr()函数检索自定义属性值,以显示包含伪元素的自定义工具提示。

[data-tooltip]:before {
    content: attr(data-tooltip);
}

在定位工具提示方面,只需使用attribute selector并根据属性的值更改展示位置。

Example here (with code) / Full screen example

示例中使用的完整CSS - 根据您的需要进行自定义。

[data-tooltip] {
    display: inline-block;
    position: relative;
    cursor: help;
    padding: 4px;
}
/* Tooltip styling */
[data-tooltip]:before {
    content: attr(data-tooltip);
    display: none;
    position: absolute;
    background: #000;
    color: #fff;
    padding: 4px 8px;
    font-size: 14px;
    line-height: 1.4;
    min-width: 100px;
    text-align: center;
    border-radius: 4px;
}
/* Dynamic horizontal centering */
[data-tooltip-position="top"]:before,
[data-tooltip-position="bottom"]:before {
    left: 50%;
    -ms-transform: translateX(-50%);
    -moz-transform: translateX(-50%);
    -webkit-transform: translateX(-50%);
    transform: translateX(-50%);
}
/* Dynamic vertical centering */
[data-tooltip-position="right"]:before,
[data-tooltip-position="left"]:before {
    top: 50%;
    -ms-transform: translateY(-50%);
    -moz-transform: translateY(-50%);
    -webkit-transform: translateY(-50%);
    transform: translateY(-50%);
}
[data-tooltip-position="top"]:before {
    bottom: 100%;
    margin-bottom: 6px;
}
[data-tooltip-position="right"]:before {
    left: 100%;
    margin-left: 6px;
}
[data-tooltip-position="bottom"]:before {
    top: 100%;
    margin-top: 6px;
}
[data-tooltip-position="left"]:before {
    right: 100%;
    margin-right: 6px;
}

/* Tooltip arrow styling/placement */
[data-tooltip]:after {
    content: '';
    display: none;
    position: absolute;
    width: 0;
    height: 0;
    border-color: transparent;
    border-style: solid;
}
/* Dynamic horizontal centering for the tooltip */
[data-tooltip-position="top"]:after,
[data-tooltip-position="bottom"]:after {
    left: 50%;
    margin-left: -6px;
}
/* Dynamic vertical centering for the tooltip */
[data-tooltip-position="right"]:after,
[data-tooltip-position="left"]:after {
    top: 50%;
    margin-top: -6px;
}
[data-tooltip-position="top"]:after {
    bottom: 100%;
    border-width: 6px 6px 0;
    border-top-color: #000;
}
[data-tooltip-position="right"]:after {
    left: 100%;
    border-width: 6px 6px 6px 0;
    border-right-color: #000;
}
[data-tooltip-position="bottom"]:after {
    top: 100%;
    border-width: 0 6px 6px;
    border-bottom-color: #000;
}
[data-tooltip-position="left"]:after {
    right: 100%;
    border-width: 6px 0 6px 6px;
    border-left-color: #000;
}
/* Show the tooltip when hovering */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
    display: block;
    z-index: 50;
}

答案 2 :(得分:20)

在大多数浏览器中,title属性将呈现为工具提示,并且通常可以灵活地确定它将使用哪种元素。

<span title="This will show as a tooltip">Mouse over for a tooltip!</span>
<a href="http://www.stackoverflow.com" title="Link to stackoverflow.com">stackoverflow.com</a>
<img src="something.png" alt="Something" title="Something">

所有这些都会在大多数浏览器中呈现工具提示。

答案 3 :(得分:4)

浏览器会将"title"属性用作工具提示的文本,如果要对其应用样式,请考虑使用某些plugins

答案 4 :(得分:4)

对于基本工具提示,您需要:

&#13;
&#13;
<span title="This is my tooltip"> Hover on me to see tooltip! </span>
&#13;
&#13;
&#13;

相关问题