2 Hyper Text Markup Language

Web Technology · Unit 2 · 10 hrs

Hyper Text Markup Language

Exam-focused notes for Hyper Text Markup Language (Web Technology, CSC329): what the TU syllabus asks and how it has actually been tested, with 11 solved past questions from this unit.

Hyper Text Markup Language in short

HTML gives every web page its structure, the skeleton that CSS later styles and JavaScript later animates, so this unit works through its tags and attributes from the ground up to the semantic and event-handling features HTML5 added.

Hyper Text Markup Language: notes

Introduction to HTML

HTML (HyperText Markup Language) is the standard markup language for building web pages. A page written in HTML is a series of elements that tell the browser how to structure and display content; browsers read the tags but do not display them, using them instead to render the page. HTML is often described as the skeleton of a website: it supplies the structure that CSS and JavaScript later dress and animate. A minimal HTML5 document looks like this:

<!DOCTYPE html>
<html>
<head>
  <title>Page Title</title>
</head>
<body>
  <h1>This is a heading.</h1>
  <p>This is a paragraph.</p>
</body>
</html>

The <!DOCTYPE html> declaration is not a tag; it simply tells the browser to render the page as HTML5.

Elements of HTML Document

Every HTML document is built from a small set of structural elements. The <html> element is the root of the page and wraps everything else. Inside it, <head> holds information about the document rather than visible content: a <title> (shown in the browser tab), meta tags, and links to stylesheets or scripts. <body> holds everything that is actually displayed: headings, paragraphs, images, links, forms and so on. Together <html>, <head> and <body> give every page the same basic skeleton, no matter how much content sits inside it.

HTML Elements and HTML Attributes

An HTML element is everything from its start tag to its matching end tag, written as <tagname>content</tagname>. A handful of elements, such as and <img>, are empty (void) elements: they carry no content and no end tag at all. Elements can carry attributes, extra information written inside the start tag as name="value" pairs, for example the href attribute on <a> that gives the link's destination, or the src and alt attributes on <img> that give an image's location and fallback text. Attributes never appear on the closing tag.

Headings

HTML provides six levels of heading, <h1> through <h6>, used to show the document's structure. <h1> is the most important heading and renders with the largest font by default; <h6> is the least important and renders smallest. Browsers automatically add margin space before and after a heading. <h1> should mark the page's main heading, <h2> its major sub-sections, and so on down the hierarchy, rather than being chosen purely for font size.

<h1>This is heading 1</h1>
<h2>This is heading 2</h2>
<h3>This is heading 3</h3>

Paragraph

The <p> element defines a paragraph of text. A paragraph always starts on a new line, and the browser automatically adds a small margin of white space before and after it.

<p>This is a paragraph.</p>
<p>This is another paragraph.</p>

Division

The <div> element defines a generic division, or section, of an HTML document with no semantic meaning of its own. It is used purely as a container, so that a group of elements, headings, paragraphs, images, forms, tables, can be styled together with CSS or manipulated together with JavaScript, typically by giving the <div> a class or id attribute. By default a browser places a line break before and after a <div>, since it is a block-level element.

Formating: b, i, small, sup, sub

HTML has several inline elements purely for text formatting. <b> renders bold text with no extra importance implied, while <strong> marks text as genuinely important (also rendered bold). <i> renders text in an alternate voice or mood, typically italic, while <em> marks text as emphasized (also rendered italic). <small> renders smaller text, commonly used for side comments or fine print. <sup> and <sub> render superscript and subscript text respectively, useful for footnotes and chemical formulas:

<p>This text is <b>bold</b> and this is <strong>important</strong>.</p>
<p>This text is <i>italic</i> and this is <em>emphasized</em>.</p>
<p>H<sub>2</sub>O is water; the reference is marked WWW<sup>1</sup>.</p>

Spacing: Pre, Br

The element inserts a single line break, forcing whatever follows onto a new line, and has no closing tag. The <pre> element marks preformatted text: the browser displays the text exactly as it is written in the source, including every space, tab and line break, using a monospace font, which makes it useful for code samples and ASCII layouts.

<p>First line Second line</p>
<pre>
function add(a, b) {
    return a + b;
}
</pre>

What this unit covers

  • Introduction to HTML
  • Elements of HTML Document
  • HTML Elements and HTML Attributes
  • Headings
  • Paragraph
  • Division
  • Formating: b, i, small, sup, sub
  • Spacing: Pre, Br
  • Formatting Text Phrases: span, strong, tt
  • Image element
  • Anchors
  • Lists: Ordered and Unordered and Definition
  • Tables
  • Frames
  • Forms: Form Elements, ID attributes, Class Attributes of HTML Elements
  • Meta Tag
  • Audio
  • Video
  • Canvas
  • Main
  • Section
  • Article
  • Header
  • Footer
  • Aside
  • Nav
  • Figure Tags
  • HTML Events: Window Events, Form Element Events, Keyboard Events, Mouse Events

Audio

2082 · 5 marks

Write an HTML script using audio tag. Set the autoplay, preload and loop properties to appropriate values. [5]

The following HTML script demonstrates the use of the <audio tag with autoplay, preload, and loop properties set to appropriate values. --- Attribute Value Used Description ------------------------------------ controls (boolean) Adds audio controls like play, pause, and volume to the player autop...

Open this answer on its own page →

HTML Events

2082 · 5 marks

Write an HTML script to demonstrate the onkeydown and onkeypress events. [5]

Show solved answer

HTML Script to Demonstrate onkeydown and onkeypress Events

Brief Explanation

Keyboard Events are:

AttributeDescription
onkeydownFires/runs when a user is pressing a key
onkeypressFires when user presses a key
onkeyupFires when user releases a key

These are event handlers written inside HTML tags that execute JavaScript when something happens (such as pressing a key).


HTML Script

<!DOCTYPE html>
<html>
<head>
    <title>onkeydown and onkeypress Event Demo</title>
    <style>
        body {
            font-family: Arial, sans-serif;
            margin: 40px;
        }
        input {
            padding: 8px;
            font-size: 16px;
            width: 300px;
        }
        #output {
            margin-top: 20px;
            padding: 10px;
            background-color: #f0f0f0;
            border: 1px solid #ccc;
            width: 320px;
        }
    </style>
</head>
<body>

    <h2>Keyboard Event Demo: onkeydown and onkeypress</h2>

    <p>Type something in the textbox below:</p>

    <input
        type="text"
        id="myInput"
        onkeydown="keyDownHandler(event)"
        onkeypress="keyPressHandler(event)"
        placeholder="Type here..."
    />

    <div id="output">
        <p id="keydown_msg">onkeydown: (waiting...)</p>
        <p id="keypress_msg">onkeypress: (waiting...)</p>
    </div>

    <script type="text/javascript">

        // Fires when a user is pressing a key (onkeydown)
        function keyDownHandler(event) {
            document.getElementById("keydown_msg").innerHTML =
                "onkeydown: Key pressed down = " + event.key;
        }

        // Fires when user presses a key (onkeypress)
        function keyPressHandler(event) {
            document.getElementById("keypress_msg").innerHTML =
                "onkeypress: Key pressed = " + event.key;
        }

    </script>

</body>
</html>

Output / Explanation

When the user types in the text box:

  • onkeydown fires first, as soon as the key is pushed down, and displays the key name in the output area.
  • onkeypress fires next, when the key press is registered, and also displays the key name.

Example output when user presses the key A:

onkeydown:  Key pressed down = A
onkeypress: Key pressed = A

Note: onkeypress does not fire for non-character keys (like Shift, Ctrl, Arrow keys), whereas onkeydown fires for all keys.

Open this answer on its own page →

2081 · 5 marks

What is onload event in HTML? Write an HTML script that displays alert message 'Hello Nepal' in onload event. [5]

Show solved answer

Onload Event in HTML

Definition

The onload event is an event handler in HTML/JavaScript that is triggered automatically when a web page (or an element such as an image) has completely finished loading in the browser. It is commonly used with the <body> tag.

  • It belongs to the category of Event Handlers -- JavaScript code placed inside HTML tags that execute when something happens.
  • The basic syntax of an event handler is:
name_of_handler = "JavaScript code goes here"

For the onload event specifically:

<body onload="JavaScript code or function call">

The onload event is useful for:

  • Displaying welcome messages when a page loads
  • Initializing variables or functions automatically
  • Checking cookies or browser information on page start

HTML Script: Display Alert 'Hello Nepal' on Onload Event

<html>
  <head>
    <title>Onload Event Example</title>
    <script type="text/javascript">
      function showMessage() {
        alert("Hello Nepal");
      }
    </script>
  </head>

  <body onload="showMessage()">
    <h2>Welcome to Nepal!</h2>
    <p>This page demonstrates the onload event in HTML.</p>
  </body>
</html>

Explanation of the Script

PartDescription
<script type="text/javascript">Defines the JavaScript block inside the <head> tag
function showMessage()A JavaScript function that triggers an alert box
alert("Hello Nepal")Displays a popup dialog with the message Hello Nepal
<body onload="showMessage()">Calls showMessage() automatically when the page finishes loading

How It Works

  1. The browser loads the HTML page completely.
  2. As soon as the <body> finishes loading, the onload event is fired.
  3. The showMessage() function is called automatically.
  4. An alert box appears with the message "Hello Nepal".
  5. The user clicks OK and sees the rest of the page content.

Note: The onload event handler follows the same event handling principle described in the curriculum: event handlers are placed inside HTML tags (not inside <script> tags) and execute JavaScript when a specific event occurs.

Open this answer on its own page →

2080 · 5 marks

What are HTML keyboard events? Write a script to display a message 'Hello world' on keypress event. [5]

Keyboard events are events that are triggered when a user interacts with the keyboard. The three keyboard event attributes are: Attribute Description ------------------------ onkeydown Fires/runs when a user is pressing a key (key held down) onkeypress Fires when a user presses a key onkeyup Fire...

Open this answer on its own page →

Tables

2081 · 5 marks

Write a HTML script to create a table with three rows and three columns. The second row third column should contain a text 'TU' with link set to www.tu.edu.np. The height of second row should be set to 200px. [5]

--- Element / Attribute Purpose ------ <table border="1" Creates the table with a visible border <tr Defines each table row <td Defines each table data cell (column) <tr height="200px" Sets the height of the second row to 200px <a href="http://www.tu.edu.np"TU</a Creates a hyperlink with text TU ...

Open this answer on its own page →

Lists

2079 · 5 marks

Write a HTML script to generate following list of items; 1. Coffee : I. Black coffee II. Green coffee 2. Tea A: Black tea B. Green tea [5]

Show solved answer

HTML Script for Nested Lists

Analysis of Required Output

The question requires:

  • Outer ordered list (numbered: 1, 2) for Coffee and Tea
  • Inner ordered list under Coffee with Roman numerals (I, II) for Black coffee and Green coffee
  • Inner ordered list under Tea with uppercase letters (A, B) for Black tea and Green tea

HTML Code

<!DOCTYPE html>
<html>
<head>
    <title>Nested List Example</title>
</head>
<body>

    <ol type="1">

        <li>Coffee
            <ol type="I">
                <li>Black coffee</li>
                <li>Green coffee</li>
            </ol>
        </li>

        <li>Tea
            <ol type="A">
                <li>Black tea</li>
                <li>Green tea</li>
            </ol>
        </li>

    </ol>

</body>
</html>

Output

1. Coffee
      I.  Black coffee
      II. Green coffee
2. Tea
      A. Black tea
      B. Green tea

Key Points (from notes)

Tag / AttributePurpose
<ol>Ordered list (numbered by default)
<li>Each list item
type="1"Numeric numbering (1, 2, 3...)
type="I"Uppercase Roman numerals (I, II, III...)
type="A"Uppercase alphabetic (A, B, C...)

Note: The type attribute in the <ol> tag is used to change the list marker style. Nesting is achieved by placing one <ol> inside a <li> of the outer <ol>.

Open this answer on its own page →

2078 · 5 marks

Create a HTML page containing ordered and unordered lists. Set the value of ordered list type to 'A'. The list should start at 'D'. [5]

  • Unordered list: Uses <ul tag; items marked with bullets by default using <li tag. - Ordered list: Uses <ol tag; items marked with numbers by default using <li tag. - The type attribute in <ol changes the marker style (e.g., type="A" for uppercase letters). - To start at a specific value, the st...
Open this answer on its own page →

Canvas

2079 · 5 marks

Create a HTML page containing a div having id 'div1'. The div should contain canvas element. The id of canvas should be 'mycanvas'. The height and width of canvas should be 200 and 300. [5]

The question asks us to create an HTML page with: - A <div element with id="div1" - Inside the div, a <canvas element with id="mycanvas" - Canvas height = 200 and width = 300 --- --- Part Description ------------------- <!DOCTYPE html Declares the document as HTML5 <div id="div1" Creates a block-...

Open this answer on its own page →

Image element

2078 · 5 marks

Create an HTML page with its body containing a div. The div should contain an image within it. Create a link on the image to redirect to the url http://www.tuiost.edu.np. Set the title of the page to “iost”. Add Meta tag on the page. [5]

--- Part Tag / Attribute Purpose --------- Page Title <titleiost</title Sets the browser tab title to "iost" Meta Tags <meta ... Provides metadata about the page (charset, description, keywords, author, viewport) Div Container <div Block-level container wrapping the image and link Anchor / Link <...

Open this answer on its own page →

Header

2076 · 5 marks

Create a HTML page with tags header, article and footer. Insert alink containing mail to [email protected] in the footer tag. Set the keywords 'iost', 'csit' using Meta tag in the page. [5]

--- Tag / Attribute Purpose ------ <meta name="keywords" content="iost, csit" Sets the keywords iost and csit for search engines. Goes inside <head. <header Defines the header section of the page (title, intro). <article Defines independent, self-contained content of the page. <footer Defines the...

Open this answer on its own page →

Frames

2076 · 5 marks

Why inline frames are used? Create a HTML page containing iframe within a paragraph. The iframe have source to http://www.tuiost.edu.np and its height and width are 200px and 400px respectively. [5]

As stated in the notes, the <iframe tag defines a rectangular region within the document on which the browser can display a separate document, including scrollbars and borders. Inline frames are used for the following reasons: - To embed another webpage inside the current webpage. - To embed YouT...

Open this answer on its own page →