跳到主要内容

计算机代码元素

code 元素如何使用?​

  • 语义: 表示一段代码、函数名或标识符;
  • 渲染: 等宽字体;
<p>The function <code>selectAll()</code> highlights all the text in the input field.</p>

pre 元素如何保留空白?​

  • 语义: 预格式化文本, 源码中的空格与换行原样渲染;
  • 对比: 普通元素会折叠连续空格与换行, 见 HTML 语法基础;
  • 场景: 需要固定排版的文本, 如代码块、ASCII 图;
<pre>
line 1
line 2 (indented)
</pre>

var 元素表示什么?​

  • 语义: 表示程序中的变量、数学符号;
  • 渲染: 默认斜体;
<p>
The volume of a box is <var>l</var> × <var>w</var> × <var>h</var>, where <var>l</var> represents
the length, <var>w</var> the width and <var>h</var> the height.
</p>

kbd 与 samp 元素分别表示什么?​

  • kbd: 键盘按键或用户输入;
  • samp: 程序输出;
<p>Please press <kbd>Ctrl</kbd> + <kbd>Shift</kbd> + <kbd>R</kbd> to re-render the page.</p>
<p>
<samp>Keyboard not found <br />Press F1 to continue</samp>
</p>