Abstract representation of HTML Image Optimization pipeline showing formats like AVIF and WebP
HTML Tutorial8 min read

By Akshay Singh

Share this tutorial:

The Complete Guide to HTML Image Optimization in 2026

Image optimization remains one of the most critical factors in web performance. Unoptimized images are the leading cause of slow Largest Contentful Paint (LCP) times and Cumulative Layout Shift (CLS) issues, which directly impact both user experience and SEO rankings.

In this tutorial, we will cover the practical HTML techniques required to serve images efficiently in modern web development. We will focus on responsive loading, next-generation formats, and preventing layout shifts.

The Problem with the Standard <img> Tag

Historically, serving an image required a single HTML tag:

<img src="hero-image.jpg" alt="A beautiful landscape" />

While this still works, it has several major flaws for a modern application:

  1. No Format Choice: It serves a JPEG to every browser, even if the browser supports more efficient formats like AVIF or WebP.
  2. No Responsive Sizing: A user on a 320px-wide mobile screen downloads the exact same 4000px-wide file as a user on a 4K desktop monitor.
  3. Layout Shifts: Because the browser doesn't know the dimensions of the image until the file downloads, the content below the image will jump down once it renders, causing CLS.

We can fix all of these issues using modern HTML attributes.

1. Preventing Cumulative Layout Shift (CLS)

When the browser parses HTML, it encounters <img> tags before the actual image assets finish downloading. Without explicit width and height dimensions, the browser assigns the image an initial size of 0x0 pixels. Once the image binary arrives, the layout shifts unexpectedly.

Provide explicit width and height attributes on the <img> tag:

<img 
  src="hero-image.jpg" 
  alt="A beautiful landscape"
  width="800" 
  height="450" 
/>

In your CSS, you ensure the image scales responsively without distorting:

img {
  max-width: 100%;
  height: auto;
}

This simple combination eliminates layout shifts caused by images.

2. Using Next-Generation Formats (AVIF and WebP)

JPEGs and PNGs are no longer the most efficient formats for the web.

  • AVIF: Currently the most efficient image format, often providing 50% smaller file sizes than JPEG with comparable visual quality. It is widely supported in modern browsers.
  • WebP: The standard for broad compatibility (supported by 97%+ of browsers), typically offering 25–35% smaller file sizes than JPEG.

Instead of writing a complex JavaScript detection script, we use the HTML <picture> element to let the browser choose the best format it supports.

<picture>
  <!-- 1. The browser checks if it supports AVIF. If yes, it uses this. -->
  <source srcset="image.avif" type="image/avif" />
  
  <!-- 2. If it doesn't support AVIF, it checks WebP. -->
  <source srcset="image.webp" type="image/webp" />
  
  <!-- 3. If neither are supported (e.g., older browsers), it falls back to JPEG. -->
  <img 
    src="image.jpg" 
    alt="Developer working on code" 
    width="800" 
    height="450"
  />
</picture>

The browser evaluates the <source> tags from top to bottom. The <img> tag at the end is required; without it, nothing will render. The <img> tag is also where you apply your alt, width, and height attributes.

3. Serving Responsive Sizes with srcset and sizes

Serving a 2000px wide image to a mobile device wastes bandwidth and slows down the page. We want to provide the browser with a menu of available sizes and instructions on how wide the image will be displayed on the screen.

We use the srcset and sizes attributes for this.

<img 
  src="fallback-800w.jpg"
  alt="Team meeting"
  srcset="
    image-400w.jpg 400w,
    image-800w.jpg 800w,
    image-1200w.jpg 1200w
  "
  sizes="(max-width: 600px) 100vw, 800px"
  width="800"
  height="450"
/>

Understanding srcset

The srcset attribute tells the browser: "Here are three versions of this image, and here is exactly how wide each file is in pixels (400w, 800w, 1200w)."

Understanding sizes

The sizes attribute tells the browser: "If the viewport is 600px or less, this image will take up 100% of the viewport width (100vw). Otherwise, it will be displayed at exactly 800px wide."

The browser then combines this information with the device's pixel density (e.g., a Retina display) to download the smallest possible file that will look sharp.

4. Combining <picture> with srcset

For maximum optimization, you can combine the format switching of the <picture> tag with the responsive sizing of srcset.

<picture>
  <source 
    type="image/avif"
    srcset="hero-400w.avif 400w, hero-800w.avif 800w, hero-1200w.avif 1200w"
    sizes="(max-width: 600px) 100vw, 800px"
  />
  
  <source 
    type="image/webp"
    srcset="hero-400w.webp 400w, hero-800w.webp 800w, hero-1200w.webp 1200w"
    sizes="(max-width: 600px) 100vw, 800px"
  />
  
  <img 
    src="hero-800w.jpg"
    alt="Main application dashboard"
    width="800"
    height="450"
  />
</picture>

This represents the gold standard for HTML image optimization. The browser selects the best supported format and the most appropriate resolution for the user's current screen size.

5. Lazy Loading Below the Fold

Images that are not immediately visible when the page loads (below the fold) should not block the initial rendering of the page.

HTML natively supports lazy loading using the loading="lazy" attribute.

<img 
  src="footer-illustration.jpg" 
  alt="Decorative footer illustration"
  width="400"
  height="200"
  loading="lazy" 
/>

Important: Do not apply loading="lazy" to images that are visible above the fold (like a hero image). Lazy loading an above-the-fold image will delay your Largest Contentful Paint (LCP) and hurt your Core Web Vitals score.

Summary Checklist

Before deploying images to production, verify these requirements:

  1. Dimensions: Are width and height attributes present to prevent CLS?
  2. Formats: Are you serving AVIF and WebP using the <picture> tag?
  3. Responsive: Are you using srcset and sizes so mobile devices don't download desktop-sized files?
  4. Loading: Is loading="lazy" applied to images below the fold?
  5. Priority: Is fetchpriority="high" (or just avoiding lazy) applied to your critical hero image?
  6. Accessibility: Is the alt attribute descriptive (or explicitly empty alt="" for purely decorative images)?

Implementing these HTML standards requires slightly more markup, but the return on investment in performance and SEO is substantial.

Akshay Singh — Author

Written by Akshay Singh

Full-stack engineer and technical writer at TheDailyDevs. Sharing practical step-by-step tutorials and best practices for modern web technologies.

TheDailyDevsTheDailyDevs
TheDailyDevs is a developer-first blog and knowledge hub created by passionate engineers to share real-world development tips, deep-dive tutorials, industry insights, and hands-on solutions to everyday coding challenges. Whether you're building apps, exploring new frameworks, or leveling up your dev game, you'll find practical, no-fluff content here, updated daily.