Chapter 2 lab Chapter 3 lab Chapter 4 lab Chapter 5 lab Chapter 6 lab Chapter 7 lab Chapter 8 lab Chapter 9 lab Chapter 10 lab Chapter 12 lab Chapter 13 lab
Chapter 4 · Lab

Lab 2: HTML5

A hands-on HTML5 lab: block and inline elements, anchors, the new semantic elements, meter, video/audio, HTML5 form input types, the output element, SVG graphics and MathML.

Learning outcomes

  • Use block-level and inline elements to structure content
  • Link to specific parts of a page and create mailto links
  • Apply the HTML5 semantic elements: header, nav, section, article, aside, footer
  • Use the meter, video and audio elements
  • Apply HTML5 form input types such as email, url, date, month, week, time, number and range
  • Draw SVG graphics and use MathML

Your progress

Tasks completed 0/8

Log in to save your lab progress. Log in

1

Task 1 — Block level elements

Block level elements generally start on a new line, and any markup under or after a block level element also starts on a new line. Examples include headings, divisions, horizontal rules and lists.

  1. Create block.html with the code shown and open it in a browser.
  2. Notice the spacing around each block element.
  3. Add an <h3> heading and another list item.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Block Level Elements</title>
</head>
<body>
  <p>I am a paragraph.</p>
  <p>I am directly after the paragraph.</p>

  <h1>Level 1 Heading</h1>
  <h2>Level 2 Heading</h2>

  <div>This is a logical division.</div>
  <hr>
  <p>I will have a horizontal rule above me.</p>

  <ol>
    <li>I am part of an ordered list</li>
    <li>li stands for list item</li>
    <li>You can add as many as you want!</li>
  </ol>

  <ul>
    <li>I am still a list item</li>
    <li>This time I am inside an unordered list</li>
  </ul>
</body>
</html>

Try it

index.html
2

Task 2 — Inline elements & anchors

The <strong> tag defines important text and renders it bold. The <em> tag renders text italic to emphasise it.

We can assign id attributes to elements and use anchor tags to link to specific parts of a page. A mailto: link opens the user's email client.

  1. Create the page and open it in the browser.
  2. Click the "Back to Top" link to jump to the top of the page.
  3. Try the mailto link.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Inline Elements and Links</title>
</head>
<body>
  <div id="top">Top of the Page!</div>

  <p>The <strong>strong</strong> tag will render bold text.</p>
  <p>The <em>em</em> tag will render italic text.</p>

  <p><a href="#top">Back to Top of Page</a></p>

  <p><a href="mailto:sazilah@utem.edu.my?Subject=Email">Contact Us</a></p>
</body>
</html>

Try it

index.html
3

Task 3 — Semantic HTML5 structure

HTML5 lets us define a web page logically with <header>, <footer>, <nav>, <section>, <article> and <aside>.

  1. Create the page and open it in the browser.
  2. An <article> can contain several <section> elements.
  3. An <aside> holds content related to but aside from the main content.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>HTML5 Document Structure</title>
</head>
<body>
  <header role="banner">
    <h1>HTML5 Document Structure Example</h1>
    <p>This page should be tried in Safari, Chrome or Mozilla.</p>
  </header>

  <nav>
    <ul>
      <li><a href="https://www.tutorialspoint.com/html">HTML Tutorial</a></li>
      <li><a href="https://www.tutorialspoint.com/css">CSS Tutorial</a></li>
      <li><a href="https://www.tutorialspoint.com/javascript">JavaScript Tutorial</a></li>
    </ul>
  </nav>

  <article>
    <section>
      <p>One article can have multiple sections.</p>
    </section>
    <aside>
      <p>This is the aside part of the web page.</p>
    </aside>
  </article>

  <footer>
    <p>Created by <a href="https://www.tutorialspoint.com">Tutorials Point</a></p>
  </footer>
</body>
</html>

Try it

index.html
4

Task 4 — The meter element

The HTML5 <meter> element displays a gauge. It takes the attributes min, max and value, and can also use low and high. The text between the tags is fallback text for older browsers.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>The Meter Element</title>
</head>
<body>
  <p>Disk usage:</p>
  <meter min="0" max="100" value="50">50%</meter>

  <p>Battery level:</p>
  <meter min="0" max="100" value="80" low="30" high="70">80%</meter>

  <p>The text between the meter tags is fallback text for older browsers.</p>
</body>
</html>

Try it

index.html
5

Task 5 — Video & audio

HTML5 can play video and audio natively with the <video> and <audio> elements. Always use the controls attribute so users can operate the player, and provide multiple <source> formats for cross-browser support.

The media files will not exist in the preview, so you will see the fallback text — that is expected.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Video and Audio</title>
</head>
<body>
  <h2>Video</h2>
  <video width="640" controls poster="thumb.jpg">
    <source src="movie.mp4" type="video/mp4">
    <source src="movie.webm" type="video/webm">
    Your browser does not support the video tag.
  </video>

  <h2>Audio</h2>
  <audio controls>
    <source src="audio.mp3" type="audio/mpeg">
    <source src="audio.ogg" type="audio/ogg">
    Your browser does not support the audio element.
  </audio>
</body>
</html>

Try it

index.html
6

Task 6 — HTML5 form input types

HTML5 introduced several new values for the type attribute: datetime-local, date, month, week, time, number, range, email and url.

  1. Create the form and open it in the browser.
  2. Try submitting an invalid email or URL — the browser validates it for you.
  3. Move the range slider and change the number with the spinner.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>HTML5 Form Input Types</title>
</head>
<body>
  <form action="" method="post">
    <label for="firstname">First name:</label>
    <input type="text" id="firstname" name="firstname"><br><br>

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

    <label for="url">Website:</label>
    <input type="url" id="url" name="url"><br><br>

    Date and Time: <input type="datetime-local" name="dtimelocal"><br><br>
    Month: <input type="month" name="mth"><br><br>
    Week: <input type="week" name="wk"><br><br>
    Time: <input type="time" name="tm"><br><br>

    Select Number:
    <input type="number" min="0" max="100" step="10" value="10" name="num"><br><br>

    Select Range:
    <input type="range" min="0" max="10" step="1" value="5" name="range"><br><br>

    <input type="submit" value="Send">
    <input type="reset">
  </form>
</body>
</html>

Try it

index.html
7

Task 7 — The output element

The <output> element represents the result of a calculation or user interaction. The for attribute links it to the input element(s) that produced the result.

  1. Type a value and click Result.
  2. The value is copied into the output element by the script.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>Output Element</title>
  <script>
    function showResult() {
      var x = document.forms["myform"]["newinput"].value;
      document.forms["myform"]["result"].value = x;
    }
  </script>
</head>
<body>
  <form action="" method="get" name="myform">
    Enter a value: <input type="text" name="newinput">
    <input type="button" value="Result" onclick="showResult()">
    <br><br>
    The value entered is <output name="result" for="newinput"></output>
  </form>
</body>
</html>

Try it

index.html
8

Task 8 — SVG graphics & MathML

SVG (Scalable Vector Graphics) describes 2D graphics in XML. It is ideal for vector diagrams such as pie charts and graphs. Shapes include <circle>, <rect>, <line>, <polygon> and <ellipse>, and gradients use <radialGradient> / <linearGradient>.

MathML lets you write mathematical notation directly in HTML.

Code

index.html
<!DOCTYPE html>
<html>
<head>
  <meta charset="utf-8">
  <title>SVG and MathML</title>
</head>
<body>
  <h2>SVG Circle</h2>
  <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <circle cx="100" cy="100" r="50" fill="red" />
  </svg>

  <h2>SVG Rectangle</h2>
  <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <rect x="20" y="20" width="160" height="100" fill="blue" />
  </svg>

  <h2>SVG Line</h2>
  <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <line x1="10" y1="10" x2="190" y2="100" style="stroke:blue;stroke-width:5" />
  </svg>

  <h2>SVG Polygon</h2>
  <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <polygon points="100,10 40,190 190,190" fill="red" />
  </svg>

  <h2>SVG Gradient (radial)</h2>
  <svg width="200" height="200" xmlns="http://www.w3.org/2000/svg">
    <defs>
      <radialGradient id="gradient" cx="50%" cy="50%" r="50%">
        <stop offset="0%" stop-color="white" />
        <stop offset="100%" stop-color="red" stop-opacity="1" />
      </radialGradient>
    </defs>
    <ellipse cx="100" cy="100" rx="90" ry="60" fill="url(#gradient)" />
  </svg>

  <h2>MathML — Pythagorean theorem</h2>
  <math xmlns="http://www.w3.org/1998/Math/MathML">
    <msup><mi>a</mi><mn>2</mn></msup>
    <mo>+</mo>
    <msup><mi>b</mi><mn>2</mn></msup>
    <mo>=</mo>
    <msup><mi>c</mi><mn>2</mn></msup>
  </math>
</body>
</html>

Try it

index.html