Learnwizy Technologies Logo

Learnwizy Technologies

Class 2: HTML Forms, Multimedia and More

Today we'll dive into how to make your web pages interactive by collecting user input using HTML forms and enriching your content with multimedia elements and much more.


HTML Forms: Gathering User Input

Forms are essential for any interactive website, allowing users to submit information, log in, search, and more. In this section, we will explore the fundamental concepts of HTML forms.

Basic Form Structure

The <form> element is the container for all input elements. Key attributes include action and method which specify where and how the form data will be sent when submitted.

<form action="/submit-form" method="POST">
    <!-- Form elements go here -->
</form>

Input Types and Elements

The <input> element is the most versatile form control. Its behavior changes based on the type attribute:

<form action="/login" method="POST">
    <label for="username">Username:</label><br />
    <input type="text" id="username" name="username" /><br /><br />

    <label for="email">Email:</label>
    <input type="email" id="email" name="email"><br><br>

    <label for="password">Password:</label><br />
    <input type="password" id="password" name="password" /><br /><br />

    <input type="checkbox" id="subscribe" name="subscribe" value="yes" />
    <label for="subscribe">Subscribe to newsletter</label><br /><br />

    <input type="radio" id="html" name="fav_language" value="HTML" />
    <label for="html">HTML</label><br />
    <input type="radio" id="css" name="fav_language" value="CSS" />
    <label for="css">CSS</label><br /><br />

    <input type="submit" value="Submit" />
</form>

Other Form Elements

<form action="/login" method="POST">
    <label for="message">Your Message:</label><br />
    <textarea id="message" name="message" rows="4" cols="50"></textarea><br /><br />

    <label for="country">Country:</label><br />
    <select id="country" name="country">
        <option value="usa">USA</option>
        <option value="canada">Canada</option>
        <option value="uk">UK</option></select><br /><br />

    <fieldset>
        <legend>Contact Information:</legend>
        <label for="email">Email:</label><br />
        <input type="email" id="email" name="email" />
    </fieldset>
    <br />

    <button type="submit">Send Inquiry</button>
</form>

Form Attributes and Client-Side Validation

Important attributes control form behavior, such as action (where to send data), method (HTTP method like GET or POST), and name (for identifying form controls). Basic client-side validation is available using HTML5 attributes like required, minlength, maxlength, and pattern to ensure data integrity before submission.

<form action="/login" method="POST">
    <label for="password">Password (min 8 chars):</label>
    <input type="password" id="password" name="password" required minlength="8" />
    <button type="submit">Login</button>
</form>

Embedding Multimedia Content

HTML5 introduced standard ways to embed audio and video directly into web pages without the need for third-party plugins.

Image Element

The <img> element is used to embed images. The essential attributes are src (source of the image) and alt (alternative text for accessibility and when the image fails to load). Attributes like width and height are used for controlling dimensions.

<img src="images/logo.png" alt="Company Logo" width="150" height="75">
<img src="https://example.com/flower.jpg" alt="A red flower in bloom">

Audio Element

The <audio> element allows you to embed sound content into your web page. Some common attributes are:

<audio controls autoplay loop>
    <source src="audio/background-music.mp3" type="audio/mpeg">
    Your browser does not support the audio element.
</audio>

Video Element

The <video> element is used to embed video content. Key attributes include:

<video width="640" height="360" controls preload="metadata">
    <source src="video/intro.mp4" type="video/mp4">
    <source src="video/intro.webm" type="video/webm">
    Your browser does not support the video tag.
</video>

Iframes: Embedding Other Documents

An <iframe> is used to embed another HTML document within the current HTML document. It's often used for embedding maps, videos from external sites (like YouTube), or other web pages.

Be cautious with iframes, especially regarding security and performance, as they load an entirely separate web page.

<iframe src="https://www.example.com" width="600" height="400" title="Example Website" ></iframe>

HTML Tables: Structuring Tabular Data

HTML tables are used to display data in a grid-like format, organized into rows and columns. They are ideal for presenting structured data such as financial reports, product lists, or schedules.

Basic Table Structure

A basic HTML table consists of the <table> element, which contains <thead> (table header), <tbody> (table body), and optionally <tfoot> (table footer). Inside these, you'll find <tr> (table row) elements, which in turn contain <th> (table header cell) or <td> (table data cell) elements.

<table>
  <thead>
    <tr>
      <th>Header 1</th>
      <th>Header 2</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>Data 1A</td>
      <td>Data 1B</td>
    </tr>
    <tr>
      <td>Data 2A</td>
      <td>Data 2B</td>
    </tr>
  </tbody>
  <tfoot>
    <tr>
      <td>Footer 1</td>
      <td>Footer 2</td>
    </tr>
  </tfoot>
</table>

Table Attributes (Basic)

While CSS is primarily used for styling tables, some basic HTML attributes can be useful:

<table>
  <thead>
    <tr>
      <th>Name</th>
      <th colspan="2">Contact Info</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <td>John Doe</td>
      <td>john@example.com</td>
      <td rowspan="2">555-1234</td>
    </tr>
    <tr>
      <td>Jane Smith</td>
      <td>jane@example.com</td>
    </tr>
  </tbody>
</table>

More HTML Elements and Concepts

HTML Void Elements

Some elements are considered void elements (also known as self-closing tags). These tags do not have a closing tag because they cannot have any child nodes (content or other elements) inside them. They are typically used to embed resources or insert content that doesn't encapsulate other content. Void tags often have attributes that provide necessary information for them to function.

The Preformatted Text Element

The HTML <pre> element represents preformatted text. Text within this element is typically displayed in a non-proportional (monospaced) font, exactly as it is written in the HTML file. This means that whitespace (spaces, tabs, newlines) inside the <pre> tag is preserved, unlike regular HTML text where multiple spaces are collapsed into one, and newlines are ignored.

The <pre> element is commonly used to display:

Here's an example:

function greet(name) {
    console.log(`Hello, ${name}!`);
}

greet("World");

Without the <pre> tag, the above Javascript code would lose its indentation and formatting when displayed in a browser. Just see below:

function greet(name) { console.log(`Hello, ${name}!`); } greet("World");

HTML Entities: Displaying Special Characters

HTML entities are used to display reserved characters (like < or >) or characters not found on a standard keyboard (like copyright symbols).

<p>This is less than &lt; and greater than &gt;.</p>
<p>&copy; 2023 My Website</p>