HTML Image Tag: Attributes, Image Formats, and Examples
What is HTML Image Tag: Attributes, Image Formats, and Examples? The HTML tag is a void (self-closing) element used to embed images on web pages. Essential attributes include 'src' (image URL), 'alt' (alternative descriptive...
How do you embed and optimize images using the <img> tag, picture element, and modern formats?
Explanation & In-Depth Details
2) Crucial Accessibility: The 'alt' Attribute: The 'alt' attribute provides alternative text read aloud by screen readers for visually impaired users. It also appears if the image fails to load and is indexed by search engines for Google Image Search. Decorative images must use alt="" so screen readers ignore them.
3) Preventing Cumulative Layout Shift (CLS): Always declare explicit 'width' and 'height' aspect ratios directly on the <img> tag. This allows browsers to reserve layout space before the image bytes download, preventing sudden page layout shifts.
4) Performance Optimization with Lazy Loading: Adding 'loading="lazy"' instructs the browser to defer downloading images located below the fold until the user scrolls near them, dramatically accelerating initial page load.
5) Modern Image Formats:
- AVIF & WebP: Next-gen formats providing superior compression at fraction of JPG/PNG sizes.
- SVG: Scalable Vector Graphics, infinitely sharp at any resolution; ideal for logos and icons.
- PNG: Lossless compression supporting alpha transparency; ideal for UI screenshots.
- JPG: Lossy compression for detailed photographs.
6) The Responsive <picture> Element: Wraps multiple <source> tags with media queries to provide art direction (different image crops for mobile vs desktop) or next-gen format fallbacks.
Code Example
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Optimized Images Showcase</title>
</head>
<body>
<h2>Optimized Image Embedding</h2>
<!-- Standard Responsive Image with Lazy Loading -->
<img src="https://images.unsplash.com/photo-1498050108023-c5249f4df085?w=600"
alt="Web developer writing code on modern laptop"
width="600"
height="400"
loading="lazy"
style="max-width: 100%; height: auto; border-radius: 8px;">
<h2>Next-Gen Picture Element with Fallback</h2>
<picture>
<source srcset="/images/hero.avif" type="image/avif">
<source srcset="/images/hero.webp" type="image/webp">
<img src="/images/hero.jpg" alt="Company Headquarters Architecture" width="800" height="450" loading="lazy">
</picture>
</body>
</html>Interview Question & Tips
A1: Screen readers may announce the raw image filename or full URL (e.g. 'image-109283-final-v2.jpg'), creating an irritating and confusing user experience. Additionally, automated accessibility audits (Lighthouse) will fail.
Q2: Why should width and height attributes be included even if CSS controls the final responsive size?
A2: Modern browsers calculate the image's intrinsic aspect ratio from the HTML width and height attributes. This allows the browser to reserve the exact layout space, preventing Cumulative Layout Shift (CLS).
Q3: When should you use srcset vs the <picture> element?
A3: Use 'srcset' with 'sizes' when serving the exact same image at different resolutions for retina/mobile screens. Use '<picture>' when serving different image crops (art direction) or serving modern formats with fallback.
Best Practices
This study reference for HTML Image Tag: Attributes, Image Formats, and Examples is curated and verified against recognized curriculum benchmarks, peer-reviewed textbooks, and official technical documentation: