为什么浏览器渲染一个换行符作为空间? [英] Why does the browser renders a newline as space?

查看:218
本文介绍了为什么浏览器渲染一个换行符作为空间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

最长的时间,我想知道为什么浏览器在它们之间存在NewLine时在呈现的HTML元素之间添加了一个空白空间,例如:

 <跨度>你好< /跨度><跨度>世界< /跨度> 

上面的html会输出HelloWorld字符串,而之间没有空格Hello和World,但在以下示例中:

 < span> Hello< / span> 
< span>世界< / span>

上面的html会输出一个Hello World字符串空格之间的你好和世界。



现在,我没有任何问题接受,这只是它的工作方式期间,但有点让我感到困扰的是我始终觉得html元素之间的空格(或换行符)在浏览器将html呈现给用户时并不重要。



所以我的问题是如果有人知道这种行为背后的哲学或技术原因。



谢谢。

解决方案

浏览器在渲染时将多个空白字符(包括换行符)压缩到单个空间。唯一的例外是< pre> 元素或那些具有 CSS属性 white-space 设置为 pre pre-wrap set。 (或者在XHTML中, xml:space =preserve属性。)


For the longest time, I have wanted to understand why the browser adds an empty space between rendered HTML elements when there is a NewLine between them, for example:

<span>Hello</span><span>World</span>

The html above will output the "HelloWorld" string without a space between "Hello" and "World", however in the following example:

<span>Hello</span>
<span>World</span>

The html above will output a "Hello World" string with a space between "Hello" and "World".

Now, I have no problem accepting that this is just the way it works period, but the thing that bugs me a little is that I was always under the impression that spaces (or newlines) between the html elements would not matter at the time when the browser rendered the html to the user.

So my question is if anyone knows what the philosophical or technical reason behind this behavior.

Thank you.

解决方案

Browsers condense multiple whitespace characters (including newlines) to a single space when rendering. The only exception is within <pre> elements or those that have the CSS property white-space set to pre or pre-wrap set. (Or in XHTML, the xml:space="preserve" attribute.)

这篇关于为什么浏览器渲染一个换行符作为空间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆