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

<span>:内容跨越元素

· 52CE 我爱测

**`<span>`** HTML 元素是一个通用的行级容器,本身不具备特殊含义。它可被用于对元素进行编组,以便于添加样式(通过使用 `class` 或 `id` 属性),或共享属性值(例如 `lang` 属性)。该元素仅应在无其他合适语义元素时使用。`<span>` 与 div 元素非常相似,但是 div 是块级元素,而 `<span>` 是一个行级元素。

<span> HTML 元素是一个通用的行级容器,本身不具备特殊含义。它可被用于对元素进行编组,以便于添加样式(通过使用 class 或 id 属性),或共享属性值(例如 lang 属性)。该元素仅应在无其他合适语义元素时使用。<span> 与 div 元素非常相似,但是 div 是块级元素,而 <span> 是一个行级元素。

HTML Demo: <span>

<p>
  Add the <span class="ingredient">basil</span>,
  <span class="ingredient">pine nuts</span> and
  <span class="ingredient">garlic</span> to a blender and blend into a paste.
</p>

<p>
  Gradually add the <span class="ingredient">olive oil</span> while running the
  blender slowly.
</p>
span.ingredient {
  color: #f00;
}

属性

这个元素只包含全局属性。

示例

示例 1

HTML

<p><span>一些文本</span></p>

结果

示例_1

示例 2

HTML

<li>
  <span>
    <a href="portfolio.html" target="_blank">查看我的作品集</a>
  </span>
</li>

CSS

li span {
  background: gold;
}

结果

示例_2

技术概要

<table class="properties"> <tbody> <tr> <th scope="row"> <a href="/zh-CN/docs/Web/HTML/Guides/Content_categories">内容分类</a> </th> <td> <a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#短语内容" >短语内容</a >、<a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#流式内容" >流式内容</a >。 </td> </tr> <tr> <th scope="row">允许的内容</th> <td> <a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#短语内容" >短语内容</a >。 </td> </tr> <tr> <th scope="row">标签省略</th> <td>不允许,开始标签和结束标签都不能省略。</td> </tr> <tr> <th scope="row">允许的父元素</th> <td> 任何接受<a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#短语内容" >短语内容</a >、<a href="/zh-CN/docs/Web/HTML/Guides/Content_categories#流式内容" >流式内容</a >的元素。 </td> </tr> <tr> <th scope="row">隐含的 ARIA 角色</th> <td> <a href="https://www.w3.org/TR/html-aria/#dfn-no-corresponding-role" >没有对应的角色</a > </td> </tr> <tr> <th scope="row">允许的 ARIA 角色</th> <td>任意</td> </tr> <tr> <th scope="row">DOM 接口</th> <td> HTMLSpanElement </td> </tr> </tbody> </table>

规范

浏览器兼容性

参见

  • HTML div 元素

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

资料来源与适用范围

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

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

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