<code>: The Inline Code element

<code> HTML 元素以某种方式显示其内容样式,旨在表明该文本是计算机代码的一小段。默认情况下,内容文本使用 user agent's 默认等宽字体显示。

¥The <code> HTML element displays its contents styled in a fashion intended to indicate that the text is a short fragment of computer code. By default, the content text is displayed using the user agent's default monospace font.

Try it

属性

¥Attributes

该元素仅包含 全局属性

¥This element only includes the global attributes.

示例

¥Examples

包含 <code> 的一段文字:

¥A paragraph of text that includes <code>:

html
<p>
  The function <code>selectAll()</code> highlights all the text in the input
  field so the user can, for example, copy or delete the text.
</p>

结果

¥Result

注意

¥Notes

要表示多行代码,请将 <code> 元素封装在 <pre> 元素内。<code> 元素本身仅代表单个代码短语或代码行。

¥To represent multiple lines of code, wrap the <code> element within a <pre> element. The <code> element by itself only represents a single phrase of code or line of code.

可以为 code 选择器定义 CSS 规则来覆盖浏览器的默认字体。用户设置的首选项可能优先于指定的 CSS。

¥A CSS rule can be defined for the code selector to override the browser's default font face. Preferences set by the user might take precedence over the specified CSS.

技术总结

¥Technical summary

内容类别 流量内容措辞内容,可触及的内容。
允许的内容 措辞内容.
标签遗漏 无,开始和结束标记都是强制性的。
允许的父级 任何接受 措辞内容 的元素。
隐式 ARIA 角色 code
允许的 ARIA 角色 任何
DOM 接口 HTMLElement 从 Gecko 1.9.2 (Firefox 4) 开始,Firefox 为该元素实现了 HTMLSpanElement 接口。

规范

Specification
HTML Standard
# the-code-element

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also