Do browsers remove whitespace in between text of any html tag

我只是一个虾纸丫 提交于 2019-11-27 09:16:05
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-wrap or pre-line)

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.

Solution1:

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

Reference

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

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

Reference

If you want to execute with css also you can perform the same the above answers are only from html point of view

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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!