“word-break: Break-all” 和有什么不一样?与“自动换行:断字”相对在CSS中?
“word-break:break-all”和“word-wrap:break-word”有什么区别?
当我使用两者时,如果它不适合容器,它们似乎会破坏这个词。但为什么 W3C 制定了两种方法来做到这一点?
What is the difference between "word-break: break-all" and "word-wrap: break-word"?
When I used both, they seemed to break the word if it was not fitting the container. But why did W3C make two ways to do it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(13)
word-wrap
已重命名为overflow-wrap
可能是为了避免这种混乱。现在这就是我们所拥有的:
溢出包装
可能的值:
正常:表示行只能在正常的单词断点处断行。
break-word:表示如果行中没有其他可接受的断点,则通常不可断的单词可以在任意点处断。
来源。
断词
来源。
现在回到你的问题。 overflow-wrap 和 word-break 之间的主要区别在于,前者确定溢出情况下的行为,而后者确定溢出情况下的行为在正常情况下(无溢出)。当容器没有足够的空间来容纳文本时,就会发生溢出情况。在这种情况下断线没有帮助,因为没有空间(想象一个具有固定宽度和高度的盒子)。
因此:
overflow-wrap:break-word
:在溢出情况下,断开单词。word-break:break-all
:正常情况下,只需将行尾的单词打断即可。溢出是不必要的。word-wrap
has been renamed tooverflow-wrap
probably to avoid this confusion.Now this is what we have:
overflow-wrap
Possible values:
normal: Indicates that lines may only break at normal word break points.
break-word: Indicates that normally unbreakable words may be broken at arbitrary points if there are no otherwise acceptable break points in the line.
Source.
word-break
Source.
Now back to your question. The main difference between overflow-wrap and word-break is that the first determines the behavior on an overflow situation, while the later determines the behavior on a normal situation (no overflow). An overflow situation happens when the container doesn't have enough space to hold the text. Breaking lines on this situation doesn't help because there's no space (imagine a box with fix width and height).
So:
overflow-wrap: break-word
: In an overflow situation, break the words.word-break: break-all
: In a normal situation, just break the words at the end of the line. An overflow is not necessary.至少在 Firefox(自 v24 起)和 Chrome(自 v30 起)中,当应用于
table
元素中的内容时:word-wrap:break-word
实际上不会导致长字换行,这可能导致表超出其容器的边界;
word-break:break-all
将导致文字换行,并且表格适合其容器。
jsfiddle 演示。
At least in Firefox (as of v24) and Chrome (as of v30), when applied to content in a
table
element:word-wrap:break-word
will not actually cause long words to wrap, which can result in the table exceeding the bounds of its container;
word-break:break-all
will result in words wrapping, and the table fitting within its container.
jsfiddle demo.
存在巨大差异。
break-word
只会破坏不适合容器的单词。虽然break-all
会无情地尝试最大化每行的字符数,并会无情地导致“不必要的断字”,这看起来很可怕,但我认为。示例
在六个字符宽的容器内以等宽字体呈现字符串
This is a text from an old magazine
。使用
word-break:break-all
时,大多数单词都会被破坏,并且看起来不可读:break-all
所做的就是逐行进行操作,只在上面放置六个字符每一行,直到没有剩余。它会毫不留情地这样做,甚至像“is”这样的两个字母的单词也可以分成两行。这看起来非常糟糕,我永远不会用它来渲染文本。使用
word-wrap:break-word
只有长单词才会被破坏:break-word
的作用要好得多。它只会破坏那些永远无法适合容器宽度的单词。当前行放不下的单词将被简单地推到下一行。因此,在这种情况下,对于每行可容纳六个字符的容器,它必须被破坏一个八个字符的单词,例如“magazine”,但它永远不会破坏像“text”这样的较短单词。
There's a huge difference.
break-word
will only break words that don't fit the container. Whilebreak-all
will ruthlessly try to max the amount of chars per row and will ruthlessly cause "unnecessary word-breaks" which looks horrible, IMO.Example
Rendering the string
This is a text from an old magazine
in a monospace font inside a container which is six characters wide.With
word-break: break-all
most words are broken and it looks unreadable:What
break-all
does is to go row-by-row and just put six characters on each row until there are none remaining. It will do this ruthlessly, even two-letter-words like "is" can be divided into two rows. This looks absolutely terrible, and I would never use it to render text.With
word-wrap: break-word
only the long words get broken:What
break-word
does is much nicer. It only breaks words that could never fit the width of the container. Words that can't fit on the current row are simply pushed to the next row. So in this case with a container that fits six-characters-per-row it has to breakan eight-character word, like "magazine", but it would never break a shorter word like "text".
这就是我能找到的全部。我不确定它是否有帮助,但我想我会将其添加到其中。
自动换行
该属性指定如果内容超出元素指定渲染框的边界,当前渲染的行是否应该换行(这在某些方面类似于“clip”和“overflow”)意图中的属性)。仅当元素具有视觉呈现、是具有明确高度/宽度的内联元素、绝对定位和/或是块元素时,才应应用此属性。
Word-break
此属性控制单词内的换行行为。当一个元素中使用多种语言时,它特别有用。
This is all I can find out. I am not sure if it helps, but I thought I'd add it to the mix.
Word-wrap
This property specifies whether the current rendered line should break if the content exceeds the boundary of the specified rendering box for an element (this is similar in some ways to the ‘clip’ and ‘overflow’ properties in intent). This property should only apply if the element has a visual rendering, is an inline element with explicit height/width, is absolutely positioned and/or is a block element.
Word-break
This property controls the line breaking behavior within words. It is especially useful in cases where multiple languages are used within an element.
word-break:break-all
:word是继续到边界然后换行。
word-wrap:break-word
:首先换行,然后继续到边框。
例子:
word-break: break-all
:word is to continue to the border and then break in newline.
word-wrap: break-word
:At first, word wrap in newline and then continue to the border.
Example:
来自相应的 W3C 规范——由于缺乏上下文 - 可以推断出以下内容:
word-break:break-all
用于分解外来的、非CJK(说西方)CJK(中文、日文或韩文)字符写作中的单词。word-wrap:break-word
用于非混合语言(我们只说西方语言)中的断词。至少,这是 W3C 的意图。实际发生的事情是由于浏览器不兼容而导致的重大问题。这是 对各种问题的精彩撰写涉及。
下面的代码片段可以总结一下如何在跨浏览器环境下使用CSS实现自动换行:
From the respective W3C specifications—which happen to be pretty unclear due to a lack of context—one can deduce the following:
word-break: break-all
is for breaking up foreign, non-CJK (say Western) words in CJK (Chinese, Japanese or Korean) character writings.word-wrap: break-word
is for word breaking in a non-mixed (let us say solely Western) language.At least, these were W3C's intentions. What actually happened was a major cock-up with browser incompatibilities as a result. Here is an excellent write-up of the various problems involved.
The following code snippet may serve as a summary of how to achieve word wrapping using CSS in a cross-browser environment:
除了之前的评论之外,浏览器还支持
word -wrap
似乎比断字
。In addition to the previous comments, browser support for
word-wrap
seems to be a bit better than forword-break
.word-break 和 word-wrap 的定义很容易让你头晕,但是当具体比较这两个时,更容易这样思考它们:
首先,您可能还想使用overflow: auto;,并且您可能想尝试单独使用它的样子:如果您可以容忍需要滚动容器中的文本而不是而不是任意的换行位置,它可能正是您所需要的。
然后请记住,在这种情况下,“单词”是一个没有空格的字符串。
word-break:break-all;
在保持任何单词完整之前,优先考虑最大限度地减少浪费的空间,同时避免溢出,因此除了右边距之外,它不会在任何地方换行。它甚至用所包含文本中的空格替换换行符。如果您想尽可能避免滚动并使用足够宽的容器以提高可读性,那么这很有用:但是,如果您的容器太窄,结果通常不太令人满意,如 Drkawashima 指出。
自动换行/溢出换行:换行;
优先保持所有单词完整,同时避免溢出,因此,如果单词太长而无法容纳该行的其余部分,它会首先换行并尝试将其余文本容纳在下一行,即使这意味着离开该行上面只有一个字符。如果您希望获得最大的可读性,同时尽可能避免滚动并使用足够宽的容器:否则您可能只想使用 overflow: auto 来代替。
关于word-wrap,它并没有真正被取代(并且可能比overflow-wrap更被全世界使用的浏览器所认可):它成为<的别名em>overflow-wrap 因为所有大型浏览器和许多网页都已经采用了word-wrap,尽管最初并未在标准中定义。
然而,由于广泛使用,它在定义overflow-wrap时并没有被丢弃,而是被定义为现在的别名,并且由于遗留原因,UA(用户代理,例如网络浏览器)必须将自动换行视为遗留overflow-wrap 属性的名称别名。因此,它已经成为 W3C 事实上的标准,并且不会很快消失(也许当 W3C 标准消失或整个网络被带有 人工智能)。
5.5。溢出换行:overflow-wrap/word-wrap 属性
溢出包裹 (MDN)
The definitions alone of word-break and word-wrap can easily make your head spin, but when comparing these two, specifically, it's much easier to think of them like this:
First of all, you would probably like to use overflow: auto; as well, and you might want to try what using that alone looks like: if you can tolerate needing to scroll the text in the container rather than having arbitrary wrap positions, it might be just what you need.
Then keep in mind that in this context, a "word" is a string with no whitespaces in it.
word-break: break-all;
Prioritizes minimizing the space wasted while avoiding overflow, before keeping any words unbroken, so it never wraps anywhere but at the right margin. It even replaces line breaks with spaces in the contained text. This is useful if you want to avoid scrolling as much as possible and use a container that is enough wide for readability: however if your container is too narrow the result is in general not very satisfying, as Drkawashima noted.
word-wrap/overflow-wrap: break-word;
Prioritizes keeping any and all words unbroken while avoiding overflow, so if a word is too long to fit on the rest of the line, it wraps first and tries to fit the rest of the text on the next line even if it means leaving the line above as short as only one single character. This is useful if you want maximum readability while avoiding scrolling as much as possible and use a container that is enough wide: otherwise you might want to use only overflow: auto instead.
Regarding word-wrap, it isn't really replaced (and maybe also more universally recognized than overflow-wrap by the browsers in use worldwide): it became an alias for overflow-wrap because all the big browsers and many many webpages had already adopted word-wrap although originally not being defined in the standards.
However, because of the widespread use, it wasn't discarded when overflow-wrap was defined, but rather defined as the alias it is today, and for legacy reasons, UAs (User-Agents, e.g., web browsers) must treat word-wrap as a legacy name alias of the overflow-wrap property. So it has become a de facto standard of W3C and it isn't going away any day soon (perhaps when the W3C standard becomes extinct or the entire web is universally updated by bots with AI).
5.5. Overflow Wrapping: the overflow-wrap/word-wrap property
overflow-wrap (MDN)
溢出换行:断字;
参考
overflow-wrap >(MDN)
overflow-wrap: break-word;
Reference
overflow-wrap (MDN)
CSS 中的 word-break 属性用于指定单词到达行尾时应如何断开或分割。 word-wrap 属性用于分割/分解长单词并将它们换行到下一行。
“word-break:break-all;”和“word-wrap:break-word;”之间的区别:
word-break:break-all; :用于在任意字符处断词,防止溢出。
word-wrap:break-word;:用于在任意点处断开单词以防止溢出。
The word-break property in CSS is used to specify how a word should be broken or split when reaching the end of a line. The word-wrap property is used to split/break long words and wrap them into the next line.
Difference between the “word-break: break-all;” and “word-wrap: break-word;”:
word-break: break-all;: It is used to break the words at any character to prevent overflow.
word-wrap: break-word;: It is used to break the words at arbitrary points to prevent overflow.
word-wrap:break-word
最近更改为overflow-wrap:break-word
word-break:break-all
因此,如果您有许多动态获取内容的固定大小的跨度,您可能更喜欢使用 word-wrap: break-word ,因为这样只有连续的单词之间会被打断,如果是一个包含许多单词的句子,则调整空格以获得完整的单词(单词内没有打断)。
如果不重要的话,两者都可以。
word-wrap: break-word
recently changed tooverflow-wrap: break-word
word-break: break-all
So if you have many fixed-size spans which get content dynamically, you might just prefer using
word-wrap: break-word
, as that way only the continuous words are broken in between, and in case it’s a sentence comprising many words, the spaces are adjusted to get intact words (no break within a word).And if it doesn’t matter, go for either.
讨论这些的 W3 规范似乎建议
分词: break-all
用于要求 CJK(中文、日文和韩文)文本具有特定行为,而word-wrap:break-word
是更通用的、不支持 CJK 的, 行为。The W3 specification that talks about these seem to suggest that
word-break: break-all
is for requiring a particular behaviour with CJK (Chinese, Japanese, and Korean) text, whereasword-wrap: break-word
is the more general, non-CJK-aware, behaviour.使用
word-break
,一个非常长的单词从它应该开始的位置开始,并且根据需要将其断开:但是,使用
word-wrap
,一个非常长的单词长单词不会从它应该开始的地方开始。它会换行到下一行,然后根据需要中断
With
word-break
, a very long word starts at the point it should start, and it is being broken as long as required:However, with
word-wrap
, a very long word will not start at the point it should start.It wraps to the next line and then being broken as long as required