<tt>:电传打字机文本元素

Deprecated: This feature is no longer recommended. Though some browsers might still support it, it may have already been removed from the relevant web standards, may be in the process of being dropped, or may only be kept for compatibility purposes. Avoid using it, and update existing code if possible; see the compatibility table at the bottom of this page to guide your decision. Be aware that this feature may cease to work at any time.

<tt> HTML 元素创建使用 user agent's 默认等宽字体呈现的内联文本。创建此元素的目的是为了呈现文本,因为文本将显示在固定宽度的显示器上,例如电传打字机、纯文本屏幕或行式打印机。

¥The <tt> HTML element creates inline text which is presented using the user agent's default monospace font face. This element was created for the purpose of rendering text as it would be displayed on a fixed-width display such as a teletype, text-only screen, or line printer.

术语“非比例”、“单色”和“等宽”可以互换使用,并且具有相同的一般含义:它们描述了一种字体,其字符的像素宽度都是相同的。

¥The terms non-proportional, monotype, and monospace are used interchangeably and have the same general meaning: they describe a typeface whose characters are all the same number of pixels wide.

然而,这个元素已经过时了。对于需要以等宽字体显示的内联文本,你应该使用语义上更有用的 <code><kbd><samp><var> 元素,或者对于应作为单独块显示的内容使用 <pre> 标记。

¥This element is obsolete, however. You should use the more semantically helpful <code>, <kbd>, <samp>, or <var> elements for inline text that needs to be presented in monospace type, or the <pre> tag for content that should be presented as a separate block.

注意:如果没有任何语义元素适合你的用例(例如,如果你需要以非比例字体显示某些内容),则应考虑使用 <span> 元素,并使用 CSS 根据需要对其进行样式设置。font-family 属性是一个很好的起点。

¥Note: If none of the semantic elements are appropriate for your use case (for example, if you need to show some content in a non-proportional font), you should consider using the <span> element, styling it as desired using CSS. The font-family property is a good place to start.

属性

¥Attributes

该元素仅包含 全局属性

¥This element only includes the global attributes

示例

¥Examples

基本示例

¥Basic example

此示例使用 <tt> 显示终端应用输入和输出的文本。

¥This example uses <tt> to show text entered into, and output by, a terminal application.

html
<p>
  Enter the following at the telnet command prompt:
  <code>set localecho</code><br />

  The telnet client should display: <tt>Local Echo is on</tt>
</p>

结果

¥Result

覆盖默认字体

¥Overriding the default font

你可以使用 CSS 覆盖浏览器的默认字体(如果浏览器允许你这样做,但并不要求这样做):

¥You can override the browser's default font—if the browser permits you to do so, which it isn't required to do—using CSS:

CSS

css
tt {
  font-family: "Lucida Console", "Menlo", "Monaco", "Courier", monospace;
}

HTML

html
<p>
  Enter the following at the telnet command prompt:
  <code>set localecho</code><br />

  The telnet client should display: <tt>Local Echo is on</tt>
</p>

结果

¥Result

使用说明

¥Usage notes

默认情况下,<tt> 元素使用浏览器的默认非比例字体呈现。你可以通过使用 tt 选择器创建规则来使用 CSS 覆盖此设置,如上面的示例 覆盖默认字体 所示。

¥The <tt> element is, by default, rendered using the browser's default non-proportional font. You can override this using CSS by creating a rule using the tt selector, as seen in the example Overriding the default font above.

注意:用户配置的对默认等宽字体设置的更改可能优先于 CSS。

¥Note: User-configured changes to the default monospace font setting may take precedence over your CSS.

尽管该元素在 HTML 4.01 中并未被正式弃用,但为了支持语义元素和/或 CSS,不鼓励使用它。<tt> 元素在 HTML 5 中已过时。

¥Although this element wasn't officially deprecated in HTML 4.01, its use was discouraged in favor of the semantic elements and/or CSS. The <tt> element is obsolete in HTML 5.

技术总结

¥Technical summary

内容类别 流量内容措辞内容,可触及的内容。
允许的内容 措辞内容.
标签遗漏 无,开始和结束标记都是强制性的。
允许的父级 任何接受 措辞内容 的元素。
允许的 ARIA 角色 任何
DOM 接口 HTMLElement

规范

Specification
HTML Standard
# tt

¥Specifications

浏览器兼容性

BCD tables only load in the browser

¥Browser compatibility

也可以看看

¥See also