Html " " 和有什么不一样 和 ” ”?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1357078/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 00:45:54  来源:igfitidea点击:

What's the difference between " " and " "?

html

提问by omg

Both of them mean space, but is there any difference?

两者都表示空间,但有什么区别吗?

回答by Brian Rasmussen

One is non-breaking spaceand the other is a regular space. A non-breaking space means that the line should not be wrapped at that point, just like it wouldn't be wrapped in the middle of a word.

一个是不间断空间,另一个是常规空间。不间断空格意味着该行不应在该点换行,就像它不会被换行在单词中间一样。

Furthermore as Svend points out in his comment, non-breaking spaces are not collapsed.

此外,正如 Svend 在评论中指出的那样,不间断空格不会折叠。

回答by Guffa

The entity  produces a non-breaking space, which is used when you don't want an automatic line break at that position. The regular space has the character code 32, while the non-breaking space has the character code 160.

该实体 会产生一个不间断空格,当您不希望在该位置自动换行时使用该空格。常规空格的字符代码为 32,而不间断空格的字符代码为 160。

For example when you display numbers with space as thousands separator: 1 234 567, then you use non-breaking spaces so that the number can't be split on separate lines. If you display currency and there is a space between the amount and the currency: 42 SEK, then you use a non-breaking space so that you don't get the amount on one line and the currency on the next.

例如,当您使用空格作为千​​位分隔符显示数字时:1 234 567,然后您使用不间断空格,以便数字不能在单独的行上拆分。如果您显示货币并且金额和货币之间有一个空格:42 SEK,那么您使用一个不间断空格,这样您就不会在一行中显示金额,而在下一行显示货币。

回答by Graeme Perrow

In addition to the other answers here, non-breaking spaces will not be "collapsed" like regular spaces will. For example, both

除了此处的其他答案外,不间断空格不会像常规空格那样“折叠”。例如,两者

<p>Word1          Word2</p>

and

<p>Word1 Word2</p>

will render the same on any browser, while

将在任何浏览器上呈现相同的效果,而

<p>Word1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Word2</p>

will keep the spaces when rendered.

渲染时将保留空格。

回答by KellCOMnet

Not an answer as much as examples...

不像例子那样有答案......

Example #1:

示例#1:

<div style="width:45px; height:45px; border: solid thin red; overflow: visible">
    Hello&nbsp;There
</div>  

Example #2:

示例#2:

<div style="width:45px; height:45px; border: solid thin red; overflow: visible">
    Hello There
</div>

And link to the fiddle.

并链接到小提琴

回答by alberto-bottarini

You can see a working example here:

你可以在这里看到一个工作示例:

http://codepen.io/anon/pen/GJzBxo

http://codepen.io/anon/pen/GJzBxo

and

http://codepen.io/anon/pen/LVqBQo

http://codepen.io/anon/pen/LVqBQo

Same div, same text, different "spaces"

相同的 div,相同的文本,不同的“空格”

<div style="width: 500px; background: red"> [loooong text with spaces]</div>

vs

对比

<div style="width: 500px; background: red"> [loooong text with &nbsp;]</div>

回答by Gumbo

Multiple normal white space characters (space, tabulator and line break) are treated as one single white space character:

多个普通空白字符(空格、制表符和换行符)被视为一个单独的空白字符

For all HTML elements except PRE, sequences of white space separate "words" (we use the term "word" here to mean "sequences of non-white space characters"). When formatting text, user agents should identify these words and lay them out according to the conventions of the particular written language (script) and target medium.

对于除 之外的所有 HTML 元素PRE,空格序列分隔“单词”(我们在这里使用术语“单词”表示“非空格字符序列”)。在格式化文本时,用户代理应该识别这些词并根据特定书面语言(脚本)和目标媒体的约定进行布局。

So

所以

foo    bar

is displayed as

显示为

foo bar

But no-break space is always displayed. So

但始终显示不间断空格。所以

foo&?nbsp;&?nbsp;&?nbsp;bar

is displayed as

显示为

foo   bar

回答by Pete

As already mentioned, you will not receive a line break where there is a "no-break space".

如前所述,您不会在有“无中断空间”的地方收到换行符。

Also be wary, that elements containing only a " " may show up incorrectly, where &nbsp; will work. In i.e. 6 at least (as far as I remember, IE7 has the same issue), if you have an empty table element, it will not apply styling, for example borders, to the element, if there is no content, or only white space. So the following will not be rendered with borders:

另请注意,仅包含“”的元素可能会显示不正确,其中   将工作。至少在 ie 6 中(据我所知,IE7 有同样的问题),如果你有一个空的表格元素,它不会应用样式,例如边框,到元素,如果没有内容,或者只有白色空间。因此,以下内容不会带有边框:

<td></td>
<td> <td>

Whereas the borders will show up in this example:

而边框将显示在此示例中:

<td>& nbsp;</td>

Hmm -had to put in a dummy space to get it to render correctly here

嗯 - 必须放入一个虚拟空间才能在此处正确渲染

回答by Zoidberg

The first is not treated as white space by the HTML parser, the second is. As a result the " " is not guaranteed to showup within certain HTML markup, while the non breakable space will always show up.

第一个不被 HTML 解析器视为空白,第二个是。因此,不能保证在某些 HTML 标记中显示“”,而不可破坏的空间将始终显示。

回答by CodingBarfield

&nbsp;should be handled as a whitespace.

&nbsp;应该作为空格处理。

&nbsp;&nbsp;should be handled as two whitespaces

&nbsp;&nbsp;应该作为两个空格处理

' ' can be handled as a non interesting whitespace

' ' 可以作为一个不感兴趣的空格处理

' ' + ' ' can be handled as a single ' '

' ' + ' ' 可以作为单个 ' ' 处理

回答by CodingBarfield

&nbsp;is stackable, meaning you can create multiple spaces all together.

&nbsp;是可堆叠的,这意味着您可以一起创建多个空间。

HTML will only parse one space '' and it drops the rest...

HTML 只会解析一个空格 '' 并丢弃其余的...

If you want five spaces, you would place 5 x &nbsp;

如果你想要五个空格,你会放置 5 x &nbsp;