Lesson 19 / Performance
Image Optimization
Optimize image formats, dimensions, compression, responsive delivery, accessibility, and loading behavior for faster websites.
Course Role
Core
Part of the main course path. Prioritize this before moving to optional polish or deeper references.
Teacher Notes / In-Class Use
Demo Live
- Walk through the interactive demo before students start changing their own project files.
- Connect the demo back to the first goal: Choose appropriate image formats such as `JPEG`, `PNG`, `SVG`, `WebP`, and `AVIF`
Try In Class
- Optimize one photo by resizing and compressing it.
- Have students make one visible change, save, refresh, and explain what changed.
Submit Or Check
- Ask students to show the work in the browser, not only in the editor.
- Have students commit their progress with a clear message when the checkpoint is stable.
Watch For
- Students copying code without checking file paths, spelling, or capitalization.
- Visual changes that work locally but break when the project is published.
Learning Goals
- Choose appropriate image formats such as `JPEG`, `PNG`, `SVG`, `WebP`, and `AVIF`
- Resize and compress images so file size matches the design need
- Write useful `alt` text and reserve image space with `width` and `height`
- Use `srcset`, `sizes`, `picture`, and `loading="lazy"` appropriately
Interactive Demo
How to use this demo.
Use the demo as a small lab. Change one thing, observe the result, then connect it back to your own project.
What To Try
- Change image format, width, loading behavior, and alt text.
- Watch the estimated size and code sample update.
What Changes
- Image choices affect performance, accessibility, and layout stability.
- The code sample shows the markup decisions behind the preview.
What To Notice
- Optimization is not only compression. It includes size, loading, dimensions, and text alternatives.
- Alt text should communicate the image purpose in context.
Apply It
- Pick one image in your project and verify its size, loading behavior, and alt text.
Interactive Demo
Image Optimization Lab
Change the format, displayed width, loading behavior, and alt text purpose. Watch how the markup and performance notes respond.
- Estimated file
- 160 KB
- Selected source
- 800w
- Loading
- Lazy
<img
src="campus-800.webp"
srcset="campus-400.webp 400w, campus-800.webp 800w, campus-1200.webp 1200w"
sizes="(max-width: 720px) 100vw, 720px"
alt="Campus walkway at sunset"
loading="lazy"
> What to notice
- Format affects file size, transparency support, and image quality.
- Displayed width should guide the source sizes you export.
- Lazy loading helps most when images start below the first screen.
Try this
- Switch from WebP to PNG and compare the estimated file size.
- Increase the display width and watch the selected source change.
- Change the alt text purpose and explain which version fits the image.
This demo uses extra JavaScript for teaching. The code sample shows the pattern to practice. View full demo source.
Introduction
Images often make up a large part of a page's download size. Optimizing them can make a site feel faster, especially on mobile networks.
Good image optimization balances quality, file size, layout stability, accessibility, and responsive delivery.
Image Optimization Workflow
A consistent workflow helps you avoid the most common image problems before they reach the browser.
- Choose the right format for the image content.
- Crop and resize the image to the dimensions the design actually needs.
- Compress the image to reduce file size.
- Add meaningful
alttext or emptyalt=""for decorative images. - Add
widthandheightto reserve space and reduce layout shift. - Use responsive images when the same image appears at different sizes.
- Lazy-load below-the-fold images with
loading="lazy". - Test the result in
Lighthouse, DevTools, or the Network panel.
Dimensions vs File Size
Pixel dimensions describe how many pixels are in the source file, such as 2400 x 1600.
Displayed size is how large the image appears in the layout, often controlled by CSS.
File size is how much data the browser downloads, such as 85 KB or 2.4 MB.
Avoid uploading a huge source image and shrinking it with CSS. The browser still downloads the huge file.
Choosing the Right Image Format
Different formats are good at different jobs. The right choice depends on the image content, transparency needs, browser support, and quality expectations.
| Image Type | Good Formats | Why |
|---|---|---|
| Photos | JPEG, WebP, AVIF | Good compression for complex photographic detail. |
| Transparent graphics | PNG, WebP, AVIF | Supports transparency when needed. |
| Logos and icons | SVG | Scales cleanly and usually stays small for simple vector art. |
| Screenshots or UI images | PNG, WebP | Preserves sharp edges and interface detail. |
| Simple animation | Animated WebP or video before GIF | GIF files are often large and limited in color. |
Format Notes
JPEGis useful for photos, but it does not support transparency.PNGis useful for sharp edges and transparency, but large photos can become heavy.SVGis best for simple logos, icons, and vector graphics.WebPusually compresses better thanJPEGorPNGand supports transparency.AVIFcan be even smaller thanWebP, but always check visual quality and browser needs.
Resizing Images
Resize source images before adding them to a site. If a card image displays around 600px wide, the source image probably does not need to be 3000px wide.
Keep larger versions only when the design truly needs them, such as full-width hero images, high-density screens, or zoomable galleries.
Compressing Images
Compression reduces file size. Lossy compression removes image data to make files smaller. Lossless compression keeps the original image data but may not shrink files as much.
| Compression Type | Use For | Tradeoff |
|---|---|---|
| Lossy | Photos and large hero images | Smaller files, possible quality loss. |
| Lossless | Logos, UI graphics, screenshots, and sharp edges | Better detail, larger files. |
Image Optimization Tools
- Squoosh for comparing formats, resizing, and compression settings.
- TinyPNG for quick
PNG,JPEG, andWebPcompression. - Adobe Photoshop or similar editors for cropping, resizing, and export settings.
- Lighthouse and the Chrome DevTools Network panel for checking page impact.
Image Markup Basics
A solid img tag includes the image path, meaningful alt text, and dimensions that reserve layout space while the image loads.
<img
src="team-photo.webp"
width="800"
height="533"
alt="Design team reviewing wireframes around a table"
decoding="async"
> Writing Alt Text
alt text describes the purpose or content of an image for people who cannot see it. It also helps when images fail to load.
Do not stuff keywords into alt text. Write for people first.
| Image Type | Alt Approach | Example |
|---|---|---|
| Meaningful image | Describe the useful content | alt="Student portfolio homepage with a large project grid" |
| Decorative image | Use empty alt text | alt="" |
| Image link or button | Describe the action or destination | alt="View project gallery" |
| Image with text in it | Include the important text nearby or in alt | alt="Sale ends Friday" |
Responsive Images with Srcset and Sizes
srcset gives the browser multiple image file options. sizes tells the browser how wide the image will appear in the layout.
sizes describes the rendered display width, not the original image width.
<img
src="project-800.webp"
srcset="project-400.webp 400w, project-800.webp 800w, project-1200.webp 1200w"
sizes="(max-width: 700px) 100vw, 700px"
width="800"
height="533"
alt="Portfolio project displayed on a laptop"
loading="lazy"
decoding="async"
> When to Use Picture
Use picture when the browser needs different image files for reasons beyond simple size selection.
| Need | Why `picture` Helps |
|---|---|
| Art direction or crop changes | Serve a different crop for mobile and desktop. |
| Format fallback | Offer AVIF or WebP with a fallback image. |
| Different image ratios | Use a square image on small screens and a wide image on desktop. |
<picture>
<source type="image/avif" srcset="hero.avif">
<source type="image/webp" srcset="hero.webp">
<img
src="hero.jpg"
width="1200"
height="675"
alt="Finished website shown on a desktop monitor"
>
</picture> Lazy Loading
Use native lazy loading for images that appear below the first viewport. This lets the browser delay downloading images until they are likely to be needed.
For beginners, loading="lazy" is usually the right first choice. JavaScript lazy loading is only needed for more custom behavior.
<img
src="gallery-image.webp"
width="600"
height="400"
alt="Gallery wall with student web design mockups"
loading="lazy"
decoding="async"
> Hero and LCP Images
The main above-the-fold image often affects Largest Contentful Paint, or LCP. This is usually a hero image, feature image, or large first-screen visual.
Do not lazy-load the main hero image. The browser should discover and load it early.
Use fetchpriority="high" only for the most important above-the-fold image. Do not add it to every image.
<img
src="hero.webp"
width="1400"
height="800"
alt="Student portfolio homepage displayed on a laptop"
fetchpriority="high"
decoding="async"
> Testing Image Performance
- Use the Chrome DevTools Network panel to sort images by file size.
- Use Lighthouse to check image sizing, modern formats, lazy loading, and layout shift warnings.
- Test on mobile viewport sizes, not just desktop.
- Compare visual quality after compression. Smaller is not better if the image looks broken.
Common Mistakes
| Mistake | Why It Hurts | Fix |
|---|---|---|
Missing alt text | The image may be inaccessible | Write meaningful alt text or use alt="" for decorative images. |
Missing width and height | The page may shift while images load | Add dimensions that match the image aspect ratio. |
| Lazy-loading the hero image | The most important image may load too late | Load the hero image normally and consider fetchpriority="high". |
Using PNG for large photos | File size can be much larger than needed | Use JPEG, WebP, or AVIF for photos. |
| Uploading huge source images | Users download unnecessary pixels | Resize images before publishing. |
Using srcset without correct sizes | The browser may choose the wrong file | Make sizes match the rendered layout width. |
Using GIF for large animations | Files can be very heavy | Use video or animated WebP when appropriate. |
Checkpoint
Before moving on, make sure these feel true.
- I can explain the main concept in my own words.
- I can apply this lesson to my current project.
- I can verify the result in the browser.
- I can commit the change with a clear message.
Project Connection
This lesson supports current class projects.
Practice
- Optimize one photo by resizing and compressing it.
- Choose appropriate formats for a photo, logo, and screenshot.
- Add
width,height, and meaningfulalttext to an image. - Create one
srcsetandsizesexample for a responsive image. - Add
loading="lazy"to below-the-fold images. - Identify one image that should not be lazy-loaded because it is above the fold.
- Test image file sizes in Lighthouse or the Chrome DevTools Network panel.
Resources
- [Squoosh image optimizer](https://squoosh.app/)
- [TinyPNG image compression](https://tinypng.com/)
- [MDN Responsive Images](https://developer.mozilla.org/en-US/docs/Learn_web_development/Core/HTML/Responsive_images)
- [MDN Lazy Loading](https://developer.mozilla.org/en-US/docs/Web/Performance/Lazy_loading)
- [web.dev Optimize Images](https://web.dev/learn/images)