html中出现连续空格的规则是什么?
我认为任何时候你想要在 html 中显示多个空格字符时  
都是必要的,但是 这个网站 向我显示多个连续的空格都被渲染了......为什么呢?
I thought that  
was necessary any time you wanted more than one space character to show up in html, but this site is showing me that multiple consecutive spaces are all being rendered... why is that?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
如果您仔细查看源代码(在浏览器解释之前),您会发现该网站实际上在网站中大量使用
:
示例(在 <强>ONE FOR THE ROAD行)
尝试将页面保存到您的计算机(使用浏览器的保存功能,而不是复制粘贴),然后在记事本或类似的文本编辑器中查看源代码,您将看到
实体无处不在。
If you look closely at the source (before it is interpreted by a browser), you'll find that the website does in fact use
very heavily in the site:
Example (before the ONE FOR THE ROAD line)
Try saving the page to your computer (with the browser's saving functionality, not copy-paste), and view the source in Notepad or a similar text editor, and you will see the
entity everywhere.
一种更简洁的方法是使用 CSS
white-space
属性。将值设置为
pre
或pre-wrap
或pre-line
将显示多个空格(换行符处理将有所不同) 。这是一篇关于此的好文章: http://www.impressivewebs.com/css-white-space /
A much cleaner way is to use CSS
white-space
property.Having the value set to
pre
orpre-wrap
orpre-line
will all display multiple spaces (line break handling will be different with each).Here's a good post about that: http://www.impressivewebs.com/css-white-space/
常规空格 (
可以防止换行到下一行。
) 换行到下一行。
不间断空格
假设 | |是 div 的两侧:
这是常规空间的渲染方式:
但是不间断的空间将在同一行上继续。在 HTML 中,这将导致空间所在的元素超出其
auto
宽度。这将依次推出该侧的整个宽度:
将呈现多个空白字符,但它们的执行方式与不间断空白不同。
看看这个jsFiddle,它会消除任何混乱。
A regular space (
prevents wrapping to the next line.
) wraps to the next line.
A non-breaking space
Assume the | | are sides of the div:
This is how a regular space will be rendered:
A non-breaking space however will continue on the same line. In HTML, this will cause the element that the space is in to stretch beyond it's
auto
width.This will in turn push out the entire width of that side:
Multiple whitespace characters will render, however they do not perform the same way a non-breaking whitespace does.
Take a look at this jsFiddle, it will clear any confusion up.
@Damon
当您查看提到的网站的来源时,您可以看到该网站实际上正在使用
实体来格式化文本:
@Damon
When you look into the source of the site that you mention, you can see that the site in fact is using the
entity to format the text: