带换行/回车的简单html和css工具提示

Pap*_*Tee 3 html css html5 css3

我偶然发现了一个简单的精彩工具提示(http://cbracco.me/a-simple-css-tooltip/),但是当我使用它时,我无法添加新的行/回车.我试过
&013; \n但到目前为止没有成功.我也试过使用html内置的工具提示,但它在firefox中看起来很难看.

悬停时的工具提示:

在这里判刑一句.句子

两个在这里.这里三句话.

我想在悬停时输出的内容:

在这里判刑一句.
判刑二.
这里三句话.

<style>
/* Some basic styles */
*,
*:before,
*:after {
-webkit-box-sizing: border-box;
-moz-box-sizing:    border-box;
box-sizing:         border-box;
}

a:hover {
text-decoration: none;
}

/* Add this attribute to the element that needs a tooltip */
[data-tooltip] {
position: relative;
z-index: 2;
cursor: pointer;
}

/* Hide the tooltip content by default */
[data-tooltip]:before,
[data-tooltip]:after {
visibility: hidden;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=0)";
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=0);
opacity: 0;
pointer-events: none;
}

/* Position tooltip above the element */
[data-tooltip]:before {
position: absolute;
bottom: 150%;
left: 50%;
margin-bottom: 5px;
margin-left: -80px;
padding: 7px;
width: 160px;
-webkit-border-radius: 3px;
-moz-border-radius:    3px;
border-radius:         3px;
background-color: #000;
background-color: hsla(0, 0%, 20%, 0.9);
color: #fff;
content: attr(data-tooltip);
text-align: center;
font-size: 14px;
line-height: 1.2;
}

/* Triangle hack to make tooltip look like a speech bubble */
[data-tooltip]:after {
position: absolute;
bottom: 150%;
left: 50%;
margin-left: -5px;
width: 0;
border-top: 5px solid #000;
border-top: 5px solid hsla(0, 0%, 20%, 0.9);
border-right: 5px solid transparent;
border-left: 5px solid transparent;
content: " ";
font-size: 0;
line-height: 0;
}

/* Show tooltip content on hover */
[data-tooltip]:hover:before,
[data-tooltip]:hover:after {
visibility: visible;
-ms-filter: "progid:DXImageTransform.Microsoft.Alpha(Opacity=100)";
filter: progid:DXImageTransform.Microsoft.Alpha(Opacity=100);
opacity: 1;
}

</style> 

<a href="#" data-tooltip ="Sentence one here.
Sentence two here. 
Sentence three here.">Click for more details</a>
Run Code Online (Sandbox Code Playgroud)

gfu*_*lam 9

您可以

编辑:这纠正了我以前的答案,说你不能.

您正在使用的CSS content: attr()用于获取接受HTML实体的指定属性内的文本.

通过属性插入换行符:

注意:您需要设置display: blockwhite-space: pre|pre-line|pre-wrap显示换行符.

[data-tooltip] {
    cursor: default;
    font: normal 1em sans-serif;
}

[data-tooltip]:hover:after {
    display: inline-block;
    content: attr(data-tooltip);
    white-space: pre-wrap;
    color: Tomato;
    background-color: OldLace;
    padding: 1em;
}
Run Code Online (Sandbox Code Playgroud)
<span data-tooltip="Sentence one here. Sentence&#xa;two here. Sentence three here.">See my CSS tooltip with HTML-entity &amp;#xa; line break:</span>
Run Code Online (Sandbox Code Playgroud)


这里也回答了这个问题:CSS数据属性新行字符和伪元素内容值