英文:
Show overflow text in tooltip with wrapped text
问题
.new {
max-width: 600px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
display: inline-block; /* Add this line */
}
英文:
I need the tooltip to appear in a block with a fixed maximum width. When text inside the block is too long, it should truncate the text and show three dots in the end, and you can hover the mouse over it to see all text in tooltip.
This is my code to try to do this:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.new{
max-width: 600px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.tooltip {
border-bottom: 1px dotted #0077AA;
cursor: help;
}
.tooltip::after {
background: rgba(0, 0, 0, 0.8);
border-radius: 8px 8px 8px 0px;
box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.5);
color: #FFF;
content: attr(data-tooltip);
left:25px;
margin-top: -24px;
opacity: 0;
padding: 3px 7px;
position: absolute;
visibility: hidden;
width: 300px;
display: block;
transition: all 0.4s ease-in-out;
}
.tooltip:hover::after {
opacity: 1;
visibility: visible;
}
<!-- language: lang-html -->
<div class="new">
<span class="tooltip" data-tooltip="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer elit lorem, elementum ut euismod sed, lacinia et dui. Morbi accumsan metus eget elit porttitor condimentum. Sed quis consectetur eros, eu luctus velit. Proin semper tortor vel risus scelerisque, nec elementum nisl imperdiet. Proin sit amet erat pulvinar, viverra nulla eu, molestie lorem. Nam sit amet neque commodo odio fringilla tempor accumsan id tellus. In hendrerit nisi ipsum.">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer elit lorem, elementum ut euismod sed, lacinia et dui. Morbi accumsan metus eget elit porttitor condimentum. Sed quis consectetur eros, eu luctus velit. Proin semper tortor vel risus scelerisque, nec elementum nisl imperdiet. Proin sit amet erat pulvinar, viverra nulla eu, molestie lorem. Nam sit amet neque commodo odio fringilla tempor accumsan id tellus. In hendrerit nisi ipsum.
</span>
</div>
<!-- end snippet -->
In my code, the overflow
works correctly and truncates the text but the tooltip also shows in one line.
If I turn off overflow in the div
, the tooltip shows perfectly but the span
overflow doesn't work.
答案1
得分: 4
这是发生的原因,因为工具提示继承了所有父元素的样式。
尝试覆盖父元素的样式,以获得您需要的结果,例如将.tooltip::after
的white-space
属性设置为normal
。
这里是一个具有可工作示例的fiddle,只需添加一行:
.white-space: normal; /* <---- 这里 */
英文:
This is happening because the tooltip is inheriting all parent styles.
Try to override the parent styles to get the result you need, for example setting the white-space
property to normal
for .tooltip::after
.
Here is a fiddle with working example, only adding one line:
<!-- begin snippet: js hide: false console: true babel: false -->
<!-- language: lang-css -->
.new {
max-width: 600px;
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
.tooltip {
border-bottom: 1px dotted #0077AA;
cursor: help;
}
.tooltip::after {
background: rgba(0, 0, 0, 0.8);
border-radius: 8px 8px 8px 0px;
box-shadow: 1px 1px 10px rgba(0, 0, 0, 0.5);
color: #FFF;
content: attr(data-tooltip);
left: 25px;
margin-top: -24px;
opacity: 0;
padding: 3px 7px;
position: absolute;
visibility: hidden;
width: 300px;
display: block;
transition: all 0.4s ease-in-out;
/** added line **/
white-space: normal; /* <---- here */
}
.tooltip:hover::after {
opacity: 1;
visibility: visible;
}
<!-- language: lang-html -->
<div class="new">
<span class="tooltip" data-tooltip="Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer elit lorem, elementum ut euismod sed, lacinia et dui. Morbi accumsan metus eget elit porttitor condimentum. Sed quis consectetur eros, eu luctus velit. Proin semper tortor vel risus scelerisque, nec elementum nisl imperdiet. Proin sit amet erat pulvinar, viverra nulla eu, molestie lorem. Nam sit amet neque commodo odio fringilla tempor accumsan id tellus. In hendrerit nisi ipsum.">
Lorem ipsum dolor sit amet, consectetur adipiscing elit. Integer elit lorem, elementum ut euismod sed, lacinia et dui. Morbi accumsan metus eget elit porttitor condimentum. Sed quis consectetur eros, eu luctus velit. Proin semper tortor vel risus scelerisque, nec elementum nisl imperdiet. Proin sit amet erat pulvinar, viverra nulla eu, molestie lorem. Nam sit amet neque commodo odio fringilla tempor accumsan id tellus. In hendrerit nisi ipsum.
</span>
</div>
<!-- end snippet -->
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论