Cyber Code Academy
Tutorials Academy Courses
Home / html-tutorials-md / HTML Responsive Images

HTML Responsive Images

> Serve appropriate image sources using srcset, sizes, picture, source, and responsive image practices.

**Cyber Code Academy · HTML Tutorial · Lesson 39 · Intermediate**

## 1. Why responsive images?
Different devices have different screen sizes, pixel densities, and network conditions. Serving one very large image to every visitor can waste bandwidth; serving one small image can look blurry on high-resolution displays.

Responsive images allow the browser to choose a suitable resource.

## 2. Use srcset with width descriptors

“`html
HTML course students learning together
“`

srcset lists alternative files and their intrinsic widths. sizes tells the browser how wide the image is expected to appear at different viewport conditions. The browser uses these hints to choose a resource.

## 3. Density descriptors

For images with the same intrinsic dimensions but different pixel densities, use x descriptors:

“`html
Cyber Code Academy logo
“`

The browser can choose a higher-density version for a high-density display.

## 4. Use picture for art direction

Sometimes mobile and desktop need different crops rather than simply different resolutions. The picture element allows media conditions:

“`html

Instructor teaching HTML in a classroom

“`

The browser checks the source elements and uses a suitable candidate. The img element provides the fallback and alternative text.

## 5. Use modern image formats

“`html

HTML course cover

“`

Provide a fallback format when needed for your target browser support. Make sure every referenced file exists and is correctly served.

## 6. Performance practices
– Resize images to the dimensions needed.
– Compress images appropriately.
– Provide intrinsic width and height.
– Lazy-load images below the initial viewport.
– Do not lazy-load the main hero image by default.
– Prefer modern efficient formats where supported.
– Use descriptive filenames and suitable alternative text.

## 7. Common mistakes
– Forgetting the fallback img inside picture.
– Using incorrect width descriptors.
– Writing sizes values that do not match actual layout.
– Providing several files that are not real alternatives.
– Lazy-loading an important above-the-fold image.
– Using the same alt text regardless of image purpose.

## 8. Practice exercises
1. Create small, medium, and large versions of a tutorial image.
2. Add srcset using width descriptors.
3. Add sizes for mobile and desktop.
4. Use picture for a mobile crop and desktop crop.
5. Add an AVIF or WebP source with a JPEG fallback.
6. Inspect the selected resource in browser Developer Tools.

## 9. Knowledge check
1. Why are responsive images useful?
2. What do width descriptors represent?
3. What does sizes tell the browser?
4. How is picture different from srcset?
5. Why should dimensions be included?
6. When should loading=”lazy” be used?

**Next lesson:** HTML Best Practices and Final Review.

Want to learn this professionally?

Explore practical IT training at Cyber Code Academy.

View Academy Courses