Home > Web Front-end > HTML Tutorial > Detailed explanation of code tag and pre tag in html

Detailed explanation of code tag and pre tag in html

黄舟
Release: 2017-07-08 11:33:26
Original
5867 people have browsed it

Preface

A few days ago, a colleague asked me a question about code tags and pre tags. Well, yes, these two tags came together. question. So I summarized it. This article will focus on the code tag and pre tag, their definitions, differences, applications, and how to use these two guys together. And when these two tags are nested, how different browsers interpret this nesting.

1. Nesting order

Code tags and pre tags can be nested. It should be pointed out that a previous version of Firefox parsed the nesting of code tags and pre tags. , improper order will cause the browser to parse the DOM structure incorrectly. This is just a problem of Firefox itself, and the latest version can already parse it correctly. Correct parsing refers to being able to parse the DOM structure correctly. All in all, the nesting order of these two tags can now be arbitrary. There will be no DOM structure parsing errors.

When we write the following code:


##

<pre class="brush:php;toolbar:false">
    <code>
        hello world    </code>
Copy after login

The browser parses it correctly:

## FF browser’s analysis of the above code

2. Regarding the omission rules of pre tags and code tags under

HTML5

The latest HTML5 specification defines tags that can omit the closing tag. For example, the

tag can be written as the

tag, omitting the closing tag. The pre tag and code tag are the same as the p tag. The end tag can be omitted. Under the HTML5 standard, the following is also correct. The parsed result is the same as the above parsed result.

<pre class="brush:php;toolbar:false">
    <code>
          hello world
Copy after login

Firefox, IE9 and Chrome can all parse it correctly. It is recommended that you read the tag omission specification under the HTML5 standard: that is, the above two ways of writing are correct.

But one thing to note is that the default standard specification under IE9 is the picture below.

The default standard under Firefox and Chrome is the picture below.


All in all, they are all correct. The DOM structure is the same. Of course, spaces may be regarded as nodes. I won’t discuss it in depth.

3. Definition of code tag and pre tag

Code tag:

1. Definition of code tag: tag, used to represent computer source code or Text content that can be read by other machines. Software code writers are used to the code format when writing code, so this tag is designed for software code writers. The text in the code tag will use a fixed-width, teletype-like font (Courier). display.

2. The application of code tags should only be used to represent computer program source code or other text content that can be read by machines. The functions of the tag include: changing the text into a fixed-width font; and another function is to imply that this text is source code. Then according to the second function, in the future the browser may add effects according to its own actual situation. For example, the programmer's browser may look for fragments and perform some additional text formatting, such as

loops

and conditional judgmentspecial indentation of statements, etc. . 3. Example of code tag, we can look at the format difference between the text fragment without code tag and the text fragment with code tag below. It should be noted that this only demonstrates the first function of the code tag. As for the function of the source program that hints to the browser, this code needs to be displayed on a specific browser.

The display effect of the following code on the browser is:

1 <code>
2     document.getELementById("id1");
3     document.getELementById("id2");
4     document.getELementById("id3");
5 </code>
6 <br>
7 document.getELementById("id4");
Copy after login

The display effect is:

## You see the difference, haha. Let’s look at the pre tag next.

pre tag:

1、pre标签的定义,

标签用来定义预格式化的文本,被包围在pre标签中的文本通常会保留空格和换行符, 而文本也会呈现出等宽字体。同样,pre标签的一个常见的应用便是用来保存计算机中的源代码文本。</p><p>      2、pre标签的功能,pre标签一个常见的应用便是用来保存计算机中的源代码的文本。但是,需要注意的是, 可以导致段落断开的标签(例如标题、<p> 和 <address> 标签)绝不能包含在 <pre class="brush:php;toolbar:false"> 所定义的块里。 pre标签允许的文本可以包括物理样式和基于内容的样式变化,还有链接、图像和水平分隔线。 当把其他标签(比如 <a> 标签)放到 <br/><pre class="brush:php;toolbar:false"> 块中时,就像放在 HTML/XHTML 文档的其他部分中一样即可。</p><p>      3、pre标签的示例:下面的代码(html中的源代码)</p><p class="cnblogs_code"><br/></p><div class="code" style="position:relative; padding:0px; margin:0px;"><pre class="brush:php;toolbar:false"> 1 <pre class="brush:php;toolbar:false"> 
 2     &lt;html&gt; 
 3     &lt;head&gt; 
 4     &lt;script type=&quot;text/javascript&quot; src=&quot;loadxmldoc.js&quot;&gt; 
 5     &lt;/script&gt; 
 6     &lt;/head&gt; 
 7     &lt;body&gt; 
 8     &lt;script type=&quot;text/javascript&quot;&gt; 
 9     xmlDoc=&lt;<font color="blue">a href="dom_loadxmldoc.asp"&gt;loadXMLDoc&lt;/a&gt;</font>(&quot;books.xml&quot;);
 10     document.write(&quot;xmlDoc is loaded, ready for use&quot;);
 11     &lt;/script&gt;
 12     &lt;/body&gt;
 13     &lt;/html&gt;
 14 
Copy after login

      在浏览器中的显示效果为:

      左边的灰色的线是浏览器视窗的最左端呢,跟代码在文档中距离左边边距的距离是一样的呢。

四、code标签和pre标签之间的关系

      通过定义我们可以知道code标签和pre标签之间的关系,两者的共同点是应用上类似,都主要应用于浏览器显示计算机中的源代码。 但是两者之间还是有很大不同的,code标签的一个功能是暗示浏览器code标签所包围的文本是计算机源代码,浏览器可以做出自己的 样式处理,pre标签则没有这项功能,但是pre标签可以保留文本中的空格和换行符,保留文本中的空格和换行符是计算机源代码显示 所必须的样式。那么这个时候我们可以想象一下,code标签和pre标签可不可以一块使用呢?答案是可以的,code标签和pre标签是 可以嵌套使用的,但是必须注意两者的嵌套顺序。

The above is the detailed content of Detailed explanation of code tag and pre tag in html. For more information, please follow other related articles on the PHP Chinese website!

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