Module 4: Links, Images & Media

The web wouldn't be "HyperText" without links, and it would be very boring without media. In this module, we will explore how to connect webpages together and how to embed images, audio, and video directly into your HTML.

2. Images & Responsive Art

The <img> Tag

The <img> tag is self-closing and requires two primary attributes: src (source) and alt (alternative text).

<img src="profile.jpg" alt="A photo of the author coding" width="400">

Crucial: The alt attribute is required for web accessibility (screen readers read it aloud) and is displayed if the image fails to load.

Responsive Images using <picture>

If you want to serve different images depending on screen size (e.g., a wide image for desktops and a tall image for phones), use the <picture> element.

<picture>
  <source media="(max-width: 600px)" srcset="img-mobile.jpg">
  <source media="(min-width: 601px)" srcset="img-desktop.jpg">
  <img src="img-fallback.jpg" alt="Responsive scenery">
</picture>

SVG (Scalable Vector Graphics)

SVGs are mathematical drawings defined in XML format. Unlike JPEGs or PNGs, SVGs never lose quality when scaled up. You can embed SVG code directly into HTML!

<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" stroke="blue" stroke-width="4" fill="lightblue" />
</svg>

3. Multimedia: Audio & Video

HTML5 introduced native tags for playing audio and video without needing third-party plugins like Flash.

The <audio> Tag

Use the controls attribute to display the browser's default play/pause/volume interface.

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

The <video> Tag

The video tag works similarly but accepts a poster attribute (a thumbnail image shown before the video plays) and width/height attributes.

<video width="600" controls poster="thumbnail.jpg">
  <source src="tutorial.mp4" type="video/mp4">
  
  <!-- Adding Subtitles (Captions) -->
  <track src="subtitles_en.vtt" kind="subtitles" srclang="en" label="English">
</video>

YouTube Embedding (<iframe>)

Often, it's better to host large videos on platforms like YouTube and embed them in your site using an <iframe> (Inline Frame).

<iframe width="560" height="315" 
  src="https://www.youtube.com/embed/dQw4w9WgXcQ" 
  title="YouTube video player" 
  frameborder="0" 
  allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture" 
  allowfullscreen>
</iframe>