×

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

SVG is used to determine the vector-based graphics in XML format. What is SVG? We utilize SVG tag to specify visuals for the website. The attribute of SVG in a HTML document...

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

HTML5 Formatting

HTML Formatting HTML Formatting is a method of text formatting for a better appearance and look. HTML allows us to format text without using the CSS. HTML includes a lot of formatting tags....

6 minutes read.

Html &lt;datalist >Tag

The autocomplete feature in HTML files is provided by the <datalist> tag. It can be used in conjunction with an input tag to enable users to quickly fill out form...

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

Before jumping into the topic, let’s understand a common scenario where we have a code snippet in our HTML file. By code snippet, we mean computer code, which is written...

5 minutes read.

HTML Button Disabled

The HTML <button> element has many attributes, one of them is the disabled attribute. The button on which you apply the disabled attribute will not work, or we can say...

4 minutes read.

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

3 minutes read.

HTML Font Size

The HTML <font> tag uses the size attribute to specify the size of the text. We can set the size attribute’s value from 1 to 7. The default size of...

4 minutes read.

How to Create a Portfolio Gallery using HTML and CSS?

First, before starting this, the user should know some of the basics of HTML and CSS to create a portfolio gallery. To add more functionality to the website, we can also...

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

Design a tribute page using HTML and CSS

A tribute page is a webpage based on a person who inspired us in our life. In this article, we will create a webpage of Mahatma Gandhi. We are going...

5 minutes read.

How to add JavaScript to HTML

JavaScript is one of the most important languages today. It is a high-level, interpreted, lightweight programming language that makes web pages more interactive. It is a language of the web....

5 minutes read.

Div Tag in HTML

In HTML, ‘div’ is a tag that specifies division or section in HTML document. But before going in detail of it, let’s first understand about tags in HTML. The tag...

8 minutes read.

HTML &lt;blink> Tag

In HTML, the blink tag is used to create the text which flashes. The blink tag is an inline element in HTML. It is observed that the blink tag used...

2 minutes read.

HTML <sup> Tag

The HTML sup element is used to describe one or more superscript contents. The sup tag shows a text which is presented above the regular line and half the length...

2 minutes 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 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 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 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.