大网数据广告活动
Web 标准与前端开发

HTML 语法与常见任务速查表

· 52CE 我爱测

使用 HTML 时,若能有一种简单的方法记住如何正确使用和应用 HTML 标签,将会非常方便。MDN 提供了丰富的 HTML 参考文档和一套深入的 HTML 指南。但是,许多情况下我们只需要一些快速的提示。这便是速查表的初衷——为常见用法提供一些快速、准确、现成的代码片段。

使用 HTML 时,若能有一种简单的方法记住如何正确使用和应用 HTML 标签,将会非常方便。MDN 提供了丰富的 HTML 参考文档和一套深入的 HTML 指南。但是,许多情况下我们只需要一些快速的提示。这便是速查表的初衷——为常见用法提供一些快速、准确、现成的代码片段。

[!NOTE] HTML 标签应根据其语义价值而非外观使用。完全可以使用 CSS 改变特定标签的外观和特性。因此使用 HTML 时,要花时间留意标签的语义,而非它们的外观。

行内元素

“元素”是网页的一个单独部分。一些元素较大,作为容器包裹较小的元素。一些元素较小,“嵌入”到更大的元素中。默认情况下,“行内元素”会在网页中并排显示。它们会在网页中按需占用宽度,并且水平排列,就像句子中的词语或同行并列摆放的书本。所有行内元素都可以放置在 <body> 元素内。

<table class="standard-table"> <caption>行内元素:用法和示例</caption> <thead> <tr> <th scope="col">用法</th> <th scope="col">元素</th> <th scope="col">示例</th> </tr> </thead> <tbody> <tr> <td>链接</td> <td>a</td> <td id="a-example"> <pre class="brush: html"> <a href="https://example.org"> 至 example.org 的链接</a>。</pre > a-example </td> </tr> <tr> <td>图片</td> <td>img</td> <td id="img-example"> <pre class="brush: html"><img src="beast.png" width="50" /></pre> img-example </td> </tr> <tr> <td>内联容器</td> <td>span</td> <td id="span-example"> <pre class="brush: html"> 用于对元素进行分组:例如,用于<span style="color:blue">添加样式 </span>。</pre > span-example </td> </tr> <tr> <td>强调文本</td> <td>em</td> <td id="em-example"> <pre class="brush: html"><em>我很时髦</em>。</pre> em-example </td> </tr> <tr> <td>斜体文本</td> <td>i</td> <td id="i-example"> <pre class="brush: html"> 用<i>斜体</i>标记短语。</pre > i-example </td> </tr> <tr> <td>粗体文本</td> <td>b</td> <td id="b-example"> <pre class="brush: html">加粗<b>单词或短语</b>。</pre> b-example </td> </tr> <tr> <td>重要文本</td> <td>strong</td> <td id="strong-example"> <pre class="brush: html"><strong>我很重要!</strong></pre> strong-example </td> </tr> <tr> <td>高亮文本</td> <td>mark</td> <td id="mark-example"> <pre class="brush: html"><mark>注意这里!</mark></pre> mark-example </td> </tr> <tr> <td>删除线文本</td> <td>s</td> <td id="s-example"> <pre class="brush: html"><s>我无关紧要。</s></pre> s-example </td> </tr> <tr> <td>下标</td> <td>sub</td> <td id="sub-example"> <pre class="brush: html">H<sub>2</sub>O</pre> sub-example </td> </tr> <tr> <td>小文本</td> <td>small</td> <td id="small-example"> <pre class="brush: html"> 用于表示文档的<small>小字</small>。</pre > small-example </td> </tr> <tr> <td>地址</td> <td>address</td> <td id="address-example"> <pre class="brush: html"> <address>主大街 67 号</address></pre > address-example </td> </tr> <tr> <td>文献引用</td> <td>cite</td> <td id="cite-example"> <pre class="brush: html"> 欲了解更多怪物,请参阅<cite>《怪物书》</cite>。</pre > cite-example </td> </tr> <tr> <td>上标</td> <td>sup</td> <td id="sup-example"> <pre class="brush: html">x<sup>2</sup></pre> sup-example </td> </tr> <tr> <td>内联引用</td> <td>q</td> <td id="q-example"> <pre class="brush: html"><q>我?</q>,她说道。</pre> q-example </td> </tr> <tr> <td>换行</td> <td>br</td> <td id="br-example"> <pre class="brush: html">第 1 行<br>第 2 行</pre> br-example </td> </tr> <tr> <td>换行机会</td> <td>wbr</td> <td id="wbr-example"> <pre class="brush: html"> <div style="width: 200px"> Llanfair<wbr>pwllgwyngyll<wbr>gogerychwyrndrobwllllantysiliogogogoch </div></pre > wbr-example </td> </tr> <tr> <td>日期</td> <td>time</td> <td id="time-example"> <pre class="brush: html"> 用于格式化日期。例如:<time datetime="2020-05-24"

发布于 2020 年 5 月 23 日</time>。</pre

    time-example
  </td>
</tr>
<tr>
  <td>代码格式</td>
  <td>code</td>
  <td id="code-example">
    <pre class="brush: html">

这段文字是正常格式,但<code>这段文字是代码格式</code>。</pre > code-example </td> </tr> <tr> <td>音频</td> <td>audio</td> <td id="audio-example"> <pre class="brush: html"> <audio controls> <source src="/shared-assets/audio/t-rex-roar.mp3" type="audio/mpeg"> </audio> </pre> audio-example </td> </tr> <tr> <td>视频</td> <td>video</td> <td id="video-example"> <pre class="brush: html"> <video controls width="250" src="/shared-assets/videos/flower.webm" > <a href="/shared-assets/videos/flower.webm">下载 WebM 视频</a> </video></pre > video-example </td> </tr> </tbody> </table>

区块元素

相反,“区块元素”,会占用整个网页宽度。它们也会占用网页的一整行;它们无法并列排列。相反,它们会像文章里的段落或像积木塔那样堆叠在一起。

[!NOTE] 由于本速查表仅包含少数几个代表特定结构或具备特殊语义的元素,因此 div 元素被特意排除————因为 div 元素不代表任何内容,也不具备任何特殊语义。

<table class="standard-table"> <thead> <tr> <th scope="col">用法</th> <th scope="col">元素</th> <th scope="col">示例</th> </tr> </thead> <tbody> <tr> <td>简单段落</td> <td>p</td> <td id="p-example"> <pre class="brush: html"> <p>我是一个段落</p> <p>我是另一个段落</p></pre > p-example </td> </tr> <tr> <td>扩展引用</td> <td>blockquote</td> <td id="blockquote-example"> <pre class="brush: html"> 他们说: <blockquote>blockquote 元素表示扩展引用。</blockquote></pre > blockquote-example </td> </tr> <tr> <td>附加信息</td> <td>details</td> <td id="details-example"> <pre class="brush: html"> <details> <summary>HTML 速查表</summary> <p>行内元素</p> <p>区块元素</p> </details></pre > details-example </td> </tr> <tr> <td>无序列表</td> <td>ul</td> <td id="ul-example"> <pre class="brush: html"><ul> <li>我是一个元素</li> <li>我是另一个元素</li> </ul></pre> ul-example </td> </tr> <tr> <td>有序列表</td> <td>ol</td> <td id="ol-example"> <pre class="brush: html"><ol> <li>我是第一个元素</li> <li>我是第二个元素</li> </ol></pre> ol-example </td> </tr> <tr> <td>定义列表</td> <td>dl</td> <td id="dl-example"> <pre class="brush: html"><dl> <dt>一个术语</dt> <dd>术语的定义</dd> <dt>另一个术语</dt> <dd>另一个术语的定义</dd> </dl></pre> dl-example </td> </tr> <tr> <td>水平分割线</td> <td>hr</td> <td id="hr-example"> <pre class="brush: html">之前<hr>之后</pre> hr-example </td> </tr> <tr> <td>文本标题</td> <td> Heading_Elements </td> <td id="h1-h6-example"> <pre class="brush: html"> <h1>这是 1 号标题</h1> <h2>这是 2 号标题</h2> <h3>这是 3 号标题</h3> <h4>这是 4 号标题</h4> <h5>这是 5 号标题</h5> <h6>这是 6 号标题</h6></pre > h1-h6-example </td> </tr> </tbody> </table>

来源:MDN Web Docs / Mozilla 贡献者 · 作者与贡献记录 · CC BY-SA 2.5。离线整理了版式、链接和媒体显示。

资料来源与适用范围

资料来源
MDN Web Docs / Mozilla 贡献者
适用范围
请按文中版本与环境说明核对
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 2.5

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。