問題描述
我早就認識到 HTML 文件中的任何一組空格都只會顯示為一個空格.例如,這個:
I have long recognized that any set of whitespace in an HTML file will only be displayed as a single space. For instance, this:
<p>Hello. Hello. Hello. Hello. Hello.</p>
顯示為:
你好.你好.你好.你好.你好.
Hello. Hello. Hello. Hello. Hello.
這很好,好像您需要多個預先格式化的文本空格,您可以使用 <pre>標簽.但原因是什么?更準確地說,為什么在 HTML 規范中會出現這種情況?
This is perfectly fine, as if you need multiple spaces of pre-formatted text you can just use the <pre> tag. But what is the reason? More precisely, why is this in the specification for HTML?
推薦答案
HTML 中的空格是壓縮的,因為 HTML 的格式化方式和呈現方式之間存在區別.考慮這樣一個頁面:
Spaces are compacted in HTML because there's a distinction between how HTML is formatted and how it should be rendered. Consider a page like this:
<html>
<body>
<a href="mylink">A link</a>
</body>
</html>
例如,如果 HTML 使用空格縮進,則鏈接前面會帶有多個空格.
If the HTML was indented using spaces for example, the link would be preceded by several spaces.
這篇關于為什么 HTML 要求多個空格在瀏覽器中顯示為單個空格?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!