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");         } }  

Exception Occured:

TypeErrorException
Messageerror_log(app.log): Failed to open stream: Permission denied
File/home/dh_czz6eb/tutorialwalk.com/common/error.php
Line36
Trace#0 (): log_error(2, 'error_log(app.log): Failed to open stream: Permission denied', '/home/dh_czz6eb/tutorialwalk.com/common/error.php', 36)
#1 /home/dh_czz6eb/tutorialwalk.com/common/error.php(36): error_log('02-12-2024 15:57:51 pm (IST),/home/dh_czz6eb/tutorialwalk.com/common/common.php,193,0,ErrorException,Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter,/html/code-tags.html,#0 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parame...', '/home/dh_czz6eb...', 193)
#1 [internal function]: check_for_fatal()
#2 {main}
', 3, 'app.log')
#2 /home/dh_czz6eb/tutorialwalk.com/common/error.php(8): log_exception(ErrorException)
#3 /home/dh_czz6eb/tutorialwalk.com/common/error.php(115): log_error(8192, 'Optional parameter $per_page declared before required parameter $href is implicitly treated as a required parameter', '/home/dh_czz6eb/tutorialwalk.com/common/common.php', 193)
#4 (): check_for_fatal()
REQUEST_URI/html/code-tags.html