HTML Computercode
On this page
HTML Computer Code Elements
HTML provides special elements to represent computer code, keyboard input, program output, and variables in a semantic and readable way.
The code Element
The <code> element is used to define a piece of computer code.
Text inside the <code> element is displayed in a monospace font by default.
<code> x = 5; y = 6; z = x + y; </code>
Preserving Line Breaks
The <code> element does not preserve whitespace or line breaks.
To keep formatting and line breaks, wrap the <code> element inside a <pre> element.
<pre> <code> x = 5; y = 6; z = x + y; </code> </pre>
The kbd Element
The <kbd> element represents keyboard input from the user.
<p>Save the document by pressing <kbd>Ctrl + S</kbd></p>
The samp Element
The <samp> element is used to display sample output from a computer program.
<p>Message from the system:</p> <p><samp>File not found.<br>Press F1 to continue</samp></p>
The var Element
The <var> element defines a variable used in programming or mathematical expressions.
<p> The area of a triangle is 1/2 × <var>b</var> × <var>h</var> </p>
Chapter Summary
- The
<code>element defines computer code - The
<kbd>element defines keyboard input - The
<samp>element defines program output - The
<var>element defines variables - The
<pre>element preserves formatting and line breaks
HTML Computer Code Tags
| Tag | Description |
|---|---|
<code> | Defines programming code |
<kbd> | Defines keyboard input |
<samp> | Defines computer output |
<var> | Defines a variable |
<pre> | Defines preformatted text |