Web Fundamentals · Lesson 5 of 10 · 4 min
Image, Video, and Media Optimization Strategies
This is a core performance topic. Get it wrong, and you signal you don't understand how to build for the modern web.
After this, you will be able to explain precisely how to diagnose and fix the most common media-related performance issues that tank Core Web Vitals scores. This isn't about saving a few kilobytes; it's about controlling the user's perception of speed.
Nail the LCP and CLS questions
Largest Contentful Paint (LCP) and Cumulative Layout Shift (CLS) are directly tied to media. In an interview, you must connect your optimization strategy back to these metrics.
The LCP element is often the main "hero" image. If it's slow to load, your LCP score is poor. The fix is to make the browser discover and download it faster. Use <link rel="preload"> or, even better, fetchpriority="high" on the <img> tag itself. This tells the browser to prioritize this image over others.
<head>
<!-- Good: Preload the LCP image so download starts sooner -->
<link rel="preload" as="image" href="/images/hero-large.webp" imagesrcset="..." imagesizes="...">
</head>
<body>
<!-- Better: More direct hint, no need for the head element -->
<img src="/images/hero-large.webp" fetchpriority="high" alt="...">
</body>
CLS happens when content shifts after rendering. An image loading without its dimensions reserved is the classic cause. The browser renders text, then the image arrives and pushes the text down.
The fix is simple: always provide width and height attributes on your <img> and <video> tags. The browser will use these to calculate the aspect ratio and reserve the correct amount of space before the media even downloads.
The trap: A candidate says "use CSS to set the image size." That's not enough. The browser needs the width and height attributes in the HTML to reserve space before CSS is even parsed. You can still override dimensions with CSS, but the initial layout will be stable.
Serve the right image with srcset and sizes
Serving a 2000px-wide image to a 375px mobile screen wastes bandwidth and slows down rendering. Responsive images are not optional. You use the srcset and sizes attributes to let the browser choose the best image for the job.
srcset provides a menu of images and their intrinsic widths (480w, 800w, 1200w).
sizes tells the browser how wide the image will be in the layout under different conditions.
The browser uses sizes to know what it needs, then picks the smallest image from srcset that is big enough.
<img
src="/images/fallback.jpg"
srcset="/images/image-480w.jpg 480w,
/images/image-800w.jpg 800w,
/images/image-1200w.jpg 1200w"
sizes="(max-width: 600px) 100vw, 50vw"
width="1200"
height="800"
loading="lazy"
alt="A descriptive alt text."
>
Reading the sizes attribute: "If the viewport is 600px or less, this image will be 100% of the viewport width. Otherwise, it will be 50% of the viewport width." The browser uses this context to pick the best fit from the srcset options.
Interview angle: You will be asked to explain the difference between
wandxdescriptors insrcset.xis for device pixel ratio and is a blunt instrument (1x,2x).wcombined withsizesis more powerful because it accounts for both screen density and the image's layout size. Always preferwandsizes.
Use modern formats and art direction with <picture>
While srcset handles different sizes of the same image, the <picture> element gives you two extra capabilities: format switching and art direction.
Format switching: Serve modern, efficient formats like AVIF or WebP to capable browsers, with a fallback to JPEG or PNG. AVIF can be ~50% smaller than an equivalent JPEG. The browser picks the first <source> it supports.
Art direction: Serve completely different images (e.g., a different crop or aspect ratio) at different breakpoints. This is for when simply scaling an image down isn't good enough.
Use <picture> when you need these specific controls. Otherwise, a simple <img> with srcset is cleaner.
<picture>
<source type="image/avif" srcset="photo.avif">
<source type="image/webp" srcset="photo.webp">
<img src="photo.jpg" alt="A photo." width="1000" height="750">
</picture>
And don't forget SVG for logos, icons, and diagrams. It's a vector format, meaning it's resolution-independent and usually has a tiny file size.
Master the loading strategy
Not every image needs to be loaded immediately. The strategy depends on where the image is.
Above the fold:
- Critical (LCP): Load it immediately and give it high priority (
fetchpriority="high"). - Not critical: Let it load normally. Don't add any special attributes.
Below the fold:
- Always: Lazy load it with
loading="lazy". The browser won't download the image until the user scrolls near it. This is a one-word performance win for offscreen content.
This simple decision framework shows you're thinking strategically about resource prioritization, not just applying a rule blindly.
Don't forget video (and kill the GIF)
Video optimization follows similar principles. The goal is to prevent a massive video file from blocking the initial page load.
Use these attributes on your <video> tag:
poster: An image placeholder shown before the video loads. This prevents a blank space and CLS.preload="none": Tells the browser not to download any part of the video until the user interacts with it.mutedandplaysinline: Often required forautoplayto work on mobile devices. Useautoplaysparingly, as it consumes user data.
Finally, stop using animated GIFs. A muted, looping <video> is orders of magnitude smaller and offers better quality.
Instead of my-animation.gif, use this:
<video autoplay loop muted playsinline width="400" height="300">
<source src="my-animation.webm" type="video/webm">
<source src="my-animation.mp4" type="video/mp4">
</video>
This is a non-negotiable best practice. If an interviewer sees "GIF optimization" on your resume, they will ask you about this.
Try it
Open DevTools on a media-heavy e-commerce product page. Using the Network tab (disable cache, filter by "Img"), identify the LCP image. Is it using fetchpriority? Are below-the-fold images using loading="lazy"? Are they serving WebP or AVIF? Find three concrete optimizations you would make.
Next: We'll move up the stack to Network Performance: HTTP/2, Caching, and CDNs.
Lock it in — apply this lesson under interview conditions.