Open In App

HTML Computer Code Elements

Last Updated : 22 Mar, 2024
Improve
Improve
Like Article
Like
Save
Share
Report

The computer has a unique formatting and text style for displaying the messages related to codes. The <code> tag is used to display the computer code on the website. There are several elements available to mark up computer code using HTML.

code Tag

The <code> Tag in HTML is used to define the piece of computer code. During the creation of web pages sometimes there is a need to display computer programming code. It could be done by any basic heading tag of HTML but HTML provides a separate tag which is <code> tag. The code tag is a specific type of text that represents computer output. HTML provides many methods for text formatting but <code> tag is displayed with fixed letter size, font, and spacing.

Syntax:

<code> Computer code contents... </code>

code Tag Examples

Example 1: Implementation of the <code> Tag.

html
<!DOCTYPE html>
<html>

<body>
    <pre>
        <code> 
            #include<stdio.h> 
            int main() { 
                printf("Hello Geeks"); 
            } 
        </code> 
    </pre>
</body>

</html>

Output: 

output

Example 2: Implementation of the <code> Tag.

html
<!DOCTYPE html>
<html>

<body>
    <pre>
        <code> 
            class GFG  
            {  
                // Program begins with a call to main() 
                // Print "Hello, World" to the terminal window  
                public static void main(String args[])  
                {  
                    System.out.println("Hello, World");  
                }  
            }  
        </code> 
    </pre>
</body>

</html>

Output: 

output2


Note: The program which is written inside the <code> tag has some different font sizes and font types to the basic heading tag and paragraph tag. <pre> tag is used to display code snippets because it always keeps the text formatting as it is.

Some points about <code> tag

  • It is mainly used to display the code snippet in the web browser.
  • This tag styles its element to match the computer’s default text format.
  • The web browsers by default use a monospace font family for displaying <code> tags element content.

kbd Tag

The <kbd> Tag is a phrase tag and is used to define the keyboard input. The text between the <kbd> tag represents similar text that should be typed on the keyboard.

Syntax:

<kbd> Contents... </kbd>

Example

Implementation of the <kbd> Tag.

html
<!DOCTYPE html>
<html>

<head>
    <title>The kbd tag</title>
    <style>
        body {
            text-align: center;
        }
    </style>
</head>

<body>
    <div class="gfg">GeeksforGeeks</div>
    <kbd>A computer</kbd>
    <kbd>science</kbd>
    <kbd>portal</kbd>
</body>

</html>

Output: 

output

Some points about <kbd> tag 

  • The text enclosed by <kbd> tag is typically displayed in the browser’s default mono-space font.
  • It is possible to achieve a richer effect with CSS
  • There is no tag-specific attributes.

pre Tag

The <pre> Tag in HTML is used to define the block of preformatted text which preserves the text spaces, line breaks, tabs, and other formatting characters which are ignored by web browsers. Text in the <pre> element is displayed in a fixed-width font, but it can be changed using CSS. The <pre> tag requires a starting and end tag.

Syntax:

<pre> Contents... </pre>

kbd Tag Examples

Example 1: Implementation of the <pre> Tag.

html
<!DOCTYPE html>
<html>

<head>
    <title>pre tag</title>
</head>

<body>
    <pre>
        GeeksforGeeks 
        A Computer   Science Portal  For Geeks 
    </pre>
</body>

</html>

Output: 

output3.1

Example 2: Implementation of the <pre> Tag.

html
<!DOCTYPE html>
<html>

<head>
    <title>pre tag with CSS</title>
    <style>
        pre {
            font-family: Arial;
            color: #009900;
            margin: 25px;
        }
    </style>
</head>

<body>
    <pre>
        GeeksforGeeks 
        A Computer  Science Portal  For Geeks 
    </pre>
</body>

</html>

Output: 

output3.2

samp Tag

The <samp> Tag is a phrase tag and used to define the sample output text from a computer program. The HTML Sample Element is used to enclose inline text which represents sample (or quoted) output from a computer program.

Syntax:

<samp> Contents... </samp>

Example

Implementation of the <samp> Tag.

html
<!DOCTYPE html>
<html>

<head>
    <title>samp tag</title>
</head>
<style>
    body {
        text-align: center;
    }

    .gfg {
        font-size: 40px;
        font-weight: bold;
        color: green;
    }

    .geeks {
        font-size: 25px;
        font-weight: bold;
    }
</style>

<body>
    <div class="gfg">GeeksForGeeks</div>
    <div class="geeks">&lt;samp&gt; Tag</div>
    <samp>A computer science portal for Geeks</samp>
</body>

</html>

Output: 

output4.1

var Tag

The <var> Tag is a phrase tag and used to specify the variable in a mathematical equation or in a computer program. The content of this tag is displayed in the italic format in most of browsers.

Syntax:  

<var> Contents... </var>

Example

Implementation of the <var> Tag.

html
<!DOCTYPE html>
<html>

<head>
    <title>var tag</title>
</head>
<style>
    body {
        text-align: center;
    }

    .gfg {
        font-size: 40px;
        font-weight: bold;
        color: green;
    }

    .geeks {
        font-size: 25px;
        font-weight: bold;
    }
</style>

<body>
    <div class="gfg">GeeksForGeeks</div>
    <div class="geeks">&lt;var&gt; Tag</div>
    <var>GeeksforGeeks Variable</var>
</body>

</html>

Output: 

output5

Supported Browser

  • Google Chrome 1
  • Microsoft Edge 12
  • Firefox 1
  • Opera 15
  • Safari 4


Previous Article
Next Article

Similar Reads

How to define a piece of computer code in HTML5?
In this article, we define a piece of code by using the code Element. It is used to define the piece of computer code. During the creation of web pages, sometimes there is a need to display computer programming code. It could be done by any basic heading tag of HTML, but HTML provides a separate tag which is &lt;code&gt;. The code tag is a specific
2 min read
How to convert 3-digit color code to 6-digit color code using JavaScript ?
In this article, we are converting a three-digit color HEX code into a six-digit color HEX code using JavaScript. To solve this problem, first, we copy the individual digits and paste them at the consecutive positions. For example - #34E will be converted into #3344EE and #E90 will be converted into #EE9900. Steps to convert 3-digit color code to 6
2 min read
How to generate QR-Code using 'react-qr-code' in ReactJS ?
react-qr-code is a module or package for react that provides the QRCode component to generate the code with custom values. QR code is a square grid that can easily be readable with digital devices like smartphones. Syntax:&lt;QRCode title="title" value="value" bgColor="background-color" fgcolor="foreground-color" level="level" size={number}/&gt;Pro
2 min read
How to include HTML code snippets in HTML ?
In this article, we will learn how to include HTML snippets in HTML code. We will include the HTML code snippet of "gfg.html" into "index.html". To achieve this task, we are going to write a JavaScript function in "index.html" which traverses the collection of all HTML elements in "gfg.html" and searches for elements with specific attributes. It cr
3 min read
HTML DOM KeyboardEvent code Property
The keyboardEvent code property in HTML represents a physical key on the keyboard. It is used to return the key that triggers the event. Syntax: event.code Return Value: It returns a string that represents which key is pressed. Example 1: With KeyDown Event C/C++ Code &lt;html&gt; &lt;head&gt; &lt;title&gt;DOM keyboardEvent code Property&lt;/title
2 min read
HTML DOM Code Object
The DOM Code Object is used to represent the HTML &lt;code&gt; element. The Code element is accessed by getElementById(). Syntax: document.getElementById("id"); Where 'id' is the ID assigned to the code tag. Example 1: In this example, we will use DOM Code Object C/C++ Code &lt;!DOCTYPE html&gt; &lt;html&gt; &lt;body&gt; &lt;h1 style=&quot;color:gr
1 min read
HTML | &lt;applet&gt; code Attribute
The &lt;applet&gt; code attribute specifies a relative URL for applets java file to be loaded and executed. It is used to link a Java applet to the concerned HTML document. It specifies the file name of the Java applet. Note : The &lt;applet&gt; tag is not supported in HTML5. Syntax: &lt;applet code = "file_name"&gt; .... &lt;/applet&gt; Attribute
1 min read
How to minify HTML code of PHP page ?
HTML output minification is important to improve website performance by reducing the page load times and reduce the overall page size. Minifying HTML output also reduces the data usage of the user requesting the site. Minification can be done by removing unnecessary details and eliminating excessive whitespaces, newlines, comments, etc. However, mi
3 min read
HTML DOMException code property
The code property of the DOMException interface returns a short number that contains the error code of the exception error, or 0 if none match. This is a read-only property. Syntax: var codeNumber = domException.code; Note: This property has been DEPRECATED and is no longer recommended. Return Value: This property returns a short number. Example: I
1 min read
How to embed PHP code in an HTML page ?
PHP is the abbreviation of Hypertext Preprocessor and earlier it was abbreviated as Personal Home Page. We can use PHP in HTML code by simply adding a PHP tag without doing any extra work. Example 1: First open the file in any editor and then write HTML code according to requirement. If we have to add PHP code then we can add by simply adding &lt;?
2 min read
Article Tags :