HTML Code Tags

Bookmark
Learn about adding program code in HTML document using code, pre tags.

HTML has got the following rich set of tag elements to format computer programming code.

HTML Tag Element Description
<code> Code snippet.
<kbd>
Keyboard input.
<var> A Variable.
<samp> Sample program output.
<pre> Pre-formatted text.

HTML Code Tag

<code> tag is used to format an inline code snippet and generally used of short codes. Following is an example.

<p><code>HashTable</code> is a Java API.</p>

Example Output

HashTable is a Java API.

HTML Keyboard Tag

<kbd> tag is used to format a keyboard input. It just formats the defined phrase. Following is an example.

<p>Press following keys to save the file.</p>
<p><kbd>CTRL + S</kbd></p>

Example Output

Press following keys to save the file.

CTRL + S

HTML Variable Tag

<var> tag is used to define a variable used in a program or formula. Following is an example.

<p><var>(a+b)<sup>2</sup> = a<sup>2</sup> + b<sup>2</sup></p>

Example Output

(a+b)2 = a2 + b2

HTML Sample Tag

<samp> tag is used to define a sample program output. Following is an example.

<samp>
PATH=C:\ProgramData\Oracle\Java\javapath;
</samp>

Example Output

PATH=C:\ProgramData\Oracle\Java\javapath;

HTML Pre Tag

<pre> tag is used to define a pre formatted text. Whatever formatting is present inside the content is preserved. Enclosed content is not parsed for HTML tags and it is displayed as it is. Following is a example.

<pre>
String name = "apple";
System.out.println(name);
function print() {
loop(true) {
print("hi");
}
}
</pre>

Example Output

String name = "apple"; System.out.println(name); function print() {         loop(true) {                 print("hi");         } }