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

jQuery plugin to implement floating menu

Learn a jQuery plugin every day - floating menu, ...

Example code for css3 to achieve scroll bar beautification effect

The specific code is as follows: /*Scroll bar wid...

MySql Installer 8.0.18 Visual Installation Tutorial with Pictures and Text

Table of contents 1. MySQL 8.0.18 installation 2....

How to bind Docker container to external IP and port

Docker allows network services to be provided by ...

js to achieve waterfall flow layout (infinite loading)

This article example shares the specific code of ...

Introduction to Javascript DOM, nodes and element acquisition

Table of contents DOM node Element node: Text nod...

Implementation of dynamic particle background plugin for Vue login page

Table of contents The dynamic particle effects ar...

jQuery canvas generates a poster with a QR code

This article shares the specific code for using j...

How to restore single table data using MySQL full database backup data

Preface When backing up the database, a full data...

SVG button example code based on CSS animation

The specific code is as follows: <a href="...

Rainbow button style made with CSS3

Result: Implementation code: html <div class=&...

Detailed explanation of several methods of installing software in Linux

1. RPM package installation steps: 1. Find the co...

Detailed explanation of Nginx reverse proxy example

1. Reverse proxy example 1 1. Achieve the effect ...