html中出现连续空格的规则是什么?

发布于 2024-11-29 13:39:54 字数 192 浏览 0 评论 0原文

我认为任何时候你想要在 html 中显示多个空格字符时 &nbsp 都是必要的,但是 这个网站 向我显示多个连续的空格都被渲染了......为什么呢?

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

幸福%小乖 2024-12-06 13:39:54

如果您仔细查看源代码(在浏览器解释之前),您会发现该网站实际上在网站中大量使用  

示例(在 <强>ONE FOR THE ROAD行)

<span style="line-height: 23px;"class="style_3">
                        
</span>

<span style="line-height: 23px; " class="style_1">
    ONE FOR THE ROAD
</span>

尝试将页面保存到您的计算机(使用浏览器的保存功能,而不是复制粘贴),然后在记事本或类似的文本编辑器中查看源代码,您将看到  实体无处不在。

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)

<span style="line-height: 23px;"class="style_3">
                        
</span>

<span style="line-height: 23px; " class="style_1">
    ONE FOR THE ROAD
</span>

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.

驱逐舰岛风号 2024-12-06 13:39:54

一种更简洁的方法是使用 CSS white-space 属性。

将值设置为 prepre-wrappre-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 or pre-wrap or pre-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/

泪痕残 2024-12-06 13:39:54

常规空格 () 换行到下一行。
不间断空格   可以防止换行到下一行。

假设 | |是 div 的两侧:

这是常规空间的渲染方式:

| |    
| |
| |
| |

但是不间断的空间将在同一行上继续。在 HTML 中,这将导致空间所在的元素超出其 auto 宽度。

|  |    
| |
| |
| |

这将依次推出该侧的整个宽度:

|  |    
|  |
|  |
|  |

将呈现多个空白字符,但它们的执行方式与不间断空白不同。

看看这个jsFiddle,它会消除任何混乱。

A regular space () wraps to the next line.
A non-breaking space   prevents wrapping to the next line.

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.

余厌 2024-12-06 13:39:54

@Damon

  是用于表示不间断空格的实体。它本质上是一个空格,主要区别在于浏览器不应在该实体占据的位置中断(或换行)文本行,它还可以防止将多个连续的空白字符折叠成单个空格,这就是当前浏览器的标准行为。

当您查看提到的网站的来源时,您可以看到该网站实际上正在使用   实体来格式化文本:

<span class="style_3" style="line-height: 23px; ">                     </span>

@Damon

  is the entity used to represent a non-breaking space. It is essentially a space, the primary difference being that a browser should not break (or wrap) a line of text at the point that this entity occupies, it also prevents the collapsing of multiple consecutive whitespace characters into a single space, which is the current browsers standard behaviour.

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:

<span class="style_3" style="line-height: 23px; ">                     </span>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文