Free Astro tutorials for complete beginners.

Beginner's Guide to Astro

How to Add Images to an Astro Site

Add an image to your Astro site, and let Astro shrink and convert it automatically. One test image went from 62 kB to 5 kB.

Tested on Astro 7.4

Cover image for How to Add Images to an Astro Site
In this guide
  1. Step 1: Add your image to the project
  2. Step 2: Use the Image component
  3. Step 3: Build and look at the result
  4. Images in Markdown
  5. Pictures in several formats
  6. Images from other websites
  7. Frequently asked questions

Astro can take a big image and turn it into a small, modern one while your site builds. In our test, a 62 kB PNG became a 5 kB WebP file, with no visible change. Here’s how to use it.

Step 1: Add your image to the project

Create a folder called src/images and put your picture in it. For this guide, we’ll call it team-photo.png.

Keep images inside src whenever you can. Files in public are copied exactly as they are, with no optimization.

Step 2: Use the Image component

Open the page where you want the picture, and add two imports and one tag:

---
import { Image } from 'astro:assets';
import teamPhoto from '../images/team-photo.png';
---
<Image src={teamPhoto} alt="Our team standing outside the office." width={600} />

The alt text is required. It’s what screen readers read out, and it helps search engines understand the picture.

Because the image is imported from src, Astro knows its real size. The width tells Astro how large to make the optimized version.

Step 3: Build and look at the result

Run npm run build. In the output you’ll see a line like this:

▶ /_astro/team-photo.CroshMPU_Z1Oj0fM.webp (before: 62kB, after: 5kB)

Astro converted the image to WebP, resized it, and gave it a unique file name so browsers can cache it safely. The HTML it produces also includes loading="lazy", so images lower down the page don’t load until needed.

Images in Markdown

In a blog post written in Markdown, use the normal image syntax with a relative path:

![A starry night sky.](../../images/stars.png)

Images referenced this way from src are optimized too. The Image component itself isn’t available in .md files, only in .astro and .mdx files.

Pictures in several formats

To serve AVIF where the browser supports it, with a fallback, use the Picture component:

---
import { Picture } from 'astro:assets';
import teamPhoto from '../images/team-photo.png';
---
<Picture src={teamPhoto} formats={['avif', 'webp']} alt="Our team standing outside the office." />

Images from other websites

Astro only optimizes remote images from sites you approve. Add them to astro.config.mjs:

export default defineConfig({
  image: {
    domains: ['example.com'],
  },
});

Remote images also need a width and height, because Astro can’t read their size in advance.

Frequently asked questions

Should images go in src or public?

Put them in src when you can, so Astro can optimize them. Use public only for files that must keep their exact name and bytes, like a favicon.

Why does Astro want alt text?

The alt text describes the image for people who can't see it, and for search engines. Astro's Image component requires it. For a purely decorative image, use alt="".

Can I use images from another website?

Yes, by giving the full address as the src. Astro only optimizes remote images from sites you list in your config, and it needs a width and height for them.

Stuck on a step?

Check the common errors guide, look up a word in the glossary, or ask in the official Astro Discord, where the support channels are friendly to beginners.

Keep reading

  1. How to Deploy an Astro Site to Cloudflare Workers

    Put your Astro site online with Cloudflare Workers in four steps. This is how AstroBeginner itself is hosted, so every step has been run for real.

    Tutorials, 7 minute read, November 6, 2026

  2. How to Add Tailwind CSS to Astro

    Add Tailwind CSS to an Astro site with one command, then style your first element. Tested on Astro 7.4 and Tailwind 4.

    Tutorials, 5 minute read, November 6, 2026

  3. How to Add a Sitemap to Astro

    Create a sitemap for your Astro site in three steps, so search engines can find every page. Includes the robots.txt line.

    Tutorials, 4 minute read, November 6, 2026

Try "blog", "deploy" or "EmDash".