Practical Exercise 5: Implementing Images in HTML
In this practical exercise, you'll learn to insert and configure images in HTML, using the <img> tag and essential attributes like src, alt, width, and height to improve accessibility and visual presentation.
Activity
Create an HTML document that includes an image using the <img> tag and its alt, width, and height attributes. The image can be from an external source to your website.
Interactive Solution
Solution with Complete HTML Code
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Images</title>
</head>
<body>
<h1>Image</h1>
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Description of the image"
width="360"
height="200"
/>
</body>
</html>
🧪 Exercise Variants
Variant 1: Image with <figure> and <figcaption>
For images with context or captions, HTML5 introduced the semantic tags <figure> and <figcaption>.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Image with Figure</title>
</head>
<body>
<h1>Example Gallery</h1>
<figure>
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="DivZone Docs social card showing logo and branding"
width="360"
height="200"
/>
<figcaption>Fig. 1 - DivZone Docs showcase card</figcaption>
</figure>
<figure>
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Second view of the DivZone social card"
width="300"
height="167"
/>
<figcaption>Fig. 2 - Reduced version of the same image</figcaption>
</figure>
</body>
</html>
<figure> groups the image with its description, and <figcaption> provides the caption text. This improves semantics and accessibility.
Variant 2: Performance Optimization with loading="lazy"
For pages with many images, the loading="lazy" attribute delays loading of off-screen images, speeding up the initial page load.
HTML
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Lazy Images</title>
</head>
<body>
<h1>Page with Many Images</h1>
<!-- The first image loads normally (it's visible) -->
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Image visible on load"
width="360"
height="200"
/>
<!-- Images with lazy loading: only load on scroll -->
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Image that loads on scroll"
width="360"
height="200"
loading="lazy"
/>
<img
src="https://docs.div.zone/img/divzone-social-card.jpg"
alt="Another deferred image"
width="360"
height="200"
loading="lazy"
decoding="async"
/>
</body>
</html>
loading="lazy": loads the image only when it's about to enter the viewport.decoding="async": allows the image to be decoded in the background without blocking page rendering.