Background images are usually set up in CSS with the background-image property. They aren’t automatically optimized because they don’t pass through Astro’s image optimization pipeline.
.selector { background-image: url('...');}But there are two ways to optimize these background images:
- Use an inline
background-imageproperty - Use a real image with
object-fit: cover
We’ll go through both methods here.
Using an inline background-image property
The trick here is to optimize the image via Astro’s getImage function before passing it into the CSS background-image property.
In this case, we don’t need to insert widths because background-image does not support any width-based srcset/sizes mechanism.
---import { getImage } from 'astro:assets'import myBackground from '../assets/background.png'const background = await getImage({ src: myBackground, format: 'avif' })---
<div style={`background-image: url(${background.src}); background-size: cover;`}></div>I also made a component called BackgroundImage to simplify the process.
---import { BackgroundImage } from '@splendidlabz/astro'---
<BackgroundImage src="/src/assets/hero.jpg"> <!-- Your content goes here --></BackgroundImage>Using a real image with object-fit: cover
I prefer this method because we can actually deliver the best experience for users — by using an img with srcset.
To do this, we need to put both the image and the content inside a stack — which is basically a CSS Grid with children overlaid onto each other.
Here’s what the HTML looks like:
<div class="stack"> <!-- image goes here --> <div><!-- content goes here --></div></div>And here’s what stack looks like:
/* Simple version of stack */.stack { display: grid; position: relative; grid-template-columns: minmax(0, 1fr);
> * { grid-column: 1 / span 1; grid-row: 1 / span 1; }}We can use Astro’s Picture component to build the image. We’ll include widths here to let browsers download an image of the appropriate size, and formats to produce modern formats like avif and webp.
---import { Picture } from 'astro:assets'import image1 from '../assets/image1.png'---
<div class="stack"> <Picture src={image1} alt="" widths={[400, 800, 1200]} formats={['avif', 'webp']} /> <div><!-- content goes here --></div></div>The important part here is to add a class to the image, along with the following CSS.
This overrides Astro Picture’s tendency to preserve the image’s intrinsic sizing — which allows the image to be a background image that conforms to the size of the content.
---import { Picture } from 'astro:assets'import image1 from '../assets/image1.png'---
<div class="stack"> <Picture class="object-background" src={image1} alt="" ... /> <div><!-- content goes here --></div></div>.object-background { position: absolute; z-index: -1; inset: 0; width: 100%; height: 100%; object-fit: cover;}To make things simpler, Splendid Astro’s Image component has a backgroundMode property that does this for you automatically.
---import { Image } from '@splendidlabz/astro'---
<div class="stack"> <Image src="/src/assets/image1.png" backgroundMode /> <div><!-- content goes here --></div></div>Code for you to copy
Using background-image
---import { BackgroundImage } from '@splendidlabz/astro'---
<BackgroundImage src="/src/assets/hero.jpg"> <!-- Your content goes here --></BackgroundImage>Using a real image
---import { Image } from '@splendidlabz/astro'---
<div class="stack"> <Image src="/src/assets/hero.jpg" backgroundMode /> <div><!-- content goes here --></div></div>Welcome! Unfortunately, you don’t have access to this lesson. To get access, please purchase the course or enroll in Magical Dev School.
Unlock this lesson