我将哪个 HTML 标记用于 bash 命令行输入?

Which HTML tag do I use for bash command line input?

假设我正在写一篇文章

<p>If you want to check your total memory, run the following command

<pre>$ free -g
              total        used        free      shared  buff/cache   available
Mem:             25            0         25           0           0          24
Swap:             2           0           2</pre>

$ free -g是bash的输入,下面的文字是它的输出。

我想正确设置输入和输出的样式。虽然我可以用<div>,但我觉得语义不够

看起来 <kbd> is more for keys on keyboard; <command> 已被弃用。

我应该使用哪个 HTML 标签进行命令行输入?

<pre> 表示代码块,<code> 表示片段。参见 https://www.w3.org/TR/html52/grouping-content.html#elementdef-pre

HTML 规范有一个完美的示例,展示了 <pre><kbd><samp> 的结合使用(以及一些特定于语法高亮显示的 <span>s) 标记命令行示例:

This second example shows a block of sample output from a console program. Nested samp and kbd elements allow for the styling of specific elements of the sample output using a style sheet. There's also a few parts of the samp that are annotated with even more detailed markup, to enable very precise styling. To achieve this, span elements are used.

<pre><samp><span class="prompt">jdoe@mowmow:~$</span> <kbd>ssh demo.example.com</kbd>
Last login: Tue Apr 12 09:10:17 2005 from mowmow.example.com on pts/1
Linux demo 2.6.10-grsec+gg3+e+fhs6b+nfs+gr0501+++p3+c4a+gr2b-reslog-v6.189 #1 SMP Tue Feb 1 11:22:36 PST 2005 i686 unknown

<span class="prompt">jdoe@demo:~$</span> <span class="cursor">_</span></samp></pre>

可以找到这个例子under the definition of <samp>

当然,您不需要 <span>;这些只是为了语法高亮效果,<span> 本身,就像 <div> 一样,不带有任何语义。换句话说,这个例子的语义在有或没有 <span>s.

的情况下是相同的

改用<code>没有错;这真的取决于你。但是如果你想遵循参考,那么规范提供了一个,我同样推荐它。