×

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 &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 <u> tag

Underline Element/Tag: The u tag in html is used to specify the important text or phrase by underline. In earlier HTML versions, this element was known as the "Underline" element, and...

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

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.

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.

HTML City tag

HTML <City> tag HTML < cite > tag specifies a citation, a reference or title to a creative work, quoted content, books, websites, a research paper, a blog post, a painting,...

3 minutes read.

How to create a link with no underline in HTML

In HTML, an anchor tag (<a>) is a Styled Container tag, which means when a hyperlink is created using this, browsers, by default, display the link by applying some styles...

5 minutes read.

HTML Elements

An HTML element is nothing but a starting tag and then some content and then the closing tag. So we can say HTML elements is everything from the starting tag...

3 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 <section> tag

The section element in HTML is used to define sections in a document. Syntax of Section Tag: <body> <h1>……….</h1> <section> <h2>……….</h2> <p> …………………………</p> </section> <section> <h2>………..</h2> <p>…………………………. <p> </section> </body> Examples of Section Tag Example 1: In this example, we use the HTML "section" element...

3 minutes read.

Responsive HTML5 and CSS3 Tutorial

Introduction to HTML5 Tutorial HTML5 is the latest and upgraded version of HTML. HTML is a hypertext markup language. Technically, it is not a programming language. It is a markup language. HTML...

17 minutes read.

HTML <sub> tag

The sub element in HTML is used to describe one or more subscript texts. If The sub tag shows the text which is presented in a smaller font and below...

2 minutes read.

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 &lt;dd> Tag

In an HTML description list, an item's definition or description is indicated using the definition description ("dd" tag). A <dd> tag can contain paragraphs, line breaks, images, links, and lists. It...

4 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 change text color in HTML

Changing the text colour of a web page is essential because it enhances readability. There are several ways to change the colour of the text in HTML, which are as follows: Text...

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

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

The HTML <font> tag has an attribute called color, which defines the color of the text. We use the color attribute inside the <font> tag to specify the color of...

6 minutes read.