Skip to content
Related Articles

Related Articles

Improve Article
Save Article
Like Article

HTML | Computer Code Elements

  • Difficulty Level : Easy
  • Last Updated : 13 Aug, 2021

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 number of 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 separated tag which is <code> tag. 
The code tag is a specific type of text which represent 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>

Example 1: 
 

Attention reader! Don’t stop learning now. Get hold of all the important HTML concepts with the Web Design for Beginners | HTML  course.

html




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

Output: 
 

output



Example 2: 
 

html




<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

Output: 
 

output2

Note: The program which is written inside the <code> tag has some different font size and font type to the basic heading tag and paragraph tag. <pre> tag is used to display code snippet because it always keeps the text formatting as it.
Some points about <code> tag: 
 

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

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

<kbd> Contents... </kbd>

Example: 
 

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 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>

Example 1: 
 

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: 
 

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: It 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: 
 



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"><samp> Tag</div>
        <samp>A computer science portal for Geeks</samp>
    </body>
</html>                    

Output: 
 

output4.1

<var> Tag: It 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 the browsers.
Syntax: 
 

<var> Contents... </var>

Example: 
 

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"><var> Tag</div>
        <var>GeeksforGeeks Variable</var>
    </body>
</html>                    

Output: 
 

output5

Supported Browser:

  • Google Chrome
  • Microsoft Edge
  • Firefox
  • Opera
  • Safari



My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!