Open In App

HTML Computer Code Elements

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


Last Updated : 22 Mar, 2024
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads