浏览器是否删除任何 html 标签文本之间的空格

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17784595/
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-08-29 11:24:47  来源:igfitidea点击:

Do browsers remove whitespace in between text of any html tag

htmlbrowserwhitespacetrim

提问by Deeptechtons

Here is a fiddle demonstrating the problem

这是一个演示问题的小提琴

http://jsfiddle.net/WM8XW/

http://jsfiddle.net/WM8XW/

I have inserted many whitespace in the content of the label tag but the html rendered seems to remove it. Is adding  only solution to the above problem

我在标签标签的内容中插入了许多空格,但呈现的 html 似乎将其删除。是添加 上述问题的唯一解决方案

HTML Content

HTML 内容

<label>label with very          long white space in between</label>

回答by Sirko

The normal behavior for the display of whitespaces is to compress them into a single one, which is then displayed.

显示空白的正常行为是将它们压缩成一个,然后显示出来。

There are two exceptions from that:

有两个例外:

  1. The <pre>tag, which keeps the whitespaces as entered.
  2. Setting the CSS property white-space: pre;(respectively pre-wrapor pre-line)
  1. <pre>标签,这使空格作为输入。
  2. 设置 CSS 属性white-space: pre;(分别pre-wrappre-line

回答by Arun Chandran C

Browsers usually treat multiple consecutive spaces and/or carriage returns as a single space. In the case of non-breaking spaces (&nbsp; or &#160;)browsers will typically honor multiple consecutive occurrences as-is with no collapsing to a single space.

浏览器通常将多个连续空格和/或回车符视为一个空格。在不间断空格的情况下,(&nbsp; or &#160;)浏览器通常会按原样尊重多个连续出现,而不会折叠为单个空格。

Solution1:

解决方案1:

You can hard code the (&nbsp; or &#160;)as much as you want.

您可以根据需要对其进行硬编码(&nbsp; or &#160;)

Reference

参考

Any text between the opening <pre>tag and the closing </pre>tag will preserve the formatting of the source document.

开始<pre>标记和结束</pre>标记之间的任何文本都将保留源文档的格式。

Solution2: You can make use of <pre>tag.

解决方案2:您可以使用<pre>标签。

Reference

参考

If you want to execute with cssalso you can perform the same the above answers are only from htmlpoint of view

如果你想用css执行,你也可以执行相同的上述答案仅从html角度来看

回答by bansi

Yes multiple white spaces are converted to one white space by browser. Your only option is to have hard space ie. &nbsp;

是的,浏览器将多个空格转换为一个空格。您唯一的选择是拥有硬空间,即。&nbsp;