HTML Computer Code Elements

Computer code

var person = {
    firstName:"Bill",
    lastName:"Gates",
    age:50,
    eyeColor:"blue"
}

HTML computer code format

Generally, HTML usesVariableof letter size, as well as variable letter spacing.

In the displayComputer codeIn the example, it is not necessary to do so.

<kbd>, <samp>, and <code> All elements support fixed letter size and spacing.

HTML keyboard format

HTML <kbd> Element DefinitionKeyboard input:

Example

<p>To open a file, select:</p>
<p><kbd>File | Open...</kbd></p>

Try It Yourself

HTML Sample Format

HTML <samp> Element DefinitionComputer Output Example:

Example

<samp>
demo.example.com login: Apr 12 09:10:17
Linux 2.6.10-grsec+gg3+e+fhs6b+nfs+gr0501+++p3+c4a+gr2b-reslog-v6.189
</samp>

Try It Yourself

HTML Code Formatting

HTML <code> Element DefinitionProgramming Code Example:

Example

<code>
var person = { firstName:"Bill", lastName:"Gates", age:50, eyeColor:"blue" }
</code>

Try It Yourself

<code> elementnot preservedextraspacesandLine breaks:

Example

Code Example:

<code>
var person = {
    firstName:"Bill",
    lastName:"Gates",
    age:50,
    eyeColor:"blue"
}
</code>

Try It Yourself

To solve this problem, you must enclose the code within the <pre> element:

Example

Code Example:

<code>
<pre>
var person = {
    firstName:"Bill",
    lastName:"Gates",
    age:50,
    eyeColor:"blue"
}
</pre>
</code>

Try It Yourself

HTML Variable Formatting

HTML <var> Element DefinitionMathematical Variable:

Example

<p>Einstein wrote:</p>
<p><var>E = m c<sup>2</sup></var></p>

Try It Yourself

HTML Computer Code Elements

Tag Description
<code> Define Computer Code Text
<kbd> Define Keyboard Text
<samp> Define Computer Code Example
<var> Define Variable
<pre> Define Preformatted Text