如果我想在视觉上强制显示两个空格,我可以这样做:
FOO BAR FOO BAR
这会避开 HTML 的空白折叠功能,但它也会导致像这样的换行符:
FOO
BAR FOO
BAR
有没有像常规空间一样的替代品,但确实会中断?这是我要问的伪代码:
FOO
BAR&bsp;&bsp;
FOO
BAR
ps: 我知道这可以用 CSS 来完成。不是我在这里感兴趣的。
最佳答案
SPACE
的呈现宽度( U+0020
) 字符取决于字体,通常为 1/4 em(经常调整)。
这意味着,平均而言,Unicode 的 FOUR-PER-EM SPACE
( U+2005
) 应该有效,因为它被定义为:1/4 em。
重复:foo  bar  
呈现“易碎”foo bar foo bar etc.
(有 2 个“空格”)(在我的 FF 电脑上)。
看看Korpela's unicode spaces overview .
Code Name of the character Width of the character U+0020 SPACE Depends on font, typically 1/4 em, often adjusted U+00A0 NO-BREAK SPACE As a space, but often not adjusted U+1680 OGHAM SPACE MARK Unspecified; usually not really a space but a dash U+180E MONGOLIAN VOWEL SEPARATOR No width U+2000 EN QUAD 1 en (= 1/2 em) U+2001 EM QUAD 1 em (nominally, the height of the font) U+2002 EN SPACE 1 en (= 1/2 em) U+2003 EM SPACE 1 em U+2004 THREE-PER-EM SPACE 1/3 em U+2005 FOUR-PER-EM SPACE 1/4 em U+2006 SIX-PER-EM SPACE 1/6 em U+2007 FIGURE SPACE "Tabular width", the width of digits U+2008 PUNCTUATION SPACE The width of a period "." U+2009 THIN SPACE 1/5 em (or sometimes 1/6 em) U+200A HAIR SPACE Narrower than THIN SPACE U+200B ZERO WIDTH SPACE Nominally no width, but may expand U+202F NARROW NO-BREAK SPACE Narrower than NO-BREAK SPACE (or SPACE) U+205F MEDIUM MATHEMATICAL SPACE 4/18 em U+3000 IDEOGRAPHIC SPACE The width of ideographic (CJK) characters. U+FEFF ZERO WIDTH NO-BREAK SPACE No width (the character is invisible)
请注意 EN SPACE
(= 1/2 em) 因此应该产生相同的视觉最终结果(2 个空格),仅使用一个 unicode 字符(并在 html 源代码中保存 7 个字符!!)
SPACE
之间的区别和 QUAD
是(来自 wikipedia ):
The en quad and the en space are usually synonymous as they both start as a space with a width of 1 en. However, in electronic publishing a few contrast the two by holding that if the font is condensed or expanded to alter the width of the characters, the en quad remains 1 en in width, while the en space is altered in width to the same proportion as the printing characters.
希望这对您有所帮助!
编辑:
我对此做了一些跨浏览器测试,包括同时出现的其他答案。
正如所指出的,我的建议依赖于字体制作者逻辑地/懒惰地得出结论,他的 SPACE
的宽度字符等于 FOUR-PER-EM SPACE
的字符(和 EN QUAD
的一半)。
作为一名程序员,我不喜欢不确定性。
BoltCock 的答案 ( <wbr>
) 和 Wesabi 的答案 ( ​
) 消除了宽度的不确定性。
具有讽刺意味的是,还没有“最佳”之分:我的建议和 Wesabi 的建议 ​
使用旧浏览器 (IE6)/字体不支持的字符;它们将呈现为白色方 block □
.
BoltCock 的 <wbr>
另一方面在 IE6 中完美运行,但存在其他问题:它在 IE8(和其他一些)中不起作用(来源:0、1、2)。
取决于您的具体用例:Chris Nielsen 的评论实际上可能是最好的跨浏览器解决方案:replace one of the entities with a normal space
...
编辑2:
结束语:从语义的角度来看(文本内容本身),您正在与空白折叠功能作斗争(您将其标记为 HTML)。因此,“最佳”解决方案是让您的内容保持原样,并且(正如您自己指出的那样):使用 css 禁用空白折叠功能。
关于HTML 等同于......确实会中断;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24468873/