简体   繁体   English

有没有办法让CSS“打破所有”这个词破坏财产更聪明?

[英]Is there any way to make the CSS “break-all” word breaking property smarter?

I'm trying to get my table to fit in a small screen (< 400 pixel width) so I applied this style to my table cells … 我正试图让我的桌子适合小屏幕(<400像素宽度)所以我将这种风格应用于我的桌子细胞......

#searchResults td {
    padding: 1px;
    word-break: break-all;
}

But my question is, can I make this function smarter? 但我的问题是,我可以让这个功能变得更聪明吗? That is, notice in my Fiddle, https://jsfiddle.net/hk42jb5r/1/ , that the first name, “Dave Echoer”, breaks the name into “Dave Echt” and “er” if you reduce the screen to 320 pixels wide. 也就是说,请注意我的小提琴, https ://jsfiddle.net/hk42jb5r/1/,第一个名字“Dave Echoer”将名称分为“Dave Echt”和“er”,如果将屏幕缩小为320像素宽。 Because there's a space in the text, it would make more sense for the split to occur at the space. 因为文本中有空格,所以在空间中进行拆分会更有意义。 Breaking the word there wouldn't affect the table width. 打破这个词不会影响表格宽度。 Is there any way I can get this to happen when its appropriate? 有什么方法可以让它在适当的时候发生吗?

It seems you want the behavior of 看来你想要的行为

word-break: break-word;

It is a non-standard property supported by Chrome which behaves almost like word-wrap: break-word . 它是Chrome支持的非标准属性,其行为几乎像word-wrap: break-word However, when they tried to remove it , they noticed 然而,当他们试图将其移除时 ,他们注意到了

In some cases (tables and display: table; layouts) the word-break: break-word behavior is different from word-wrap: break-word; 在某些情况下(表和display: table; layouts), word-break: break-word行为与word-wrap: break-word;不同word-wrap: break-word; and the former works as expected. 而前者按预期工作。

The CSS WG resolved to CSS工作组解决

Add word-break: break-word to spec if Edge/Firefox find it critical enough to Web compat to implement it. 如果Edge / Firefox发现它足以让Web compat实现它,那么将word-break: break-word添加到spec。

So it may become standard. 所以它可能成为标准。


But currently, if you want to prevent table cells from being at least as wide as the longest word, and still avoid breaking inside words when it's not necessary, you can use 但是目前,如果你想防止表格单元格至少与最长单词一样宽,并且在没有必要时仍然避免破坏内部单词,你可以使用

table-layout: fixed;

Note this will get rid of some special table behaviors, eg all columns will be equally wide even if their contents could fit better in other arrangements. 请注意,这将消除一些特殊的表行为,例如,即使其内容在其他安排中更适合,所有列也将同样宽。

I got this working in your example with: 我在你的例子中得到了这个:

word-break: normal;
overflow-wrap: break-word;

The word-break CSS property is used to specify whether to break lines within words. word-break CSS属性用于指定是否在单词中断行。

Source - Mozilla Developer Network 来源 - Mozilla开发者网络

The overflow-wrap property is used to specify whether or not the browser may break lines within words in order to prevent overflow when an otherwise unbreakable string is too long to fit in its containing box. overflow-wrap属性用于指定浏览器是否可以在单词中断行,以防止在其他不可破解的字符串太长而无法容纳其包含框时溢出。

Source - Mozilla Developer Network 来源 - Mozilla开发者网络

I usually don't use word-break in situations like this, but instead put &shy; 我通常不会在这样的情况下使用断言,而是放弃&shy; tags (= "soft hyphen") into the longer words, at positions where a hyphenation is appropriate, like 标签(=“软连字符”)成长字,在连字符合适的位置,如

 Hydro&shy;philius Pono&shy;clacti&shy;vizius

So this will be displayed in one of the following forms, depending on the width of its container: 因此,这将以下列形式之一显示,具体取决于其容器的宽度:

Hydrophilius Ponoclactivizius

or 要么

Hydrophilius Pono-
clactivizius

or 要么

Hydrophilius 
Ponoclacti-
vizius

or 要么

Hydro-
philius 
Pono-
clacti-
vizius

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

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