×

HTML Code Tag

The code tag in HTML is a phrase tag used to insert a piece of computer programming code. In HTML there is a separate tag named as <code> tag. Sometimes, there is a place in webpages where you want to format the text in such a way that your website, which is related to coding or Information Technology field, then you can display code in your website. That’s why we use the code tag. When we need to show computer code on web pages, we use the code tag.

The code is displayed in the monospace font, a default browser font. Primarily we use code tag when we want to show a single line in the webpage. For showing multi-line, we prefer to use the <pre> tag.

HTML <code> tag supports the Global attributes, which can be used to any HTML element.

HTML <code> tag also supports the Event attributes on which the browser takes action.

Syntax of HTML Code Tag

<code>Computer code</code>

Examples of HTML Code Tag

Example 1: This example has only a single line of code.

<html>  
   <head>  
      <title>code</title>
      </head>  
      <body>  
         <p>This is a code.</p> 
            <code>var a=6; </code>
         </p>
      </body>  
</html>  

Output: You can see the code with the monospace font, as shown in the image below.

HTML Code Tag

Example 2: We have used multi-lines of Java programming code in this example.

<html>
   <head> 
      <title>code</title>
      </head> 
      <body> 
         <p>This is a Java Programming code.</p> 
            <code>import java.util.Scanner;
                public class AddTwoNumbers {
                
                    public static void main(String[] args) {
                        
                        int number1, number2, sum;
                        Scanner sc = new Scanner(System.in);
                        System.out.println("Enter First Number: ");
                        num1 = sc.nextInt();
                        
                        System.out.println("Enter Second Number: ");
                        num2 = sc.nextInt();
                        
                        sc.close();
                        sum = number1 + number2;
                        System.out.println("Sum of two numbers: "+sum);
                    }
                }
            </code>
         </p>
      </body>  
</html>  

Output: Although it is a multi-line code, we can see that it displays in a single line because we are using code tags for single-line code display.

HTML Code Tag

Example 3: Use of <pre> tag.

<html>  
   <head>  
      <title>code</title>
      </head>  
      <body>  
         <p>This is a Java Programming Code.</p> 
            <pre>import java.util.Scanner;
                public class AddTwoNumbers {
                
                    public static void main(String[] args) {
                        
                        int number1, number2, sum;
                        Scanner sc = new Scanner(System.in);
                        System.out.println("Enter First Number: ");
                        num1 = sc.nextInt();
                        
                        System.out.println("Enter Second Number: ");
                        num2 = sc.nextInt();
                        
                        sc.close();
                        sum = number1 + number2;
                        System.out.println("Sum of two numbers: "+sum);
                    }
                }
            </pre>
         </p>
      </body>  
</html>  

Output: You can see code displays in multi-lines because we have used the <pre> tag in this example.

HTML Code Tag

Example 4: We can also used CSS to highlight the code.

<!DOCTYPE html>
<html>
  <head>
    <title>Code</title>
    <style>
      .style {
        font-size: 20px;
        line-height: 28px;
        background-color: rgb(177, 228, 245);
        color: #f9e3e3;
      }
    </style>
  </head>
  <body>
    <p>This is a code.</p>
    <code class="style">The code goes here.</code>
    <p>Happy coding!</p>
  </body>
</html>

Output: As you can see in the given image, the code looks attractive with the use of CSS.

HTML Code Tag

Browser Supported

  1. Google Chrome
  2. Internet Explorer
  3. Microsoft Edge
  4. Mozilla Firefox
  5. Opera
  6. Safari

Related Topics

HTML <video> Tag

The Video tag in HTML is used to play video on a media player on the web page. We can also insert an audio by the video tag, but audio...

6 minutes read.

HTML <address> Tag

Hyper Text Markup Language, HTML is the best markup language for creating Web pages. HTML consists of a wide variety of elements. In all of these elements, one element is...

4 minutes read.

HTML Layouts

HTML Layouts HTML templates provide a way for well-managed, well-structured responsive web pages to be organized. We could say that the HTML layout specifies how web pages can be set up....

4 minutes read.

Font tag in HTML

HTML stands for Hypertext Mark-up Language and is used to create web pages and other information that can be later displayed in a web browser. It also consists of a...

4 minutes read.

HTML Frames

Frames in Html is used to divide the browser into different sections where each section can load different Html documents. Collection of frame is known as Frameset. Creating frames:We use frameset...

2 minutes read.

HTML <summary> tag

When we simply want to show the primary heading or topic name of hidden detailed material to users, then we can achieved this by using the summary tag. This makes it...

2 minutes read.

HTML <table> tag

The Table element If we want to show the data and information in a two-dimensional table with rows and columns, then we can achieve this by using the HTML element <table>. Syntax: <body> <table> <thead> <tr> <thcolspan="#">………….</th> </tr> </thead> <tbody> <tr> <td>……….</td> <td>……….</td> </tr> </tbody> </table> </body> Example: <!DOCTYPE...

9 minutes read.

HTML Lists

HTML List HTML lists are used to specify data sets. All lists can include one or more elements from the list. HTML lists are available in three different types: Ordered List or...

4 minutes read.

Html Wbr Tag

The Line Break Opportunity element A word break opportunity is a place in text from where the browser breaks a line. This opportunity is represented by the element wbr in the...

2 minutes read.

HTML Hide Element

The HTML hide element is used to hide the element. We place the hidden attribute inside those elements which we want to hide. The hidden attribute is a Boolean attribute. The...

3 minutes read.

HTML <br> tag

HTML is referred to as Hyper Text Markup Language, which is the most popular language for creating Web pages. HTML explains the construction of a Web page. It contains a...

4 minutes read.

HTML Id Attribute

The HTML id attribute represents the unique identifier for an element of the HTML document. Each element has its unique identifier. The id attribute is a global attribute that can be...

12 minutes read.

Html &lt;center> Tag

The term HTML refers to Hyper Text Markup Language. The most widely used markup language for making web pages is HTML. A structure of web page is described in HTML....

3 minutes read.

HTML Audio

HTML AUDIO: HTML5 provides a method to embed the audio file into a webpage by using HTML5. Before HTML5 audio files is used to play on the browser with the help of...

1 minute read.

HTML Canvas

HTML Canvas: Using JavaScript, the < canvas > tag in HTML is used for drawing graphics on web page. It can be used to draw routes, boxes, text, gradient, and...

3 minutes read.

HTML SVG

HTML SVG: SVG stands for Scalable Vector Graphics which defines the graphics for the web. It is a language for describing 2-D graphics in XML. Each drawn shape in SVG is...

1 minute read.

HTML Unordered List

In HTML, we have <ul> tag which defines the unordered list. And it is also known as bulleted list, because as we know it is not ordered list therefore it...

2 minutes read.

HTML Basic Tags

Heading Tags: This tag represents the document header. It is said to be a good practice if every document starts with Heading. Heading tags ranges from H1 to H6 i.e....

3 minutes read.

HTML Links

It consist of anchor element which is used to create a link between a source anchor and destination anchor. The anchor tag can be embedded between text, image, video or...

2 minutes read.

How to add Google Translate Button on your Webpage?

Google translator is a multi-language translator used for translating the webpage. Google develops it. In today's digital age, it is essential because information is available worldwide in some specific language...

5 minutes read.