A brief discussion on spaces and blank lines in HTML code

A brief discussion on spaces and blank lines in HTML code

All consecutive spaces or blank lines (newlines) in HTML code are displayed as a single space.

Example 1: (Continuous spaces in text content)

Code

XML/HTML CodeCopy content to clipboard
  1. < p > In this text, there are about ten consecutive spaces entered. </ p >   
Display effect: There is a space between "格" and "大", and it is displayed as just a space.

XML/HTML CodeCopy content to clipboard
  1. In this text, there are about ten consecutive spaces entered.

Example 2: (continuous spaces between codes)

Code

XML/HTML CodeCopy content to clipboard
  1. < span > span is an inline tag </ span >                 There are many spaces between < span > and the previous span element </ span >   
Display effect: The continuous spaces between the two span elements are displayed as the spaces between "签" and "和", with only one space.
XML/HTML CodeCopy content to clipboard
  1. Span is an inline tag and there are many spaces between it and the previous span element

The above two examples prove that consecutive spaces in HTML code will be displayed as one space when displayed, and the remaining redundant spaces will be removed or ignored.

Paragraph text is actually part of the HTML code, but it is inside the p tag, while the space in Example 2 is between the two span tags.


Now that we understand spaces, let's look at blank lines.

Example 3: (blank line in text content)

Code

XML/HTML CodeCopy content to clipboard
  1. < p > In this text, enter consecutive blank lines
  2.   
  3.   
  4.   
  5.   
  6.   
  7. About five lines were entered. </ p >   
Display effect: As we can see, the five blank lines in the text code are displayed as just one space.

XML/HTML CodeCopy content to clipboard
  1. In this text, enter about five consecutive blank lines.
Example 4: (Blank lines between elements/labels). Just replace the spaces in Example 2 with blank lines. The display effect is the same as Example 2. Multiple blank lines will only be displayed as one space.

XML/HTML CodeCopy content to clipboard
  1. < span > span is an inline tag </ span >   
  2.   
  3.   
  4.   
  5.   
  6.   
  7. There are many blank lines between < span > and the previous span element </ span >   
XML/HTML CodeCopy content to clipboard
  1. Span is an inline tag and there are many blank lines between it and the previous span element

Proof: All consecutive spaces or blank lines (newlines) in HTML code will be displayed as one space.

In this case, what should we do if we want to increase the spacing between two characters so that consecutive spaces or blank lines in the code are also displayed as consecutive spaces or blank lines? It’s actually very simple.

Method 1: We can use the pre-formatting tag <pre>, which is applicable to both spaces and blank lines.

XML/HTML CodeCopy content to clipboard
  1. < pre >   
  2. This is
  3. Preformatted text.
  4. It preserves spaces
  5. and line breaks.
  6. </ pre >   
Display Effect
XML/HTML CodeCopy content to clipboard
  1. This is
  2. Preformatted text.
  3. It preserves spaces
  4. and line breaks.

Method 2: We can use the space entity character &nbsp; to replace spaces, and use the line break tag <br/> to replace blank lines. Although this method can achieve the display effect we want, it is not the most search engine friendly way because &nbsp; and <br/> have no semantics in HTML. So it is recommended to use it as little as possible. Also note that &nbsp; must be lowercase and the final semicolon cannot be omitted.

 

Method 3: (Fit spaces) Use full-width spaces

Full-width spaces are interpreted as Chinese characters, so they will not be interpreted as HTML delimiters and can be displayed according to the actual number of spaces.

Question: How to use full-width input method?

Taking Sogou Input Method as an example, we usually use half-width input. If there is a moon icon in the status bar, it means that half-width input is being used. If it is a sun icon, it means that full-width input is being used. You can switch between full-width and half-width by clicking the icon or using the shortcut key Shift+Space (space character).

Half-width input (moon) Full-width input (sun)

Method 4: Use the word-spacing property in CSS style to control the word-spacing property in CSS. The word-spacing property in CSS can change the standard spacing between characters (words). We know that two words in English are separated by spaces, so we can visually think of it this way: word-spacing changes (lengthens or shortens) the width of the space between words.

Method 5: Use the white-space property in the CSS style. This property declares how to handle whitespace within an element.

value describe
normal default. Whitespace is ignored by browsers.
pre Whitespace is preserved by the browser. It behaves similarly to the <pre> tag in HTML.
nowrap The text will not wrap, the text will continue on the same line until a <br> tag is encountered.
pre-wrap Preserve whitespace sequences, but wrap lines normally.
pre-line Collapses sequences of whitespace, but preserves newline characters.

white-space:normal; is normal, just like not setting it, consecutive spaces and blank lines will only display one space.

white-space:nowrap; What does it mean to not wrap? Normally, when our text exceeds the text area, the text will automatically wrap. This setting means that it will not automatically wrap, but will only wrap when it encounters a line break tag<br />

white-space:pre; Same as method 1, the text is output and displayed as is. When the text exceeds the text area, no line break occurs and a scroll bar is generated.

white-space:pre-wrap; retains spaces and blank lines, but automatically wraps when text exceeds the text area.

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

The above is all the content of the editor's brief discussion on spaces and blank lines in HTML code. I hope everyone will support 123WORDPRESS.COM~

Original URL: http://www.cnblogs.com/web-HCJ/p/4543093.html

<<:  MySQL tutorial data definition language DDL example detailed explanation

>>:  Causes and solutions for front-end exception 502 bad gateway

Recommend

How to configure MySQL master-slave synchronization in Ubuntu 16.04

Preparation 1. The master and slave database vers...

Basic knowledge of website design: newbies please read this

Now many people are joining the ranks of website ...

Implementation of MySQL joint index (composite index)

Joint Index The definition of the joint index in ...

How to deal with time zone issues in Docker

background When I was using Docker these two days...

A brief analysis of kubernetes controllers and labels

Table of contents 01 Common controllers in k8s RC...

11 common CSS tips and experience collection

1. How do I remove the blank space of a few pixels...

Docker installation tutorial in Linux environment

1. Installation environment Docker supports the f...

How to modify server uuid in Mysql

Source of the problem: If the slave server is the...

Correct steps to install Nginx in Linux

Preface If you are like me, as a hard-working Jav...

How to implement mobile web page size adaptation

I finally finished the project at hand, and the m...

Detailed explanation of Tomcat's commonly used filters

Table of contents 1. Cross-domain filter CorsFilt...

JavaScript to achieve dynamic color change of table

This article shares the specific code for JavaScr...

Methods and techniques for quickly displaying web page images

1. Use .gifs rather than .jpgs. GIFs are smaller ...

MySQL transaction control flow and ACID characteristics

Table of contents 1. ACID Characteristics Transac...

Example of using rem to replace px in vue project

Table of contents tool Install the plugin Add a ....