A brief discussion on spaces and blank lines in HTML code

高洛峰
Release: 2017-02-18 15:37:01
Original
6174 people have browsed it

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>
Copy after login

Display effect: There is a space between "square" and "big" and it is displayed as just one Space.

这段文本中,输入连续的空格 大概输入了十个。
Copy after login

Example 2: (Continuous spaces between codes)

Code

<span>span是一个行内标签</span>               <span>和前面的span元素之间隔了很多个空格</span>
Copy after login

Display effect: Continuous spaces between two span elements are displayed as "sign There is only one space between " and ".

span是一个行内标签 和前面的span元素之间隔了很多个空格
Copy after login

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>
Copy after login

Display effect: As we can see, the five blank lines in the text code are only displayed as a space.

这段文本中,输入连续的空行 大概输入了五行。
Copy after login

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>
Copy after login
span是一个行内标签 和前面的span元素之间隔了很多空行
Copy after login

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. </p><pre class="brush:html;toolbar:false"><pre class="brush:php;toolbar:false">  
这是  
预格式文本。  
它保留了      空格  
和换行。  
Copy after login


Display effect

这是  
预格式文本。  
它保留了      空格  
和换行。
Copy after login


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)

A brief discussion on spaces and blank lines in HTML code

# 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.


##Valuenormalprenowrappre-wrappre-line


white-space:normal; is normal. Just like if it is not set, only one space will be displayed for consecutive spaces and blank lines.

white-space:nowrap;What does not wrap mean? Under normal circumstances, when our text exceeds the text field, the text will automatically wrap. This setting means that it will not automatically wrap, but will wrap when it encounters the line break tag

white-space:pre; Same as method 1, output and display the text as it is. When the text exceeds the text area, no line breaks will occur and scroll bars will be generated.

white-space:pre-wrap;Retain spaces and blank lines, but when the text exceeds the text field, it will automatically wrap.

white-space:pre-line;Continuous spaces will be displayed as one space, but consecutive blank lines will be retained.

The above is the brief discussion of spaces and blank lines in HTML code brought to you by the editor. I hope everyone will support the PHP Chinese website~

More brief discussion of spaces and blank lines in HTML code For articles related to spaces and blank lines, please pay attention to the PHP Chinese website!


Description
default. White space is ignored by the browser.
Blank spaces will be retained by the browser. It behaves like the
 tag in HTML. 
Copy after login
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.
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template