简体   繁体   English

table-cell自动换行不使用斜杠

[英]table-cell word-wrap not working with slashes

I am trying to wrap text in a fixed layout but it doesn't work when the text contains slashes. 我试图将文本包装在固定的布局中,但当文本包含斜杠时它不起作用。

Can this be fixed without inserting spaces from Javascript (pure CSS)? 这可以修复而不从Javascript(纯CSS)插入空格?

jsfiddle: 的jsfiddle:

http://jsfiddle.net/HgBhk/1/ http://jsfiddle.net/HgBhk/1/

Not working:
<div style="display:table; width:170px; background-color:cyan;">
    <div style="display:table-row">
        <div style="display:table-cell;word-wrap: break-word;">abfdbfdbfdb/dfbfdbdfbfbf/bdffbdbfdfbddfbdfbdfb</div>
        <div style="display:table-cell; width:34px;  background-color:red;">xxxxx</div>        
    </div>
</div>

</br>
Working:
<div style="display:table; width:170px; background-color:cyan;">
    <div style="display:table-row">
        <div style="display:table-cell;word-wrap: break-word;">abfdbfdbfdb dfbfdbdfbfbf bdffbdbfdfbddfbdfbdfb</div>
        <div style="display:table-cell; width:34px;  background-color:red;">xxxxx</div>        
    </div>
</div>

Insert either a <wbr> tag or a zero-width space &#x200b; 插入<wbr>标签或零宽度空间&#x200b; after each occurrence of a slash or other character that should be treated as allowing direct line break after it. 每次出现斜杠或其他字符后,应将其视为允许在其后直接换行。 The choice between these alternatives is a bit complicated , but since your code already fails to work on old versions of IE, you might just as well ignore them here too, and this would make &#x200b; 这些替代方案之间的选择有点复杂 ,但由于您的代码已经无法在旧版本的IE上运行,您也可以在这里忽略它们,这将使&#x200b; the right choice. 正确的选择。 That is, you would write eg 也就是说,你会写例如

abfdbfdbfdb/&#x200b;dfbfdbdfbfbf/&#x200b;bdffbdbfdfbddfbdfbdfb

The issue is at the character level, not a matter of styling. 问题出在角色层面,而不是造型问题。

A string containing no whitespace characters is normally indivisible in wrapping and should be that way, unless you can reliably make browsers either break at acceptable breaking points or hyphenate properly and use word division. 一个不包含空白字符的字符串通常在包装中是不可分割的,应该是这样的,除非你可以可靠地使浏览器在可接受的断点处断开或正确连字并使用分词。 A setting like word-wrap: break-word is meant for exceptional cases, emergency breaks when there is no good way to control wrapping and there is a need to avoid overflow. word-wrap: break-word这样的设置word-wrap: break-word意味着特殊情况,当没有好的方法来控制包装时需要紧急中断,并且需要避免溢出。

Add table-layout:fixed; 添加table-layout:fixed; to the "table" div-element and assign a width to the cell to make it break lines: 到“table”div-element并为单元格指定宽度以使其断行:

JSFiddle 的jsfiddle

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

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