Code has been added to clipboard!

HTML Code Snippets: How to Replicate Computer Output

Reading time 2 min
Published Feb 27, 2019
Updated Sep 27, 2019

TL;DR – HTML computer code elements are used to display computer output or code fragments.

What's Special About Computer Code

Normally, HTML uses various letter sizes, fonts, and spacing. You can adjust it with CSS properties like font-size, font-family, and so on.

HTML computer code is displayed with fixed letter size, font, and spacing. You may want to replicate this look when adding HTML code snippets in your web page. There are a few HTML elements to help you achieve this goal:

Element Defines
<code> Computer code
<samp> Computer output
<kbd> Keyboard input
<var> Variables
<pre> Preformatted text (e.g., an HTML code block)

Tags for HTML Code Snippet Formatting

The HTML <code> tags define an HTML code snippet on your website. The text inside the <code> tags will be displayed in a default monospace font:

Example
<code>
  a = 6;
  b = 9;
  c = "this is the best program";
</code>

The <samp> element depicts the output of the system or computer in your HTML document. The font, size, and spacing remain the same as with other HTML code elements:

Example
This is an output of a program ->
<samp>
 a + b + c = alotOfNumbers
</samp>

While technically the <kbd> element also defines a fragment of HTML code, according to HTML syntax, it should only be used to display a keyboard input:

Example
Press <kbd>Ctrl + O</kbd> to open a document file

The <var> element defines a variable. It can be used to define variables in mathematics or programming:

Example
<p>Einstein's best known equation goes like this:
<var>E</var> = <var>mc</var><sup>2</sup></p>

Preformatting HTML Code Blocks

Just like <p> elements, HTML code tags ignore all white spaces and line breaks. If you need to convey the structure of an HTML code block with indentations, you should put the <code> element inside a <pre> element:

Example
<pre>
  <code>
    variables :
     a
     b
     c
 
    I can write my code anyway I like
    and the "pre" tag will keep it that way!
  </code>
</pre>

HTML Code: Useful Tips

  • If all you need to keep is a line break, you can also use the <br> tag to insert one instead of preformatting the whole HTML code block.
  • You can highlight the syntax in your HTML code snippets by using a JavaScript library, such as Highlight.js or Prism.js.
Basics
Introduction
Syntax
Editors
Basic Examples
Head Section
<!DOCTYPE>
Tags and Elements
Semantic Elements
Tags Reference
Attributes
Comments
Block and Inline Elements
Forms
Form Elements
Input
Responsive Web Design
Inline Scripts
Uniform Resource Locator
Redirect
XHTML
Geolocation
Drag and Drop
Local Storage
Web Workers
Server-Sent Events
Character Encoding
Text Formatting
Quotation and Citation Elements
Headings
Paragraphs
Links
Tables
Lists
Symbols
Space
Tab
Styles
Computer Code
Layout
Classes
Colors
Images
iframes
Audio Player
Video Player
YouTube Videos
Multimedia
Canvas
SVG
<!-- -->
<a>
<abbr>
<acronym> DEPRECATED
<address>
<applet> DEPRECATED
<article>
<aside>
<audio>
<b>
<base>
<basefont> DEPRECATED
<bdi>
<bdo>
<big> DEPRECATED
<blink> DEPRECATED
<blockquote>
<body>
<br>
<button>
<canvas>
<caption>
<center> DEPRECATED
<cite>
<code>
<col>
<colgroup>
<datalist>
<dd>
<del>
<details>
<dfn>
<dialog>
<dir> DEPRECATED
<div>
<dl>
<dt>
<em>
<embed>
<fieldset>
<figcaption>
<figure>
<font> DEPRECATED
<footer>
<form>
<frame> DEPRECATED
<frameset> DEPRECATED
<h1> – <h6>
<head>
<header>
<hr>
<html>
<i>
<iframe>
<img>
<input>
<ins>
<kbd>
<keygen> DEPRECATED
<label>
<legend>
<li>
<link>
<main>
<map>
<mark>
<menu>
<menuitem> DEPRECATED
<meta>
<meter>
<nav>
<noframes> DEPRECATED
<noscript>
<object>
<ol>
<optgroup>
<option>
<output>
<p>
<param>
<pre>
<progress>
<q>
<rp>
<rt>
<ruby>
<s>
<samp>
<script>
<section>
<select>
<small>
<source>
<span>
<strike> DEPRECATED
<strong>
<style>
<sub>
<summary>
<sup>
<table>
<tbody>
<td>
<tfoot>
<th>
<thead>
<time>
<title>
<tr>
<track>
<tt> DEPRECATED
<u>
<ul>
<var>
<video>
<wbr>