HTMLs Tutorial

HTML Tutorial HTML Tags HTML Basic Tags HTML Attributes HTML Elements HTML Formatting HTML Text Format HTML body tag HTML samp tag HTML script Tag HTML section tag HTML select tag HTML source tag HTML span tag HTML strike tag HTML strong tag HTML style tag HTML sub tag HTML summary tag HTML sup Tag HTML svg tag HTML table tag HTML u tag HTML Headings HTML Paragraphs HTML wbr tag HTML Anchor HTML Image HTML Lists HTML Ordered List HTML Unordered List HTML Form HTML Form input HTML with CSS HTML Layouts HTML References HTML Frames HTML Links Fieldset Tag in HTML Basic HTML Tags Br Tag in HTML Free HTML Templates How to Create a Table in HTML HTML Calendar HTML Card HTML Cellspacing HTML Center Image HTML Checkbox Read-only HTML Cleaner HTML Code for a Tab HTML Comment HTML Compiler HTML Nested Forms HTML Overlay Text on the Image HTML Select Option Default HTML Snake Game HTML Subheader HTML Tab Character dd Tag in HTML How Many HTML Tags are There HTML Align Tag HTML Responsive HTML Tab Code HTML Table Alternate Row Color HTML Table Fix Column Width Contact HTML DL Tag in HTML How to Insert Image in HTML HTML Background Color HTML Dark Mode How to Convert HTML to PNG HTML Data Toggle HTML Email Template HTML Font Color HTML Font Family ID and Class in HTML HTML Tab Space HTML Tab Tag HTML Itemprop HTML Itemscope HTML Form Design HTML Input Only Numbers HTML Textarea HTML to JPG HTML to Markdown Python li Tag in HTML MDN HTML What is the Correct HTML for Making a Hyperlink? What is the Root Element of an HTML Document How to Make a Box in HTML How to Save HTML Files in Notepad How to Align Text in HTML How to Change Font Color in HTML? How to Change Font Size in HTML How to Change Image Size in HTML How to Create a HTML Page How to Create a Link in HTML File? How to Create an HTML File? HR Tag in HTML HTML Base Tag HTML Default Attribute HTML Hyperlink HTML Indent HTML Injection Payloads HTML Input Numbers Only HTML Roadmap HTML Row Height HTML Schedule HTML Space HTML Tab HTML vs HTTP HTML5 API HTML5 Video HTML Collection to Array Text Area in HTML

HTML5 Advance

HTML5 Tutorial HTML5 Tags HTML Button Tag HTML canvas Tag HTML caption Tag HTML City tag HTML Tag HTML5 SVG HTML Event Attribute HTML5 Audio HTML5 Youtube HTML5 Button Tag HTML5 Tags

Misc

How to add JavaScript to HTML How to change font in HTML How to change text color in HTML HTML Date HTML Hide Element HTML Nested Table HTML Reset Button What does HTML stand for? HTML Background Image HTML Tag Div Tag in HTML How to insert Image in HTML How to create a link with no underline in HTML How to insert spacestabs in text using HTMLCSS HTML tag HTML Code HTML Tag HTML Canvas Design a tribute page using HTML and CSS What is a Container tag Font tag in HTML Difference between HTML and DHTML Empty Tag in HTML HTML Button Link Html Line Break Img src HTML Nested List in HTML Placeholder in HTML TD in HTML HTML Space Code HTML Target Attribute HTML Tag Markup Meaning in HTML Border-Collapse in HTML HTML Onclick Online HTML Compiler Convert HTML to PDF HTML Formatter HTML5 - Web Storage HTTP – Responses Container Tag in HTML DL Tag in HTML Horizontal Rule HTML HTML Tab Text Html Table Cell Background Color HTML Table Cell Color HTML Col Width How Many HTML Tags are There Convert String to Unicode Characters in Python HTML Runner HTML Style Attribute HTML Superscript Attribute HTML tabindex Marquee Tag in HTML HTML Dynamic Form HTML side Tag HTML Pattern Attribute HTML q Tag HTML Readonly Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line HTML Plain Text HTML Popup Hspace in HTML HTML Interpreter HTML Maker HTML Nav Tag HTML Nested List How to create cards in HTML HTML Writer OnKeyDown in HTML Onmove in HTML Strip HTML Tags from String Style Dark Mode in HTML Automatic Image Slider in HTML CSS Base 64 Encoding in HTML Documents Enhancing Data Portability and Security Evo Cam Web Cam HTML Free code camp HTML CSS How to Add a JS File in HTML? How to Add Picture in HTML How to Add the Logo in HTML? How to Add Video in HTML HTML Class Attribute HTML Entities HTML Form Elements HTML Form Templates HTML Marquee Tag HTML Radio Buttons HTML Text box HTML to JSX HTML Tooltip Basic HTML Codes How to Align Image Center in HTML HTML Header Tag HTML Image Tag HTML Next Line

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.