我们经常会在要保持文本格式的时候使用pre标签,比如当我们要展示源代码的时候,只要放一个pre标签,然后把源代码直接复制,粘贴,然后在页面上就可以保持好格式。不会像放在其它标签里那样,把换行和空格都自动折叠了。这里看一下pre是如何工作的呢?
pre标签
HTML里的pre元素,可定义预格式化的文本。在pre元素中的文本会保留空格和换行符。文本显现为等宽字体。
下面我们看一个示例,这里我使用的是一段css代码,你也可以换成其它的。如下:
body{ background:#fff; font: 12px/24px 1.66; }
当我们用pre包裹它们时
<pre class="brush:php;toolbar:false">body{ background:#fff; font: 12px/24px 1.66; }
会在浏览器上直接得到 tag" alt="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" style="max-width:90%" style="max-width:90%"><br>可以看出上面的tab,空格,换行都完整的保留下来了。<br>我们可以把这段css代码放到其它元素下,如得到下面的图。<br><img src="/static/imghwm/default1.png" data-src="https://img.php.cn/upload/article/000/000/194/e9522b7619e9a198e14b9b77d7341a8c-1.jpg" class="lazy" title="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" alt="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" style="max-width:90%" style="max-width:90%"><br>很完美,以后就可以用pre来标识代码了。哪里想用放哪里,但这里还有一些可以优化。</p>
<h3 id="a-href-http-www-php-cn-html-html-yuyihua-html-target-blank-语义化-a"><a href="http://www.php.cn/html/html-yuyihua.html" target="_blank">语义化</a></h3>
<p>pre元素并不能代码放入,里面的内容是什么,可以是歌词,可以是代码,可以是其它文本。当pre元素来展示源代码的时候最好的方式是用code元素来包裹代码,这样既可以保持格式又可以代表语义,一举数得。如上面的代码可以改写为:</p><pre class='brush:php;toolbar:false;'><pre class="brush:php;toolbar:false">
<code>body{
background:#fff;
font: 12px/24px 1.66;
}</code>
嵌套html其它标签
pre中最好不要包含可以导致段落断开的标签(如:p,标题),虽然主流浏览器对此解析没有问题,但最好不要这样使用。存在着语义不明的情况,比如无法判断是想显示结构的不同展示,还是想把标签作为代码的一部分显示,最好对这里只包含代码文本,对于标签进行转义如'>'对应'>'。
pre元素中允许的文本可以包括物理样式和基于内容的样式变化,还有链接、图像和水平分隔线。当把其它标签放到pre块中时,会被直接渲染为正常元素。
示例如下:
<pre class="brush:php;toolbar:false">[ti:凡人歌] [ar:李宗盛] [al:凡人歌] [00:00](music) [00:28]你我皆凡人,生在人世间; [00:35]终日奔波苦,一刻不得闲; [00:43]既然不是仙,难免有杂念; [00:50]<a href="#">道义放两旁</a>,利字摆中间。
显示出来样式如下: tag" alt="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" style="max-width:90%" style="max-width:90%"></p>
<h2 id="问题">问题</h2>
<h3 id="文本过长时-溢出">文本过长时,溢出</h3>
<p>如果我们在pre里放置的文本过长,中间也没有换行,由于pre会保持文本的格式,导致文本溢出。<br>演示如下:</p><pre class='brush:php;toolbar:false;'><p style="width:500px; background:red; padding:20px;">
<pre class="brush:php;toolbar:false">[ti:凡人歌][ar:李宗盛][al:凡人歌][00:00](music)[00:28]你我皆凡人,生在人世间;[00:35]终日奔波苦,一刻不得闲;[00:43]既然不是仙,难免有杂念;[00:50]<a href="#">道义放两旁</a>,利字摆中间。
浏览器中的表现 tag" alt="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" style="max-width:90%" style="max-width:90%"><br><strong>解决方法1</strong>:给pre标签定义横向滚动条</p><pre class='brush:php;toolbar:false;'>pre{
overflow:auto;
}
解决方法2:使用text-wrapping
直接定义pre标签里的css属性white-space的值为pre-wrap。
pre{ white-space:pre-wrap; }
渲染html元素
上面已经提到过,html元素会在pre标签中直接被解析。如果我们想显示这些标签,只要把这些特殊符号转换为符号实体,就可以了。如: "" 代表 ">"。
<pre class="brush:php;toolbar:false"><code><ul> <li><a>藏新线才是最西藏的进、出线</a></li> <li><a>藏新线才是最西藏的进、出线</a></li> <li><a>藏新线才是最西藏的进、出线</a></li> </ul></code>
也可以使用一些线上的工具去完成这个转义的过程,这里可以百度一下,随便找了一个截了个图 tag" alt="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" style="max-width:90%" style="max-width:90%"></p>
<h3 id="意外的空格">意外的空格</h3>
<p>有时候我们把代码直接复制到页面时,<a href="http://www.php.cn/code/6852.html" target="_blank">编辑器</a>会给我们自动缩进对齐,这里其实是很好的,但这里遇到pre标签就有麻烦了,比如:</p><pre class='brush:php;toolbar:false;'><p>
<pre class="brush:php;toolbar:false"><code><ul class="main-list">
<li><a href="#">藏新线才是最西藏的进、出线</a></li>
<li><a href="#">藏新线才是最西藏的进、出线</a></li>
<li><a href="#">藏新线才是最西藏的进、出线</a></li>
</ul></code>
结果效果如下: tag" alt="A brief introduction to the html <pre class="brush:php;toolbar:false"> tag" style="max-width:90%" style="max-width:90%"></p>
<p>建议使用下面的形式来对代码进行排版。</p><pre class='brush:php;toolbar:false;'><p>
<pre class="brush:php;toolbar:false"><code>第一行
//中间代码进行格式化
最后一行</code>
扩展
定义一下tab的大小
pre{ tab-size:2; }
但这个IE浏览器支持情况不太好。视情况来选择吧。 tag" alt="A brief introduction to the html
tag" style="max-width:90%" style="max-width:90%">Using fixed-width fonts
can facilitate typesetting and looks more comfortable.
Using syntax highlighting
This needs to be done with the help of some libraries, such as highlight.js. How to use it, please Baidu.
Summary
The above are just some small summaries. If there are any other applications or black technologies not mentioned here, you can mention them in the comments and discuss them together.
The above is the detailed content of A brief introduction to the html tag. For more information, please follow other related articles on the PHP Chinese website!

The function of HTML is to define the structure and content of a web page, and its purpose is to provide a standardized way to display information. 1) HTML organizes various parts of the web page through tags and attributes, such as titles and paragraphs. 2) It supports the separation of content and performance and improves maintenance efficiency. 3) HTML is extensible, allowing custom tags to enhance SEO.

The future trends of HTML are semantics and web components, the future trends of CSS are CSS-in-JS and CSSHoudini, and the future trends of JavaScript are WebAssembly and Serverless. 1. HTML semantics improve accessibility and SEO effects, and Web components improve development efficiency, but attention should be paid to browser compatibility. 2. CSS-in-JS enhances style management flexibility but may increase file size. CSSHoudini allows direct operation of CSS rendering. 3.WebAssembly optimizes browser application performance but has a steep learning curve, and Serverless simplifies development but requires optimization of cold start problems.

The roles of HTML, CSS and JavaScript in web development are: 1. HTML defines the web page structure, 2. CSS controls the web page style, and 3. JavaScript adds dynamic behavior. Together, they build the framework, aesthetics and interactivity of modern websites.

The future of HTML is full of infinite possibilities. 1) New features and standards will include more semantic tags and the popularity of WebComponents. 2) The web design trend will continue to develop towards responsive and accessible design. 3) Performance optimization will improve the user experience through responsive image loading and lazy loading technologies.

The roles of HTML, CSS and JavaScript in web development are: HTML is responsible for content structure, CSS is responsible for style, and JavaScript is responsible for dynamic behavior. 1. HTML defines the web page structure and content through tags to ensure semantics. 2. CSS controls the web page style through selectors and attributes to make it beautiful and easy to read. 3. JavaScript controls web page behavior through scripts to achieve dynamic and interactive functions.

HTMLisnotaprogramminglanguage;itisamarkuplanguage.1)HTMLstructuresandformatswebcontentusingtags.2)ItworkswithCSSforstylingandJavaScriptforinteractivity,enhancingwebdevelopment.

HTML is the cornerstone of building web page structure. 1. HTML defines the content structure and semantics, and uses, etc. tags. 2. Provide semantic markers, such as, etc., to improve SEO effect. 3. To realize user interaction through tags, pay attention to form verification. 4. Use advanced elements such as, combined with JavaScript to achieve dynamic effects. 5. Common errors include unclosed labels and unquoted attribute values, and verification tools are required. 6. Optimization strategies include reducing HTTP requests, compressing HTML, using semantic tags, etc.

HTML is a language used to build web pages, defining web page structure and content through tags and attributes. 1) HTML organizes document structure through tags, such as,. 2) The browser parses HTML to build the DOM and renders the web page. 3) New features of HTML5, such as, enhance multimedia functions. 4) Common errors include unclosed labels and unquoted attribute values. 5) Optimization suggestions include using semantic tags and reducing file size.


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools