HTML | Computer Code Elements

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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:
output



Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

<!DOCTYPE html>
<html
    <head
        <title>pre tag</title
    </head
    <body
        <pre
            GeeksforGeeks 
            A Computer   Science Portal  For Geeks 
        </pre
    </body
</html>                                       

chevron_right


Output:
output3.1

Example 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


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:

filter_none

edit
close

play_arrow

link
brightness_4
code

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

chevron_right


Output:
output5



My Personal Notes arrow_drop_up

Connecting dots

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.




Article Tags :
Practice Tags :


2


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.