简体   繁体   English

绝对定位的元素调整大小以适合文本

[英]Absolutely positioned element resize to fit the text

I am trying to make a tooltip that shows extra info when the keyword is hovered or selected.我正在尝试制作一个工具提示,在悬停或选择关键字时显示额外信息。 The part I am struggling with is getting an absolutely positioned element to reflow the text in a nice manner.我正在努力的部分是获得一个绝对定位的元素来以一种很好的方式重排文本。

Using white-space: nowrap;使用空格:nowrap; works well with small amounts of text but is useless with large amounts of text.适用于少量文本,但对大量文本无用。 The best solution I have come up with is to wrap the tooltip's container in an extra container that has a width of the max width I wish the tooltip to be.我想出的最佳解决方案是将工具提示的容器包装在一个额外的容器中,该容器的宽度是我希望工具提示的最大宽度。 This works great when the keyword is on the left of the screen and the container doesn't touch the right side of the screen, but when the keyword is close enough to the right side of the screen that the container passes the edge of the screen a horizontal scroll bar appears.当关键字位于屏幕左侧且容器未触及屏幕右侧时,此方法非常有用,但当关键字与屏幕右侧足够接近时,容器会通过屏幕边缘出现水平滚动条。

Does anybody have any suggestion on how to get an absolutely positioned element to resize around text so that small amounts of text are contained fully without extra space but still allow large amounts of text to reflow once a certain width is hit?有没有人对如何让一个绝对定位的元素在文本周围调整大小有任何建议,以便在没有额外空间的情况下完全包含少量文本,但一旦达到一定宽度仍然允许大量文本重排?

 a.tooltip { position: relative; } a.tooltip >div { position: absolute; display: none; width: 350px; } a.tooltip >div >div { display: inline-block; border: 1px solid red; border-radius: 5px; padding 5px; } a.tooltip:focus >div, a.tooltip:hover >div { display: inline-block; }
 <div style="display: inline-block; width: 400px;"> <a href="javascript:void(0)" class="tooltip"> House <div> <div> A self-contained dwelling structurally independent from other dwellings and intended for long-term residential use. To be self-contained the suite of rooms must possess cooking and bathing facilities as building fixtures. </div> </div> </a> </div> <a href="javascript:void(0)" class="tooltip"> Keyword <div> <div> Not much text </div> </div> </a>

A negative margin-right combined with max-width can do the trick here.margin-right结合max-width可以在这里解决问题。 You can also keep only one wrapper:您也可以只保留一个包装器:

 a.tooltip { position: relative; } a.tooltip >div { position: absolute; display: none; max-width: 350px; margin-right: -350px; border: 1px solid red; border-radius: 5px; padding 5px; } a.tooltip:focus >div, a.tooltip:hover >div { display: inline-block; }
 <div style="display: inline-block; width: 400px;"> <a href="javascript:void(0)" class="tooltip"> House <div> A self-contained dwelling structurally independent from other dwellings and intended for long-term residential use. To be self-contained the suite of rooms must possess cooking and bathing facilities as building fixtures. </div> </a> </div> <a href="javascript:void(0)" class="tooltip"> Keyword <div> Not much text </div> </a>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM