Home > Article > Web Front-end > A brief discussion on spaces and blank lines in HTML code
All consecutive spaces or empty lines (newlines) in HTML code will be displayed as a single space.
Example 1: (Continuous spaces in the text content)
Code
<p>这段文本中,输入连续的空格 大概输入了十个。</p>
Display effect: There is a space between "square" and "big" and it is displayed as just one Space.
这段文本中,输入连续的空格 大概输入了十个。
Example 2: (Continuous spaces between codes)
Code
<span>span是一个行内标签</span> <span>和前面的span元素之间隔了很多个空格</span>
Display effect: Continuous spaces between two span elements are displayed as "sign There is only one space between " and ".
span是一个行内标签 和前面的span元素之间隔了很多个空格
The above two examples prove that continuous spaces in the HTML code will be displayed as one space when displayed, and the remaining extra spaces will be removed or ignored.
The paragraph text is actually part of the HTML code, but it is inside the p tag, and the space in Example 2 is between the two span tags.
Understood the spaces, now look at the blank lines, the same reason
Example 3: (blank lines in text content)
Code
<p>这段文本中,输入连续的空行 大概输入了五行。</p>
Display effect: As we can see, the five blank lines in the text code are only displayed as a space.
这段文本中,输入连续的空行 大概输入了五行。
Example 4: (blank lines between elements/labels), just replace the spaces in example 2 with blank lines. The display effect is the same as in example 2, with multiple blank lines. will only be displayed as a space.
<span>span是一个行内标签</span> <span>和前面的span元素之间隔了很多空行</span>
span是一个行内标签 和前面的span元素之间隔了很多空行
Proof: All consecutive spaces or empty lines (line breaks) in HTML code will be displayed as one space.
In this case, what should we do if we want to expand the distance between two characters so that the result of continuous spaces or blank lines in the code is also continuous spaces or blank lines? it's actually really easy.
Method 1: We can use the pre-formatted tag
, whether it is a space or a blank line. <pre class="brush:html;toolbar:false"><pre class="brush:php;toolbar:false"> 这是 预格式文本。 它保留了 空格 和换行。
Display effect
这是 预格式文本。 它保留了 空格 和换行。
Method 2: We can use the space entity character instead of spaces and line breaks The tag
replaces empty lines. Although this method can get the display effect we want, it is not the most friendly to search engines because and
have no semantics in HTML. So it is recommended to use it as little as possible. Also note that must be lowercase, and the semicolon at the end cannot be omitted.
Method three: (suitable for spaces) Use full-width spaces
Full-width spaces are interpreted as Chinese characters, so they will not be interpreted as HTML delimiters. You can follow the actual The number of spaces is displayed.
Question: How to use the full-width input method?
Take Sogou input method as an example. We usually use half-width input. If there is a moon symbol in the status bar, it means that half-width input is being used. If it is a sun symbol, it means that it is using half-width input. It is a full-width input. You can switch between full-width and half-width by clicking on the icon, or by pressing the shortcut key Shift+Space.
Half-angle input (Moon) Full-angle input (Sun)
# Method Four: Use CSS style intervals attribute control. Change the standard spacing between characters (words). We know that two words in English are separated by a space, so we can visually think that word-spacing changes (lengthens or shortens) the width of the space between words. Method 5: Use the white-space attribute in the CSS style. This attribute declares how to handle whitespace characters within the element.Description | |
---|---|
default. White space is ignored by the browser. | |
Blank spaces will be retained by the browser. It behaves like the tag in HTML. |
|
The text will not wrap, the text will continue on the same line until the tag is encountered. |
|
Preserves whitespace sequences, but wraps normally. | |
Combines whitespace sequences, but retains newlines. |