Astro Image Optimisation: Formats, Responsive Sizes, and Killing Layout Shift
1 November 2026 · Updated 28 November 2026 · 13 min read · by Yang Kai
Optimise images in Astro the practical way: use the built-in Image component, pick WebP or AVIF sensibly, set responsive sizes, and prevent CLS with fixed dimensions.
Checklist inside: Astro Image Optimisation Checklist
Contents
- The five rules, up front
- Why importing images beats dropping them in `public/`
- The Image component, used properly
- When one format is not enough: the Picture component
- Choosing a format without the religion
- Responsive images: sizes are a promise to the browser
- A worked example: the 4 MB hero
- Remote images and CMS images
- Images in Markdown
- When the components do not fit: `getImage()`
- Killing layout shift (CLS)
- The image checklist
- Build time and hosting realities
- How we researched this
- What to do next
- Learn more
A content page’s images usually outweigh its HTML, CSS, and JavaScript combined — and most of that weight is self-inflicted. The original photo is wider than any screen that will show it, in a format chosen by a camera, dropped in a folder where the framework cannot touch it.
Astro ships a complete answer to this: an image pipeline that resizes, re-encodes, and fingerprints images at build time, plus components that emit the right markup for you. This guide uses them in the right order — where images live, the Image and Picture components, formats, responsive sizes, remote images, and the layout shift rules that keep pages still while images load.
The behaviour described follows the official Astro images guide and the astro:assets API reference, checked November 2026.
The five rules, up front
| Rule | Why it matters |
|---|---|
Import images from src/; never rely on public/ for content images |
Only imported images go through the optimisation pipeline |
Render with <Image /> or <Picture />, not a hand-written <img> |
The components generate sizes, formats, and dimensions for you |
Give every image real dimensions, or use a layout that infers them |
Dimensions are what stop layout shift |
Let the browser pick the size: widths + sizes, or the layout prop |
A phone should not download a desktop hero |
Mark the one hero image priority; leave everything else lazy |
The largest image loads first, the rest wait their turn |
Why importing images beats dropping them in public/
Where an image lives decides what Astro can do with it. Images imported from src/ — conventionally src/assets/ — flow through the build pipeline, powered by the Sharp image service by default. Astro can resize them, convert formats, generate multiple sizes, and fingerprint filenames so browsers can cache them aggressively.
Images in public/ are copied verbatim. Your 4 MB camera JPEG ships exactly as-is, at whatever size you saved it, and no component can optimise it after the fact.
The rule is simple: content and component images go in src/ and get imported. public/ is for the few files that must keep an exact URL — a favicon, a file referenced by absolute path from outside your pages. Most themes handle this correctly out of the box, and it is one of the quiet quality checks in how I test Astro themes before recommending them.
The Image component, used properly
The Image component from astro:assets takes an imported image and produces an <img> with optimised output: converted format, generated sizes, and loading="lazy" with decoding="async" set by default. The props that matter:
---
import { Image } from "astro:assets";
import heroShot from "../assets/hero.jpg";
---
<Image
src={heroShot}
alt="Dashboard showing weekly signups by channel"
width={1200}
height={675}
format="webp"
loading="lazy"
decoding="async"
/>
| Prop | What it controls |
|---|---|
src |
The imported image, or an authorised remote URL |
alt |
Required. The component errors without it; use alt="" only for purely decorative images |
width / height |
The output size. For a local image, give one and Astro calculates the other from the real aspect ratio |
format |
Output format — webp (the default), avif, png, jpeg, and others the service supports |
quality |
A preset (low, mid, high, max) or a number from 0 to 100 |
widths + sizes |
Generate a responsive srcset by hand (covered below) |
densities |
Generate 1x/2x versions for fixed-size images like avatars and logos |
layout |
Hand responsive sizing to Astro (covered below) |
priority |
For the hero: flips loading to eager and fetch priority to high |
alt is not decoration. Write what the image shows, for screen readers and for image search — part of doing SEO honestly on an Astro site. And notice what width and height are doing beyond sizing: they let the browser compute the aspect ratio before the file arrives, which is the layout shift fix covered below.
When one format is not enough: the Picture component
<Image /> emits a single <img> in a single format. <Picture /> emits a <picture> element with one <source> per format you list, plus a fallback image:
<Picture
src={heroShot}
alt="Dashboard showing weekly signups by channel"
formats={["avif", "webp"]}
fallbackFormat="webp"
width={1200}
height={675}
/>
Use Picture when the extra saving from AVIF is worth offering it to browsers that support it while WebP catches the rest. Use Image everywhere else — one format, less markup, nothing to misconfigure. Both components accept the same responsive props, so the choice is about formats, not sizing.
Choosing a format without the religion
WebP is the sensible default for photographs and screenshots: broadly supported, meaningfully smaller than JPEG at comparable quality, and what the Image component outputs unless you say otherwise.
AVIF can be smaller again, but it encodes more slowly at build time and its advantage shrinks on small images. On a site with hundreds of images, build duration is a real cost, so AVIF earns its place on large hero images — usually through Picture, so it is an offer rather than a requirement.
PNG remains right for crisp graphics with flat colours and transparency. SVG is not a raster format at all: it is correct for logos and icons, can be imported and used directly in current Astro, and should never be “optimised” into a PNG.
Match format to content, not to fashion, and let the build generate the variant rather than hand-exporting files.
Responsive images: sizes are a promise to the browser
A 1600-pixel hero displayed in a 400-pixel phone slot wastes most of its bytes. Responsive images fix this by offering the browser several widths and a sizes hint describing how wide the image will actually render at each breakpoint, so the phone downloads the small file and the desktop the large one.
There are two ways to get there in current Astro:
By hand, with widths and sizes. You list the widths to generate, and write the sizes value yourself:
<Image
src={heroShot}
alt="Dashboard showing weekly signups by channel"
widths={[480, 800, 1200]}
sizes="(max-width: 800px) 100vw, 1200px"
/>
The sizes value is a promise about your layout. Lie in it — claim the image renders at 1200px when your content column is 720px — and every visitor downloads a bigger file than they need.
Automatically, with the layout prop (available since Astro 5.10). Set layout="constrained" and Astro generates the srcset and sizes for you, based on the image’s dimensions, and applies the styles that make it scale down to fit its container without exceeding its set width. layout="full-width" suits hero banners that always fill their container; layout="fixed" suits images that never resize, like logos. You can also set a project-wide default with image.layout in your config and override it per image.
This is the same discipline as hydrating only what needs it: ship each visitor the version their device and viewport actually require, nothing more.
A worked example: the 4 MB hero
Take the most common case on a new content site. A phone photo, 4000 × 3000 pixels, saved as a JPEG of roughly 4 MB, used as an article hero that displays at most 1200 pixels wide in the layout — and about 400 pixels wide on a phone.
Served raw from public/, every visitor downloads all 4 MB, including the phone on a slow connection rendering it at a tenth of its width. Run through the pipeline instead — imported, rendered with <Image /> at width 1200 in WebP, with responsive sizes generated — the desktop visitor gets a file a small fraction of that size, and the phone gets the 400-pixel variant, smaller again. The exact numbers vary with the photo’s content, and this example is arithmetic, not a benchmark; the point is the shape of the win. The saving comes from three separate decisions — resize, re-encode, and per-device sizes — and skipping any one of them leaves most of it on the table.
Multiply that by every image on every page and you see why image handling, not JavaScript, is where most content sites win or lose their performance budget.
Remote images and CMS images
Images do not have to live in your repo. For a remote image, pass the full URL as src — but know the two conditions attached:
- Optimisation is opt-in per source. Astro only processes remote images from sources you authorise in your config, via
image.domainsorimage.remotePatterns. An unauthorised remote image still displays; it just ships unprocessed. - Dimensions are your problem. Astro cannot inspect a remote file the way it inspects a local import. Supply
widthandheightyourself, or addinferSizeand Astro will fetch the image to learn its dimensions — a build-time fetch that only happens for authorised domains.
For most content sites, local images plus a CMS that exports files into the build beats hot-linking a media library. If your content arrives through a content collection, the schema’s image() helper can validate cover images at build time, so a broken remote path fails the build instead of the page.
Images in Markdown
Local images referenced with Markdown syntax inside your content files are processed too: Astro runs them through the same pipeline when they live in src/. If you configure a global image.layout, those Markdown images get the responsive treatment as well. It is one more reason to keep article images beside the articles, in src/, rather than in a public/ folder where nothing can touch them.
When the components do not fit: getImage()
Some images never appear in an <img> tag: Open Graph images, CSS backgrounds, images generated for emails or feeds. For those, getImage() from astro:assets runs the same optimisation and returns the URL and attributes to use wherever you need them. Reach for it when a component genuinely cannot do the job — not as the default, since the components handle the markup details (dimensions, loading, decoding) that are easy to forget by hand.
Killing layout shift (CLS)
Cumulative Layout Shift — the page jumping as images load — happens when the browser does not know an image’s height until the file arrives. Fixed dimensions, or an aspect ratio on the wrapper, reserve the space up front and the text never moves. Three rules eliminate nearly all image CLS:
- Always set width and height, or use a
layoutthat carries the aspect ratio for you. - Never inject images above existing content after load without a reserved slot.
- Treat late-loading embeds with the same suspicion as images.
On an ad-supported or embed-heavy page this discipline matters even more, because every late element compounds the shift. Note the connection to priority: it changes when the hero loads, not whether space is reserved — a prioritised hero with no dimensions still shoves the page around.
The image checklist
- Location: Content images live in
src/assets/and are imported — not dropped inpublic/ - Component: Rendered with
Image(orPicturefor multiple formats), never a hand-written<img>for local files - Dimensions: Real
widthandheighton every image, matching the source’s aspect ratio - Format: WebP default for photos; AVIF only where the size win beats slower builds; SVG kept as SVG for logos and icons
- Responsive: A
layoutset (or honestwidths+sizes) so each device downloads an appropriately sized file - Loading: The hero image carries
priority; everything below the fold staysloading="lazy"withdecoding="async" - Alt text: Descriptive alt on meaningful images; empty
alt=""on purely decorative ones - Weight sanity: No single content image ships larger than the layout can display; screenshots cropped to the relevant region
- CLS check: Page loaded on a throttled connection with no visible jumping of text
- Social images: A dedicated, correctly sized Open Graph image exists, generated with
getImage()if it needs processing
Build time and hosting realities
Optimisation happens at build time for local images, so a gallery-heavy site builds slower. That is the honest trade, and it argues for preparing your source files to sane dimensions before they enter the repo rather than feeding the pipeline 8000-pixel originals.
If you deploy on Cloudflare, the interaction between build-time optimisation and the platform is covered in running Astro on Cloudflare. The short version: pre-built static images suit the platform perfectly, which is one reason deploying Astro on Cloudflare Pages is this site’s default recommendation.
How we researched this
Component props, defaults, and version notes (the layout and priority props arrived in Astro 5.10; inferSize in Astro 4.4) come from the official Astro images guide and the astro:assets API reference, checked November 2026. The format and workflow recommendations are editorial judgement built on that documented behaviour — this article makes no benchmark claims of its own.
Frequently asked questions
Does Astro optimise images in public/?
No. Files in public/ are copied as-is and responsive layouts do not apply to them. Move content images into src/assets/ and import them so the build pipeline can resize and convert them.
WebP or AVIF — which should I pick?
WebP for most sites: fast to encode, well supported, much smaller than JPEG, and the Image component’s default output. AVIF for large hero images where the extra saving is real, offered through Picture with a WebP fallback.
What does the priority prop actually do?
It sets the image to load eagerly with high fetch priority and synchronous decoding — the combination you want for the one above-the-fold image that is usually the page’s largest element. Use it on that image only; prioritising everything prioritises nothing.
Why does my page still shift if images have dimensions? Check the other late arrivals: web fonts, embeds, and any image injected by a script after load. CLS is a whole-page property; images are just its most common cause.
Do I need an image CDN with Astro? Not to start. Build-time optimisation plus a normal CDN in front of your static hosting covers most content sites. Add an image service when build times or remote images genuinely demand it.
What to do next
Run the checklist against your heaviest article page and fix the top three findings — usually public-folder images, missing dimensions, and a hero image loading lazily when it should carry priority.
Then look at your theme choice with fresh eyes: the guide to choosing an Astro theme explains why image handling is one of the fastest ways to tell a carefully built theme from a pretty one.
Learn more
- Astro islands explained: partial hydration in practical terms
- Astro content collections: schemas, references, and loaders
- Astro for vibe coders: the beginner’s route in
- Running Astro on Cloudflare
Category: Astro Themes & Starters · Tags: #astro #themes
Next step: this article is part of Astro guides, which covers the full topic from first setup to advanced use.