For a chat about HTML & lt; pre & gt; tags, we often use pre tags when we want to maintain the text format. For example, when we want to display the source code, you only need to put a pre Tag, copy and paste the source code, and maintain the format on the page. The line breaks and spaces are not automatically folded as they are placed in other labels. How does pre work?
Pre Tag
Pre elements in HTML, which can define pre-formatted text. The text in the pre element retains spaces and line breaks. The text is displayed as an equal-width font.
Let's look at an example. Here I am using a piece of css code, and you can change it to another one. As follows:
body{ background:#fff; font: 12px/24px 1.66;}
When we wrap them with pre
body{ background:#fff; font: 12px/24px 1.66;}
Will be directly obtained in the browser
We can see that the preceding tabs, spaces, and line breaks are completely retained.
We can put this css code under other elements, as shown in the figure below.
Perfect. You can use pre to identify the code in the future. But there are still some optimizations.
Semantic
The pre element cannot be put into the code. The content can be the lyrics, code, or other texts. When pre elements are used to display the source code, the best way is to use the code element to wrap the code, so that the format and semantics can be maintained and measured in one fell swoop. The code above can be rewritten:
body{ background:#fff; font: 12px/24px 1.66;}
Nesting other html tags
It is recommended that you do not include tags (such as p and title) that can lead to paragraph disconnection in pre. Although mainstream browsers have no problems with this resolution, it is recommended that you do not use it like this. There is a situation where the semantics is unknown. For example, you cannot determine whether to display different structures or display tags as part of the code. It is best to include only the code text here, escape tags, for example, '> '.
The text allowed in the pre element can include physical styles and content-based style changes, as well as links, images, and horizontal separation lines. When other labels are placed in the pre block, they are directly rendered as normal elements.
Example:
[Ti: Fan renge] [ar: Li zongsheng] [al: Fan renge] [] (music) [] You and I are all mortal, born in the world; [] the day is full of hardships, not idle for a moment; [] since it is not a fairy, it is inevitable that there are distractions; [] morality on both sides, the word in the middle.
The displayed style is as follows:
Problem text too long, Overflow
If the text we put in pre is too long and there is no line break in the middle, because pre will keep the text format, resulting in text overflow.
The demo is as follows:
[Ti: Fan renge] [ar: Li zongsheng] [al: Fan renge] [] (music) [] You and I are all mortal, born in the world; [] the day is full of hardships, not idle for a moment; [] since it is not a fairy, it is inevitable that there are distractions; [] morality on both sides, the word in the middle.
Browser Performance
Solution 1: Define a horizontal scroll bar for the pre label
pre{ overflow:auto;}
Solution 2: Use text-wrapping
Directly define the value of the css attribute white-space in the pre Tag as pre-wrap.
pre{ white-space:pre-wrap;}
Rendering html elements
As mentioned above, html elements are directly parsed In the pre Tag. If we want to display these tags, we only need to convert these special symbols into symbolic entities. For example, "<" indicates "<", ">" indicates "> ".
<Ul class = "main-list"> <li> <a href = "#"> the Tibet new line is the Tibet new line. </a> </li> <li> <a href = "#"> the new Tibet line is the top entry and exit line for Tibet. </a> </li> <a href = "#"> the new Tibet line is tibet's entry and exit </a> </li> </ul>
You can also use some online tools to complete the escape process. Here, you can use Baidu to find a screenshot.
Unexpected Space
Sometimes when we copy the Code directly to the page, the editor will automatically indent and align it. This is actually good, but there is a problem with the pre Tag, for example:
<Ul class = "main-list"> <li> <a href = "#"> the Tibet new line is the Tibet new line. </a> </li> <li> <a href = "#"> the new Tibet line is the top entry and exit line for Tibet. </a> </li> <a href = "#"> the new Tibet line is tibet's entry and exit </a> </li> </ul>
The result is as follows:
We recommend that you use the following format to typeset the code.
The first line // The intermediate code to format the last line
Expand to define the tab size
pre{ tab-size:2;}
However, this IE browser does not support very well. Select as needed.
Use an equal-width font
It is easy to typeset and looks comfortable.
Use syntax highlighting
We need to use some libraries, such as highlight. js. How to use them.
Summary
The above is just a small summary. If there are any other methods of application, or the Black technology that is not mentioned here, you can put it forward in the comments and discuss it together.