简体   繁体   English

CSS 中的更智能断词?

[英]Smarter word break in CSS?

If I just put word-break: break-all on an element, I often end up with this:如果我只是将word-break: break-all放在一个元素上,我通常会得到这样的结果:

Hello people, I am typing a mes大家好,我正在打字
sage that's too long to fit!圣人太长了,不适合!

Obviously this would be much better as:显然这会更好:

Hello people, I am typing a大家好,我正在输入
message that's too long to fit!消息太长,无法容纳!

But at the same time if someone writes:但与此同时,如果有人写道:

BLAAAAAAAAARRRRRRRRRRRRGGGGGGGGHHHHHHHH!!!!!!啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊啊!!!!!!

Then I'd want it to be:然后我希望它是:

BLAAAAAAAAARRRRRRRRRRR BLAAAAAAAAARRRRRRRRRRRR
RGGGGGGGGHHHHHHHH!!!!!!嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎嘎!!!!!!

I can't seem to find a way to actually do this.我似乎找不到真正做到这一点的方法。

Note that the width of the element is not fixed and may change.请注意,元素的宽度不是固定的,可能会发生变化。

Try word-break: break-word;尝试word-break: break-word; it should behave as you expect.它应该按照您的预期运行。

For a lot of our projects we usually add this where necessary:对于我们的很多项目,我们通常在必要时添加:

.text-that-needs-wrapping {
    overflow-wrap: break-word;
    word-wrap: break-word;
    -ms-word-break: break-all;
    word-break: break-word;
    -ms-hyphens: auto;
    -moz-hyphens: auto;
    -webkit-hyphens: auto;
    hyphens: auto;
}

It handles most odd situations with different browsers.它处理不同浏览器的大多数奇怪情况。

The updated answer should be:更新后的答案应该是:

overflow-wrap:break-word;

It will break a word that by itself would not be able to fit on its own line, but leave all other words as they are (see overflow-wrap here ).它会破坏一个本身无法容纳在自己的行中的单词,但保留所有其他单词(请参阅 此处的溢出包装)。

You could also use:您还可以使用:

overflow-wrap:anywhere; but this will allow line breaks after any word in an effort to reduce the width of an element.但这将允许在任何单词后换行,以减少元素的宽度。 See the difference described below from MDN:请参阅以下 MDN 中描述的差异:

[break-word is] The same as the anywhere value, with normally unbreakable words allowed to be broken at arbitrary points if there are no otherwise acceptable break points in the line, but soft wrap opportunities introduced by the word break are NOT considered when calculating min-content intrinsic sizes. [break-word is] 与anywhere值相同,如果行中没有其他可接受的断点,则允许在任意点打破通常不可破坏的单词,但在计算时不考虑由单词break引入的软换行机会最小内容内在大小。

Also, anywhere is not supported by Internet Explore, Safari, and some mobile browsers while break-word is supported on all major browsers (see [here][2]).此外,Internet Explorer、Safari 和某些移动浏览器不支持anywhere ,而所有主要浏览器都支持break-word (请参阅 [此处][2])。

word-break: break-word; should no longer be used because it is deprecated in favor of the overflow-wrap:break-word;不应再使用它,因为它已被弃用,取而代之的是overflow-wrap:break-word; . . Now, the word-break property is intended to be used when you want to break words regardless of whether they could fit on their own line (ie the OP's first example with word-break: break-all .现在, word-break属性旨在用于当您想要中断单词时,无论它们是否适合自己的行(即 OP 的第一个带有word-break: break-all的示例。

In contrast to word-break, overflow-wrap will only create a break if an entire word cannot be placed on its own line without overflowing.与 word-break 相比,overflow-wrap 只会在整个单词不能放在它自己的行上而不溢出的情况下才会创建一个 break。

(From overflow-wrap also linked above ) (来自 上面也链接的溢出包装)

For smart word breaks, or for correct word breaks in the first place, you need language-dependent rules.对于智能分词,或首先要正确分词,您需要与语言相关的规则。 For English and many other languages, the correct breaking means hyphenation, with a hyphen added at the end of a line when a break occurs.对于英语和许多其他语言,正确的断行意味着断字,断行时在行尾添加连字符。

In CSS, you can use hyphens: auto , though you mostly still need to duplicate it using vendor prefixes.在 CSS 中,您可以使用hyphens: auto ,尽管您仍然需要使用供应商前缀来复制它。 As this does not work on IE 9, you may consider JavaScript-based hyphenation like Hyphenate.js instead.由于这在 IE 9 上不起作用,您可以考虑使用基于 JavaScript 的字符,例如 Hyphenate.js。 In both cases, it is essential to use language markup ( lang attribute).在这两种情况下,都必须使用语言标记( lang属性)。

Breaking long, unhyphenateable strings is a different issue.打断长的、不可连字符的字符串是一个不同的问题。 They would best be handled in preprocessing, but in a simple setting, word-break: break-word (which means incorrect breaking of words, in English for example) may be considered as an emergency.最好在预处理中处理它们,但在简单的设置中, word-break: break-word (这意味着不正确的断词,例如英语)可能被视为紧急情况。

This is what youre looking for:这就是你要找的:

.break-word {
-ms-word-break: break-all;
-ms-word-wrap: break-all;
-webkit-word-break: break-word;
-webkit-word-wrap: break-word;
word-break: break-word;
word-wrap: break-word;
-webkit-hyphens: auto;
-moz-hyphens: auto;
hyphens: auto;
}

overflow-wrap: anywhere; is what you need.是你需要的。

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

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