为什么HTML要求多个空格在浏览器中显示为一个空格? [英] Why does HTML require that multiple spaces show up as a single space in the browser?

查看:241
本文介绍了为什么HTML要求多个空格在浏览器中显示为一个空格?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很早就认识到,HTML文件中的任何空格都只会显示为一个空格。例如:

 < p>你好。你好。你好。你好。你好< / p为H. 

显示为:

你好。你好。你好。你好。您好。



这很好,就好像您需要预先格式化文本的多个空格,您可以使用< pre>标签。但是是什么原因?更确切地说,为什么在HTML规范中这样做? 解决方案

HTML格式化与应该如何呈现有区别。考虑一下这样的网页:

 < html> 
< body>
< a href =mylink>连结< / a>
< / body>
< / html>

如果使用空格缩进HTML,则链接前面会有几个空格。 p>

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>

displays as:

Hello. Hello. Hello. Hello. Hello.

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?

解决方案

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>

If the HTML was indented using spaces for example, the link would be preceded by several spaces.

这篇关于为什么HTML要求多个空格在浏览器中显示为一个空格?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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