×

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 in some computer programming language like JavaScript.

In order to reflect this computer code uniquely among the regular HTML script/code, we use the code tag in HTML.

<code> in HTML

The <code> tag in HTML is a phrase tag, which has no attribute of its own. Do you have a computer code (computer programming code) in your HTML text content? No worries, the <code> tag comes into play in such scenarios. It is used to display a piece of computer code.

The output generated after the introduction of this tag is displayed in the monospace font. The font is generally the browser’s default monospace font.

Syntax:

<code> “computer code in some programming language” </code>

Attribute support

The code tag does not have attributes of it’ s own, but there are some global attributes in HTML which are supported by the code tag. These are as follows:

  • Global attributes: attributes that can be used with all HTML elements.
  • Event attributes

Types:

  1. Window events
  2. Form events
  3. Keyboard events
  4. Mouse events
  5. Media events

Example

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <p>Let's consider this basic code for creating a table in HTML:</p>
    <pre>
<code>
&lt;table&gt;
&lt;tr&gt;
&lt;td&gt;&lt;/td&gt;
&lt;td&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/table&gt;
</code>
</pre>
  </body>
</html>

Output:

Let's consider this basic code for creating a table in HTML:


<table>
<tr>
<td></td>
<td></td>
</tr>
</table>

Code 1: HTML + program code (in JS) without code tag

<!DOCTYPE html>
<html>
  <head>
    <title>Revision 1</title>
  </head>
  <body style="background-color: rgb(252, 245, 230)">
    <h1 style="text-align: center; color: rgb(32, 116, 243)">Hello! World</h1>
    <h2 style="text-align: left; color: saddlebrown">Recipe for Chai (Tea):</h2>
    <ol type="1">
      <li>Add 1 cup of water to a container</li>
      <li>Boil the water</li>
      <li>
        Add the following contents in it:
        <ol type="a">
          <li>Ginger and cardamom</li>
          <li>Two tsp sugar</li>
          <li>1 tsp of tea leaves</li>
          <li>1 cup of milk</li>
        </ol>
      </li>
      <li>Let the mixture come to a boil</li>
      <li>The tea is ready</li>
    </ol>
    <hr />
    <p style="text-align: center">* * * * * * * * * * * * * *</p>
    <hr />
    <ul>
      <li>
        <b style="color: rgb(193, 63, 253); font-style: italic">
          Let's suppose we need to make 'n' number of cups of tea using the same
          recipe, but since the quantity is not 1 cup, hence we need to
          calculate the ratio of ingredients depending on the number of cups.
        </b>
        <br />
        <b
          >Let's make this activity fun by adding a demo function to try to
          calculate the ratio using a function in JavaScript</b
        >
      </li>
      <br />
      <li>The following is the JavaScript code :</li>
      <p style="color: rgb(253, 25, 120)">
        const chai_maker = function (cups) { const liquids = cups / 2;
        console.log(`The quantity of milk and water should be ${liquids} cups
        each`); const masala = cups / 1.6; console.log( masala <= 3 ? "Add 1
        piece of each ingredient stated above" : `Add ${Math.floor(masala)}
        piece of each ingredient stated above` ); }; chai_maker(3);
        chai_maker(5); chai_maker(9);
      </p>
    </ul>
    <hr />
    <hr />
  </body>
</html>

Output:

HTML Code

Example 2: formatting the code using <code>

<!DOCTYPE html>
<html>
  <head>
    <title>Revision 1</title>
  </head>
  <body style="background-color: rgb(252, 245, 230)">
    <h1 style="text-align: center; color: rgb(32, 116, 243)">Hello! World</h1>
    <h2 style="text-align: left; color: saddlebrown">Recipe for Chai (Tea):</h2>
    <ol type="1">
      <li>Add 1 cup of water to a container</li>
      <li>Boil the water</li>
      <li>
        Add the following contents in it:
        <ol type="a">
          <li>Ginger and cardamom</li>
          <li>Two tsp sugar</li>
          <li>1 tsp of tea leaves</li>
          <li>1 cup of milk</li>
        </ol>
      </li>
      <li>Let the mixture come to a boil</li>
      <li>The tea is ready</li>
    </ol>
    <hr />
    <p style="text-align: center">* * * * * * * * * * * * * *</p>
    <hr />
    <ul>
      <li>
        <b style="color: rgb(193, 63, 253); font-style: italic">
          Let's suppose we need to make 'n' number of cups of tea using the same
          recipe, but since the quantity is not 1 cup, hence we need to
          calculate the ratio of ingredients depending on the number of cups.
        </b>
        <br />
        <b
          >Let's make this activity fun by adding a demo function to try to
          calculate the ratio using a function in JavaScript</b
        >
      </li>
      <br />
      <li>The following is the JavaScript code :</li>
      <code style="color: rgb(253, 25, 120)">
        const chai_maker = function (cups) { const liquids = cups / 2;
        <p>
          console.log(`The quantity of milk and water should be ${liquids} cups
          each`);
        </p>
        <p>const masala = cups / 1.6;</p>
        console.log( masala <= 3 ? "Add 1 piece of each ingredient stated above"
        : `Add ${Math.floor(masala)} piece of each ingredient stated above` );
        };
        <p>chai_maker(3); chai_maker(5); chai_maker(9);</p>
      </code>
    </ul>
    <hr />
    <hr />
  </body>
</html>

Output:

HTML Code

//Formatted text by <code>

HTML Code

Some important attributes/tags related to code

NameDescription
<kbd>It defines keyboard input.
<samp>It specifies a sample output from a computer program.
<pre>Defines preformatted text.
<dfn>It defines a definition term.
<var>It defines a variable.
<strong>It defines an important text.

Related Topics

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

HTML5 Attribute

HTML Attribute HTML attributes are special words that provide additional information about the HTML element or attributes.The element or tags have attributes, which define the element's behavior.It also adds attributes with a start...

3 minutes read.

What is a Container tag?

In HTML, there are different types of tags or say instructions which make the developer specify various elements in the document and make the browsers understand what to display. Tags in...

5 minutes read.

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

The span tag in HTML is used as an inline container element to mark up a specific or special chunk of a text or a section of a page. If you...

2 minutes read.

HTML Background Image

Inserting image in HTML An image (technically digital image) in HTML or even in general technical aspects, is a binary representation of visual information. By visual information we mean, in the...

8 minutes read.

HTML Search Box

The HTML search box is also known as the search bar. It is an input text field that is used to search the content. If the users want to explore...

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

Difference between HTML and DHTML

Introduction to HTML HTML stands for Hypertext Markup Language. It is a standard web language used to create web pages and applications. HTML is used to define the structure and content...

4 minutes read.

How to insert image in HTML

An image (technically digital image) in HTML or even in general technical aspects is a binary representation of visual information. By visual information we mean, in the form of drawings/pictures,...

5 minutes read.

HTML Form Input Types

HTML Form Input Types An important element of the HTML form is < input type= "> in HTML. The type attribute of the input element can be of different types, which...

8 minutes read.

HTML Forms

Forms are required when we want to collect data from the user. Html forms consists of different fields to take user inputs such as: input, text area, checkbox, password, radio...

4 minutes read.

HTML <script> Tag

For embedding a client -side script, we use script tag in html tag. This <script> tag can be used for containing scripting statements. It can also point another external script file...

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

How to insert spaces/tabs in text using HTML/CSS

What is HTML? HTML or HyperText Markup Language is used to design web pages. It was released in 1993. The file extension name for HTML file is “.html”. An HTML file...

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.

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.

Html &lt;Col> Tag

You can write the col tag inside the table tag after the thead, tbody, foot, and tr tags. You can also apply it after the caption tag (in which the...

4 minutes read.