Skip to content

HTML Computercode

Representing Code and Technical Output
function greet(name) {
console.log(`Hello, ${name}!`);
}
greet('World');

HTML provides specific semantic elements to represent different types of computer-related text, such as code snippets, keyboard input, sample output, and variables.

Using these elements improves accessibility and allows for specific styling via CSS. Most of these elements are typically rendered in a monospace font by default, preserving character spacing.

The ``` element is used to mark up a fragment of computer code.

<p>The HTML `&lt;section&gt;` element defines a section in a document.</p>
<p>In JavaScript, use the `console.log()` method for debugging.</p>

Note: The element itself does not preserve whitespace (like line breaks and multiple spaces). For code blocks where formatting is important, wrap the element inside a <pre> element.

<pre>: Preformatted Text (for Code Blocks)

Section titled “<pre>: Preformatted Text (for Code Blocks)”

The <pre> element defines preformatted text. Whitespace inside a <pre> element (spaces, tabs, line breaks) is preserved exactly as written in the HTML source.

It’s commonly used together with ``` to display blocks of code, maintaining indentation and line breaks:

<pre>`
function calculateArea(width, height) {
if (width > 0 && height > 0) {
return width * height;
}
return 0;
}
`</pre>

This combination ensures the code is both semantically marked as code (```) and visually formatted correctly (<pre>).

The <kbd> element represents user input, typically from a keyboard, but also potentially voice commands or other inputs.

<p>To save the file, press <kbd>Ctrl</kbd> + <kbd>S</kbd>.</p>
<p>Type <kbd>exit</kbd> to close the terminal.</p>

The <samp> element represents sample or example output from a computer program or system.

<p>If you enter the wrong password, you might see:</p>
<p><samp>Login incorrect</samp></p>
<p>The script outputted: <samp>Process completed successfully.</samp></p>

The <var> element represents a variable. This could be a variable in a mathematical expression or programming context, an identifier, or a placeholder.

<p>The area of a rectangle is calculated as <var>width</var> &times; <var>height</var>.</p>
<p>In the command `git commit -m "<var>message</var>"`, replace <var>message</var> with your commit description.</p>
TagDescription
`Defines a piece of computer code.
Defines preformatted text (preserves whitespace). Often used with ```.
Defines keyboard input (or other user input).
Defines sample output from a program.
Defines a variable or placeholder.

Using these elements appropriately makes your technical content more meaningful and accessible.