浏览器是否删除任何 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
Do browsers remove whitespace in between text of any html tag
提问by Deeptechtons
Here is a fiddle demonstrating the problem
这是一个演示问题的小提琴
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:
有两个例外:
- The
<pre>
tag, which keeps the whitespaces as entered. - Setting the CSS property
white-space: pre;
(respectivelypre-wrap
orpre-line
)
- 该
<pre>
标签,这使空格作为输入。 - 设置 CSS 属性
white-space: pre;
(分别pre-wrap
或pre-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 ( or  )
browsers will typically honor multiple consecutive occurrences as-is with no collapsing to a single space.
浏览器通常将多个连续空格和/或回车符视为一个空格。在不间断空格的情况下,( or  )
浏览器通常会按原样尊重多个连续出现,而不会折叠为单个空格。
Solution1:
解决方案1:
You can hard code the ( or  )
as much as you want.
您可以根据需要对其进行硬编码( or  )
。
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>
标签。
If you want to execute with cssalso you can perform the same the above answers are only from html
point 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.  
;
是的,浏览器将多个空格转换为一个空格。您唯一的选择是拥有硬空间,即。 
;