Why is a newline converted into a ' ' in a

2019-03-06 22:14发布

问题:

I have a question related to HTML. In order to demonstrate my simple question, I will use a minimal example.

Consider the following HTML content:

<html>
Foo: Bar 
</html>

When you call this in a browser, it displays "Foo: Bar" in one line. So far so good.

However had, when you do almost the same, and store this:

<html>
Foo
: Bar 
</html>

In other words, if you add a newline right before the ':' character, then suddenly the display becomes this here:

"Foo : Bar"

Now I wonder where from the ' ' comes? Because that character is not part of the original source.

回答1:

In HTML a carriage return or line feed in the source code is treated as white space and rendered as a space. Multiple spaces or white space (CR, LF, tabs, etc.) amount to a single white space on the rendered page.

So if you have 50 carriage returns in your source code between Foo and : Bar it will render a one space (Foo : Bar) when th HTML page is displayed in the browser.

From the HTML 4.01 spec: Controlling line breaks.



标签: html newline