javascript 关于 HTML 源代码中的空格和换行符

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4714619/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-25 14:37:28  来源:igfitidea点击:

About the space and line-break in HTML source code

javascripthtmlcss

提问by lovespring

Sometimes, the space and line-break between two HTML tag will ignored by the browser, sometimes not.

有时,浏览器会忽略两个 HTML 标记之间的空格和换行符,有时则不会。

I feel that space and line-break between two line box show, two block box don't show.but how about one line box and one block box?

我感觉两个行框之间的空格和换行符显示,两个块框不显示。但是一个行框和一个块框如何?

I want the rule or specification, could you tell me?

我想要规则或规范,你能告诉我吗?

采纳答案by Zak

In general, browsers ignore extra white space (extra being more than one space).

通常,浏览器会忽略额外的空格(额外的空格不止一个)。

If you have at least one space between two inline elements, then the browser will render one space (but only one space no matter how many you have). If you want more than one space to display, use the html character code  . That will render as many spaces as you need. (although as Pointy pointed out, it is much better to use CSS for spacing)

如果两个内联元素之间至少有一个空格,那么浏览器将渲染一个空格(但无论您有多少个空格,都只会渲染一个空格)。如果要显示多个空格,请使用 html 字符代码 。这将根据您的需要渲染尽可能多的空间。(虽然正如 Pointy 指出的那样,使用 CSS 进行间距要好得多)

As for line breaks, some browsers (such as firefox) will treat a line break the same as a space (where one space will be added no matter how many line breaks you have). Other browsers, however, (such as internet explorer) will ignore the line break completely. To force browsers to render a line break, use the <br>tag.

至于换行,一些浏览器(比如firefox)会把换行和空格一样处理(不管你有多少换行,都会添加一个空格)。但是,其他浏览器(例如 Internet Explorer)将完全忽略换行符。要强制浏览器呈现换行符,请使用<br>标签。

I hope that cleared everything up!

我希望清除一切!

回答by Stefaan Colman

The specs say that all spaces, tabs and line-breaks are interpreted as a word separator. So no matter how many spaces, tabs and line-breaks you have it will only be rendered as a single space.

规范说所有空格、制表符和换行符都被解释为单词分隔符。因此,无论您有多少空格、制表符和换行符,它都只会呈现为一个空格。

The only exception is the pre-tag. The spaces, line-breaks and tabs in a pre-tag are all shown. You can style every tag as a pre-tag by applying the rule white-space: pre.

唯一的例外是前置标签。预标签中的空格、换行符和制表符都显示出来。您可以通过应用规则white-space: pre将每个标签设置为预标签。

Specs: http://www.w3.org/TR/html401/struct/text.html#h-9.1

规格:http: //www.w3.org/TR/html401/struct/text.html#h-9.1

For showing line-breaks the browser checks the type of the content (inline or block). If it's inline (strong, em, a, ...) it doesn't show an line-break. If it's a block (p, table, div, ...) it shows a line-break before and after the element.

为了显示换行符,浏览器会检查内容的类型(内联或块)。如果它是内联的 (strong, em, a, ...) 它不会显示换行符。如果它是一个块 (p, table, div, ...),它会在元素前后显示一个换行符。

Again with css you can change the block-type (with the displayproperty) and so alter how your site looks.

再次使用 css,您可以更改块类型(使用display属性),从而更改站点的外观。