Image Optimization Basics for the Web
If you’ve ever waited forever for a page to load, there’s a good chance big images were to blame. The good news: with a few basic skills, you can dramatically speed up your site without making your images ugly or blurry.
In this beginner-friendly guide, you’ll learn what image optimization is, why it matters, and simple steps to do it yourself. We’ll walk through practical examples, including HTML code you can copy, paste, and tweak.
What is image optimization (in plain English)?
Image optimization means making your images as small in file size as possible while still looking good. Smaller image files load faster, which means:
- Your website feels snappy instead of slow
- Visitors are less likely to leave
- Search engines (like Google) usually rank you better
- Mobile users save data
We optimize images mainly by:
- Choosing the right file format
- Resizing images to the right dimensions
- Compressing images so they’re smaller
- Writing good HTML so browsers load the right version
Let’s go step-by-step.
Step 1: Choose the right image format
There are many image formats, but you only need to remember a few for the web:
- JPEG (JPG) – Great for photos (people, places, products). Small file size, good quality.
- PNG – Great for images that need transparency or sharp edges (logos, icons, simple graphics).
- WebP – Newer format that usually makes files even smaller than JPEG/PNG, with good quality.
- SVG – Best for simple logos and icons made of shapes and lines. Scales perfectly without getting blurry.
Simple rule of thumb:
- Use JPEG or WebP for photos.
- Use PNG or SVG for logos, icons, and simple graphics.
Try it yourself: Take one photo and one logo. Save the photo as JPEG and the logo as PNG. Compare file sizes and how they look in your browser.
Step 2: Resize your images to the right dimensions
Many beginners upload huge images straight from a camera or phone (like 4000×3000 pixels) and then show them on the page at a much smaller size (like 800×600). The browser still downloads the giant image first, which is slow.
Better approach: Resize the image to roughly the size it will appear on the page.
For example:
- Big banner image: about 1200–1600px wide
- Content image (inside an article): about 800–1200px wide
- Small thumbnail: about 300–400px wide
You can resize images using free tools like:
- Online: TinyPNG, Squoosh, ILoveIMG, Canva
- Desktop: Photoshop, GIMP, Preview (Mac), Paint (Windows)
Try it yourself: Take one of your photos, resize it from full size (e.g., 4000px wide) down to 1200px wide, and save as JPEG. Compare the file size before and after.
Step 3: Compress your images
Compression means squeezing out extra data from the image file so it’s smaller to download.
There are two main types:
- Lossless – No quality lost, just smarter storage (often used for PNG, SVG)
- Lossy – Slight quality reduction, but much smaller files (often used for JPEG, WebP)
Most online tools let you choose a quality level (like 60%, 70%, etc.). Surprisingly, 60–80% quality often looks almost the same as 100% to the human eye, but can cut file size in half.
Try it yourself: Use a tool like TinyPNG or Squoosh. Upload your image, slide the quality slider down to around 70–80%, and watch the file size drop. Zoom in to check if you’re happy with the quality.
Step 4: Display images in HTML (basic example)
Now that you’ve chosen a format, resized, and compressed your images, you need to add them to your web page.
Here’s a very simple HTML example:
<!-- Basic image tag -->
<img
src="images/product-800.jpg" <!-- the image file to load -->
alt="Red running shoes on a white background" <!-- text shown if image can't load and for screen readers -->
width="800" <!-- actual width of the image in pixels -->
height="600" <!-- actual height of the image in pixels -->
>
What each part does:
src– The path (location) to your image file.alt– A text description. Helpful for accessibility and SEO.widthandheight– Tell the browser the image’s size so it can reserve space and reduce layout shifting.
The result: Your image appears on the page at the right size, and the browser knows its dimensions in advance.
Try it yourself: Create a simple
index.htmlfile, save an image in animagesfolder, and use the code above to show it in your browser.
Step 5: Make images responsive with srcset
On a phone screen, you don’t need a giant 1600px-wide image. It’s a waste of bandwidth. You can give the browser multiple versions of the same image and let it choose the best one.
First, create multiple sizes of the same image, for example:
product-400.jpg(400px wide)product-800.jpg(800px wide)product-1200.jpg(1200px wide)
Then use the srcset and sizes attributes:
<img
src="images/product-800.jpg" <!-- default image -->
srcset="
images/product-400.jpg 400w, <!-- 400px wide version -->
images/product-800.jpg 800w, <!-- 800px wide version -->
images/product-1200.jpg 1200w <!-- 1200px wide version -->
"
sizes="
(max-width: 600px) 100vw, <!-- on small screens: use full width of screen -->
(max-width: 1200px) 50vw, <!-- on medium screens: use half the screen width -->
400px <!-- otherwise: assume a fixed 400px width -->
"
alt="Red running shoes on a white background"
>
How this works in simple terms:
srcsetlists the available image files and how wide they are (400w,800w,1200w).sizestells the browser how much space the image will roughly take up on different screen sizes.- The browser picks the smallest image that will still look good on the current screen.
Result: Smaller phones get smaller images, big screens get bigger ones. Everyone loads only what they need.
Try it yourself: Create 2–3 different widths of the same image, add the
srcsetandsizescode, then resize your browser window and refresh to see which file loads (you can check in the browser dev tools Network tab).
Step 6: Use modern formats like WebP (with fallback)
Many modern browsers support WebP, which is often smaller than JPEG or PNG while keeping good quality. You can offer a WebP version and a fallback JPEG/PNG for older browsers.
Use the <picture> element:
<picture>
<!-- First, offer WebP (modern format) -->
<source
srcset="images/product-800.webp"
type="image/webp"
>
<!-- Fallback: regular JPEG for older browsers -->
<source
srcset="images/product-800.jpg"
type="image/jpeg"
>
<!-- Final <img> is the default / fallback -->
<img
src="images/product-800.jpg"
alt="Red running shoes on a white background"
>
</picture>
What happens here:
- If the browser supports WebP, it uses
product-800.webp. - If not, it falls back to
product-800.jpg.
Either way, the visitor sees the same image, but modern browsers get a smaller file.
Try it yourself: Convert one JPEG to WebP using an online tool. Then use the
<picture>example above to serve WebP, with JPEG as fallback.
Step 7: Lazy-load images so they load only when needed
If you have many images on a page, loading them all at once can be slow. Lazy loading means images load only when the visitor scrolls near them.
The good news: there’s a super simple way to do this in modern browsers using just one attribute.
<img
src="images/product-400.jpg"
alt="Red running shoes on a white background"
loading="lazy" <!-- browser delays loading until needed -->
>
This tells the browser: “Don’t load this image right away. Wait until it’s almost visible on the screen.”
Result: Faster initial page load, especially for pages with many images.
Try it yourself: Add
loading="lazy"to lower images on a long page. Refresh and check how fewer images download immediately in your browser’s Network tab.
Quick recap: Your image optimization checklist
You’ve learned a lot, so let’s summarize the key steps:
Pick the right format
- Photos → JPEG or WebP
- Logos/icons → PNG or SVG
Resize images to roughly the size they appear on your site (e.g., 800–1200px wide for content images).
Compress images using an online tool, aiming for a good balance between quality and file size.
Use HTML properly:
- Add
alttext for accessibility - Set
widthandheightwhen possible
- Add
Make images responsive with
srcsetandsizesso different devices get different image sizes.Use modern formats like WebP with
<picture>and a JPEG/PNG fallback.Lazy-load off-screen images with
loading="lazy".
Learning this can feel like a lot at first, but every small improvement makes your site faster and more enjoyable to use. Start with just one step—for example, compressing and resizing your images—and build from there. Over time, these basics will become a natural part of how you create and share images on the web.
