在工具提示中显示换行文本的溢出文本

huangapple go评论50阅读模式
英文:

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 -->

&lt;div class=&quot;new&quot;&gt;
  &lt;span class=&quot;tooltip&quot; data-tooltip=&quot;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.&quot;&gt;
    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.
&lt;/span&gt;
&lt;/div&gt;

<!-- 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::afterwhite-space属性设置为normal

这里是一个具有可工作示例的fiddle,只需添加一行:

.white-space: normal; /* &lt;---- 这里 */
英文:

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; /* &lt;---- here */
}
        
.tooltip:hover::after {
    opacity: 1;
    visibility: visible;
}

<!-- language: lang-html -->

&lt;div class=&quot;new&quot;&gt;
  &lt;span class=&quot;tooltip&quot; data-tooltip=&quot;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.&quot;&gt;
    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.
&lt;/span&gt;
&lt;/div&gt;

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年5月13日 23:13:36
  • 转载请务必保留本文链接:https://go.coder-hub.com/76243460.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定