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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1055581/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:02:38  来源:igfitidea点击:

How do I add a tool tip to a span element?

htmlcss

提问by MichaelICE

In the following code, I want a tool-tip to come up when the user hovers the span, how do I do that? I don't want to use any links.

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

<span> text </span>

回答by RichieHindle

Here's the simple, built-in way:

这是简单的内置方式:

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

That gives you plain text tooltips. If you want rich tooltips, with formatted HTML in them, you'll need to use a library to do that. Fortunately there are loads of those.

这为您提供纯文本工具提示。如果你想要丰富的工具提示,其中包含格式化的 HTML,你需要使用一个库来做到这一点。幸运的是,有很多这样的

回答by Josh Crozier

Custom Tooltips with pure CSS - no JavaScript needed:

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

Example here (with code)/ Full screen example

此处示例(带代码)/全屏示例

As an alternative to the default titleattribute tooltips, you can make your own custom CSS tooltips using :before/:afterpseudo elementsand HTML5 data-*attributes.

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

Using the provided CSS, you can add a tooltip to an element using the data-tooltipattribute.

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

You can also control the position of the custom tooltip using the data-tooltip-positionattribute (accepted values: top/right/bottom/left).

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

For instance, the following will add a tooltop positioned at the bottom of the span element.

例如,下面将添加一个位于 span 元素底部的工具顶部。

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

enter image description here

在此处输入图片说明

How does this work?

这是如何运作的?

You can display the custom tooltips with pseudo elements by retrieving the custom attribute values using the attr()function.

您可以通过使用该attr()函数检索自定义属性值来显示带有伪元素的自定义工具提示。

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

In terms of positioning the tooltip, just use the attribute selectorand change the placement based on the attribute's value.

在定位工具提示方面,只需使用属性选择器并根据属性值更改位置。

Example here (with code)/ Full screen example

此处示例(带代码)/全屏示例

Full CSS used in the example- customize this to your needs.

示例中使用的完整 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;
}

回答by Brian Arnold Sinclair

In most browsers, the title attribute will render as a tooltip, and is generally flexible as to what sorts of elements it'll work with.

在大多数浏览器中,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">

All of those will render tooltips in most every browser.

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

回答by Abk

For the basic tooltip, you want:

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

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

回答by Rony

the "title"attribute will be used as the text for tooltip by the browser, if you want to apply style to it consider using some plugins

"title"属性将被浏览器用作工具提示的文本,如果您想对其应用样式,请考虑使用一些插件