By WebNest Studio Engineering Team
HTML Tutorial
HTML Images and Responsive Images
The <img> element embeds images into a page. Unlike most HTML elements, it is a self-closing (void) element with no separate closing tag, and it depends heavily on a few attributes to behave correctly, accessibly, and efficiently.
Why it matters
Images are usually the largest files on a page, and the most common accessibility failure on the web is an image with no text alternative. Three attributes do most of the work: alt for people who cannot see the image, width and height to stop the page jumping as it loads, and loading to delay images that are off screen.
The src and alt Attributes
src points to the image file, either as a relative or absolute path. alt provides a text alternative describing the image's content or purpose, which is read aloud by screen readers, displayed if the image fails to load, and used by search engines to understand image content. Purely decorative images (a background flourish with no informational value) should use alt="" (an empty but present attribute) so screen readers skip over them silently instead of reading a distracting filename.
Width, Height, and Layout Shift
Setting explicit width and height attributes on an <img> lets the browser reserve the correct amount of space before the image finishes loading, preventing the surrounding content from visibly jumping around as images load in — a problem known as cumulative layout shift. CSS can still control the final displayed size responsively; the HTML attributes primarily establish the image's aspect ratio for layout reservation.
Responsive Images with srcset
The srcset attribute lets you offer multiple versions of an image at different resolutions, so the browser can pick the most appropriate one for the user's screen density and viewport size instead of always downloading one large file. This improves load performance on smaller or lower-density screens without sacrificing quality on larger, high-resolution ones.
Example
An accessible image with explicit dimensions and responsive sources
<img
src="team-photo-800w.jpg"
srcset="team-photo-400w.jpg 400w, team-photo-800w.jpg 800w, team-photo-1200w.jpg 1200w"
sizes="(max-width: 600px) 400px, 800px"
width="800"
height="533"
alt="The Webnest Studio team standing outside the office">
Displays a photo of the team, reserving 800x533 space to avoid layout shift, while the browser downloads whichever source image best matches the viewer's screen size; a screen reader announces "The Webnest Studio team standing outside the office."
Common Mistakes
- Leaving alt text empty (or missing entirely) on meaningful images, making them invisible to screen reader users.
- Writing unhelpful alt text like "image123.jpg" instead of a description of the image's content or purpose.
- Omitting width and height attributes, causing visible layout shift as images load.
- Using one oversized image for all screen sizes instead of srcset, wasting bandwidth on mobile devices.
Exercise
Add two images. The first is a meaningful photo, team.jpg, 600 by 400 pixels, with the alternative text The WebNest team at their desks, loaded lazily. The second is a purely decorative divider, divider.png, which screen readers should skip.
Expected result: two img elements with correct attributes (the files do not exist, so the browser shows the alternative text of the first and nothing for the second).
<h1>About us</h1>
<!-- TODO: the team photo -->
<!-- TODO: the decorative divider -->
Show hints
- Lazy loading is requested with
loading="lazy". - A decorative image still needs the attribute, but empty:
alt="".
Show solution
<h1>About us</h1>
<img src="team.jpg" alt="The WebNest team at their desks" width="600" height="400" loading="lazy">
<img src="divider.png" alt="">
Quick quiz
1. What is the correct alt for a purely decorative image?
- A. alt="image"
- B. No alt attribute at all
- C. An empty value: alt=""
- D. alt="decorative"
Show answer
C. An empty alt tells screen readers to skip the image. With no alt attribute, many read out the file name.
2. Why set width and height on an image?
- A. To compress the file
- B. To improve the image quality
- C. It is required for the image to display
- D. So the browser reserves the right space before the image loads, which prevents the layout from shifting
Show answer
D. The browser works out the aspect ratio from the two values.
3. What does the srcset attribute do?
- A. Lists versions of the image so the browser can choose one suited to the screen
- B. Sets the alternative text
- C. Adds a caption
- D. Loads the image twice
Show answer
A. A phone can download a small file and a large display a sharper one.
Interview questions
How do you write good alt text?
Show answer
Describe the content and purpose of the image in the context of the page, briefly, as you would to someone on the phone. Do not start with "image of", because the screen reader already announces that it is an image. If the image is a link or a button, describe where it goes or what it does. If the image is decorative or repeats text beside it, use an empty alt="".
How would you make images load efficiently?
Show answer
Serve them at the size they are displayed and in an efficient format such as WebP or AVIF. Use srcset and sizes, or the picture element, so that small screens download small files. Add loading="lazy" to images below the first screen, but not to the main image at the top. Always give width and height to avoid layout shift.
Key Points to Remember
- <img> is a self-closing element requiring src and a meaningful alt attribute.
- Use alt="" (empty) only for purely decorative images so screen readers skip them.
- width/height attributes reserve layout space and reduce cumulative layout shift.
- srcset lets the browser choose the best-fitting image source for the viewer's device.
Practice in the playground
Write and run your own code for this topic, or start from the exercise or an example. Shows a live preview of your HTML, CSS and JavaScript. Your code for each topic is kept in this browser.
Run the examples
Change an input, predict the result, then compare it with the output. Explain why the result changes.
Written by
WebNest Studio Engineering Team
Practical lessons from developers working with Java, Python, React, databases and production systems.
Led by Vansh Duggal, Founder, WebNest Studio · Full-stack & software engineering