30 Mins

5. Images & Media

Images (img, alt tags)

The <img> element is used to embed an image into a webpage. It is a self-closing tag that requires the src attribute to specify the image path and the alt attribute to provide alternative text.

The alt attribute is mandatory for web accessibility. It provides a text description for screen readers, allowing users with visual impairments to understand the image content. If an image is purely decorative, use an empty alt="" attribute to ensure screen readers skip it.

Operator Note: Always include width and height attributes to prevent layout shifts as the browser loads the image.
HTML
<img src="logo.png" alt="Company logo for VoidX Academy" />

SVG integration

SVG (Scalable Vector Graphics) is an XML-based format for vector images. Unlike raster formats, SVGs remain sharp at any resolution, making them ideal for logos, icons, and diagrams.

You can embed SVGs directly into your code using the <svg> tag. This allows for direct styling and animation using CSS or JavaScript, providing deeper control over visual presentation than external image files.

Operator Note: Use SVGs for icons to ensure they look crisp on high-DPI (Retina) displays.
HTML
<svg width="100" height="100">
  <circle cx="50" cy="50" r="40" fill="blue" />
</svg>

Picture Element for responsiveness

The <picture> element is a wrapper that provides a way to serve different versions of an image based on screen size or device capabilities. It works by nesting <source> tags containing media queries alongside a fallback <img> tag.

This allows you to load smaller image files for mobile devices and larger, higher-resolution assets for desktops, significantly improving page load performance and user experience.

Deep Dive: Advanced Media Query Logic

Media queries apply styles based on device characteristics. While min-width and max-width are most common, you can query much more:

  • orientation: (orientation: portrait) or (orientation: landscape).
  • hover capability: (hover: hover) ensures you don't apply confusing hover states on touch devices.
  • Logical Operators: You can chain conditions using and, or provide fallback alternatives using commas (which act like OR).
Operator Note: Always place the <img> tag as the last child of the <picture> element to serve as the fallback for older browsers.
HTML
<picture>
  <source media="(min-width: 800px)" srcset="large.jpg" />
  <img src="small.jpg" alt="<strong>Responsive design</strong> example" />
</picture>

Audio & Video elements

The <audio> and <video> tags are used to embed multimedia content directly into the document without third-party plugins. Both tags support the controls attribute, which provides users with playback tools like play, pause, and volume.

For accessibility, you should always provide captions for videos using the <track> element. It is also good practice to provide multiple source formats (e.g., MP4 and WebM) to ensure cross-browser compatibility.

Operator Note: Avoid auto-playing audio or video content as it can be jarring for users, especially those using screen readers.
HTML
<video controls width="400">
  <source src="video.mp4" type="video/mp4" />
  Your browser does not support the video tag.
</video>

Iframe embeds

The <iframe> (inline frame) element allows you to embed another HTML document within the current page. This is frequently used for maps, social media feeds, or embedded video players from third-party services.

While powerful, iframes should be used sparingly due to security risks and potential performance impacts. Always use the title attribute on your iframe to help screen readers identify the purpose of the embedded content.

Operator Note: Use the sandbox attribute to restrict what the iframe content can do, such as preventing scripts from executing or forms from submitting.
HTML
<iframe src="https://example.com" title="External embedded content"> </iframe>
HTML
<!-- Try it: sandbox -->
<iframe src="embed.html" sandbox="allow-scripts allow-same-origin"></iframe>

Figure and Figcaption

The <figure> element is used to group self-contained content, such as images, charts, or code snippets, often with a caption. The <figcaption> element serves as the label for the content within the <figure>.

Using these tags semantically identifies that the image or media is related to the main text, improving the document structure for both search engines and assistive technology.

Operator Note: Only use <figure> for content that is supplementary to the main text; it should be able to move independently from the rest of the document without losing meaning.
HTML
<figure>
  <img src="chart.jpg" alt="Sales data" />
  <figcaption>Figure 1: Quarterly sales growth report.</figcaption>
</figure>

Knowledge Check

Ready to test your understanding of 5. Images & Media?

Back