🔥 All-Access Pass — $199 USD (Regular $499) · Unlock all 2,836 Astro themes ➔
komal.nakrani
Get All-Access Pass ($199)
ThemesCategoriesDocsBlogTips & TricksPricingGet All-Access Pass ($199)
Performance••By Komal Nakrani

Astro Background Image: Optimized CSS Backgrounds With getImage

Make an Astro background image optimized: import it, process it with getImage(), and pass the URL to CSS with define:vars or an inline style. Includes image-set().

#Astro#Background Image#Images#CSS

For an optimized Astro background image, import the file from src/, run it through getImage() from astro:assets to get a resized WebP or AVIF, and pass the resulting URL to CSS with define:vars or an inline style. A plain url('/hero.jpg') in CSS works too, but the file ships unoptimized.

Key Takeaways

  • Images in public/ and plain CSS url() references are copied as-is, with no resizing or format conversion.
  • getImage() returns an optimized src you can use anywhere, including CSS.
  • image-set() lets the browser pick AVIF or WebP and the right density.
  • For a hero that is the LCP element, an <Image> with object-fit: cover is often faster than a CSS background.

Optimize with getImage()

---
// src/components/Hero.astro
import { getImage } from 'astro:assets';
import heroPhoto from '../assets/hero.jpg';

const bg = await getImage({ src: heroPhoto, width: 1600, format: 'webp', quality: 'mid' });
---
<section class="hero">
  <slot />
</section>

<style define:vars={{ heroBg: `url(${bg.src})` }}>
  .hero {
    min-height: 60vh;
    background: var(--heroBg) center / cover no-repeat, #0f172a;
  }
</style>

define:vars turns heroBg into a CSS custom property on the component’s elements. The #0f172a color shows while the image loads and keeps text readable if it fails. Our define:vars tip explains how the variables are injected.

Serve AVIF and WebP with image-set()

---
// src/components/Hero.astro
import { getImage } from 'astro:assets';
import heroPhoto from '../assets/hero.jpg';

const [avif, webp] = await Promise.all([
  getImage({ src: heroPhoto, width: 1600, format: 'avif' }),
  getImage({ src: heroPhoto, width: 1600, format: 'webp' }),
]);
const bg = `image-set(url(${avif.src}) type("image/avif"), url(${webp.src}) type("image/webp"))`;
---
<section class="hero" style={`background-image: ${bg}`}>
  <slot />
</section>

The browser downloads only the first format it supports. Add a second width for small screens with a media query if the image is large.

When to use the Image component instead

CSS backgrounds are discovered late: the browser must download and parse the CSS before it requests the image, and it cannot use srcset or fetchpriority. For a large hero that is likely the Largest Contentful Paint element, an absolutely positioned <Image> is usually faster:

---
// src/components/Hero.astro
import { Image } from 'astro:assets';
import heroPhoto from '../assets/hero.jpg';
---
<section class="hero">
  <Image src={heroPhoto} alt="" widths={[800, 1600, 2400]} sizes="100vw" priority class="hero-img" />
  <div class="hero-content"><slot /></div>
</section>
<style>
  .hero { position: relative; min-height: 60vh; display: grid; place-items: center; }
  .hero-img { position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; z-index: -1; }
</style>

priority loads it eagerly with high fetch priority. Use alt="" when the image is decorative. Keep CSS backgrounds for patterns, textures and images below the fold. Our LCP guide covers why this matters, and the official getImage() reference lists every option.

Images in public/

background: url('/textures/grain.png') points at public/textures/grain.png and is served unchanged. That is fine for small textures and SVG patterns. Export them at the size you need, since Astro will not resize them. The theme image docs show where hero images live in our Astro themes.

FAQ

How do I use a background image in Astro?

Either reference a file in public/ with CSS url(), or import the image, run it through getImage() and pass the optimized URL to CSS with define:vars or an inline style. The second way resizes and converts the image.

Can Astro optimize CSS background images automatically?

No. Astro only optimizes images that go through astro:assets. CSS url() references are copied as-is, so use getImage() when you want optimization.

Is a CSS background image bad for LCP?

It can be, because the browser finds it only after loading the CSS. For a hero image that is the LCP element, prefer an <Image> with priority and object-fit: cover.