Setting text-overflow ellipsis for a float left div or list

試著忘記壹切 提交于 2019-12-23 20:01:07

问题


Here's what I'd like to do:

  • Create a list that only takes up as much room (horizontally) as needed (i.e. as the longest list element.

  • When the list doesn't fit on the page, trim the list text and show an ellipsis.

I use a combination of white-space:nowrap and text-overflow:ellipsis. It works fine for a normal list, but in that case the list takes up the full width of the page.

To force it to only take as much space as needed I applied a float:left, but this breaks the text-overflow.

Example:

<ol style="border:1px solid red;font-size:100px;">
    <li style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">Long list item. Ellipsis works.</li>
</ol>

<ol style="float:left;border:1px solid red;font-size:100px;">
    <li style="white-space:nowrap;overflow:hidden;text-overflow:ellipsis;">Float left list. No ellipsis here.</li>
</ol>

Live example: http://jsfiddle.net/73M7G/1/


回答1:


because a floating element does not inherits the size from its container,

you need to specify the width:; to < ULs > too!

http://jsfiddle.net/73M7G/3/

test with width:100%;

another test http://jsfiddle.net/73M7G/6/




回答2:


I suggest to make the list an inline-block, see it in action here http://jsfiddle.net/73M7G/4/



来源:https://stackoverflow.com/questions/7678728/setting-text-overflow-ellipsis-for-a-float-left-div-or-list

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!