問(wèn)題描述
我早就認(rèn)識(shí)到 HTML 文件中的任何一組空格都只會(huì)顯示為一個(gè)空格.例如,這個(gè):
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.
這很好,好像您需要多個(gè)預(yù)先格式化的文本空格,您可以使用 <pre>標(biāo)簽.但原因是什么?更準(zhǔn)確地說(shuō),為什么在 HTML 規(guī)范中會(huì)出現(xiàn)這種情況?
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 中的空格是壓縮的,因?yàn)?HTML 的格式化方式和呈現(xiàn)方式之間存在區(qū)別.考慮這樣一個(gè)頁(yè)面:
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 使用空格縮進(jìn),則鏈接前面會(huì)帶有多個(gè)空格.
If the HTML was indented using spaces for example, the link would be preceded by several spaces.
這篇關(guān)于為什么 HTML 要求多個(gè)空格在瀏覽器中顯示為單個(gè)空格?的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!