这是我的HTML
<div class="tooltip" tooltip-data="Hi, I'm a tooltip">Div with standard tooltip. Hover me to see the tooltip.
</div>
这是我的CSS
.tooltip:before {
content: attr(tooltip-data);
position: absolute;
opacity: 0;
margin-left: 200px;
transition: all 0.15s ease;
padding: 10px;
color: #333;
border-radius: 10px;
box-shadow: 2px 2px 1px silver;
}
.tooltip:hover:before {
opacity: 1;
background: yellow;
margin-top: 0px;
margin-left: 220px;
}
.tooltip {
border: 1px solid silver;
background: #ddd;
margin: 20px;
padding: 10px;
width: 200px;
}
我做了这个JSfiddle
我想使工具提示更时尚和链接
我想拥有:
您可以通过添加额外的div并使用CSS来完成此操作。
它基本上是一个手动工具提示,您需要它,因为您无法将HTML插入工具提示的数据字段中。
希望这段代码片段有所帮助!
编辑:添加换行符。
.tooltip:before {
content: attr(tooltip-data);
position: absolute;
opacity: 0;
margin-left: 200px;
transition: all 0.15s ease;
padding: 10px;
color: #333;
border-radius: 10px;
box-shadow: 2px 2px 1px silver;
}
.tooltip:hover .tooltip-content {
opacity: 1;
background: yellow;
margin-top: 0px;
}
.tooltip-content {
position: absolute;
opacity: 0;
border-radius: 12px;
padding: 10px;
margin-left: 220px;
top: 3%;
}
.tt-title {
font-weight: bold;
}
span {
display: block;
}
.tooltip {
border: 1px solid silver;
background: #ddd;
margin: 20px;
padding: 10px;
width: 200px;
}
<div class="tooltip">Div with standard tooltip. Hover me to see the tooltip.
<div class="tooltip-content">
<span class="tt-title">Bold title</span> <span>short sentence</span> <a href="#"">link</a>
</div>
</div>
在span
DOM中使用wrap
并将其显示为onhover
.tooltip {
position: absolute;
opacity: 0;
margin-left: 200px;
transition: all 0.15s ease;
padding: 10px;
color: #333;
border-radius: 10px;
box-shadow: 2px 2px 1px silver;
}
.wrap:hover .tooltip{
opacity: 1;
background: yellow;
margin-top: 0px;
margin-left: 220px;
}
.wrap {
border: 1px solid silver;
background: #ddd;
margin: 20px;
padding: 10px;
width: 200px;
}
<div class="wrap">
<div class="tooltip">
<h4>TITLE</h4>
<p>short sentence </p>
<a href="#">click me!</a>
</div>
Div with standard tooltip. Hover me to see the tooltip.
</div>
伪内容有一些限制,我们不能用它来容纳HTML
元素。但是,我们仍然可以通过创建自己的基于标签的工具提示来做一些很酷的事情。
transform
代替margin
进行移动的性能改进(特别是在移动设备上)transform
缩放工具提示来改善外观role=tooltip
提高可访问性.tooltip-wrapper {
border: 1px solid silver;
background: #ddd;
margin: 20px;
padding: 10px;
width: 200px;
position: relative;
}
[role="tooltip"] {
position: absolute;
opacity: 0;
left: 100%;
min-width: 90px;
transform: translateX(-20px) scale(.85);
transition: all 0.15s ease;
padding: 10px;
color: #333;
border-radius: 10px;
box-shadow: 2px 2px 1px silver;
}
.tooltip-wrapper:focus [role="tooltip"],
.tooltip-wrapper:hover [role="tooltip"] {
opacity: 1;
background: yellow;
transform: translateX(0) scale(1);
}
[role="tooltip"].bottom {
left: 50%;
top: 100%;
transform: translate(-50%, -20px) scale(.85);
}
.tooltip-wrapper:focus [role="tooltip"].bottom,
.tooltip-wrapper:hover [role="tooltip"].bottom {
transform: translate(-50%, 0) scale(1);
}
<div class="tooltip-wrapper" tabindex="0">
<div role="tooltip">
<strong>Bold Title</strong>
<p>short setence</p>
<a href="#">a link!</a>
</div>
Div with HTML tooltip. Hover me to see the tooltip.
</div>
<div class="tooltip-wrapper" tabindex="0">
<div role="tooltip" class="bottom">
<strong>Bold Title</strong>
<p>short setence</p>
<a href="#">a link!</a>
</div>
Div with bottom HTML tooltip. Hover me to see the tooltip.
</div>